Character encoding in NSString - objective-c

I am working on an iOS application and I have a basic problem. I have spent many hours to resolve it but I haven't had any success.
I have some JSON like this : {"htmlCode":"<thead><tr><td class=/"/"><p>Protected.....
SBJsonParser *jsonParser = [[[SBJsonParser alloc]init]autorelease];
NSDictionary *results = [jsonParser objectWithString:jsonTableData];
NSLog(#" result %#",results);
result (null)
My problem is the :/"/" - how I can replace it with \"\"?
i would like to do something like this :
jsonTableData = [jsonTableData stringByReplacingOccurrencesOfString:#"/"/"" withString:#"\"\""];

You can replace characters in string like this!
NSString *str = #"<thead><tr><td class=/\"/\"><p>Protected";
NSLog(#"%#", str);
str = [str stringByReplacingOccurrencesOfString:#"/" withString:#"\\"];
NSLog(#"%#", str);

Related

Objective-c how to convert NSURL into NSString?

Total objective-c noob here with a question.
Is there a way to convert NSURL into NSString in one line?
I need to retrieve URL from sqlite database abd then save it into string.
Currently the line i want to convert looks like this ->
MyString.url = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 1)];
so ofcourse im getting 'Incompatible pointer types assigning to 'NSURL *' from 'NSString *''
:)
Try This :
NSString *aStrUrl = [aUrlObj absoluteString];
You can use absoluteString property of NSURL
Example:
NSString *urlString = [url absoluteString];
In Swift
var urlString = url.absoluteString
If you're working on swift than use :
var urlStr : String = myUrl.absoluteString
You can use any one
NSString *string=[NSString stringWithFormat:#"%#",url1];
or
NSString *str=[url1 absoluteString];
NSLog(#"string :: %#",string);
string :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAAA1F7476071FE/DuplicateMedia.app/loading_circle_animation.gif
NSLog(#"str :: %#", str);
str :: file:///var/containers/Bundle/Application/E2D7570B-D5A6-45A0-8EAA-A1F7476071FE/DuplicateMedia.app/loading_circle_animation.gif

appending string- works with string with one word but not two

I'm appending a string to a url which then inputs into a database, it works with a single worded string e.g
NSString * string = #"one";
however if my string has two words such as
NSString * string = #"one two";
it does not work. Please see the code below.
NSMutableString * urlString = [NSMutableString stringWithString:url];
[urlString appendString:[NSString stringWithFormat:#"?%#=%#",kWord,word]];
The problem obviously lies with stringWithFormat;
turns out the problem is with the space.
Space characters (and certain others) are not allowed in URLs. You need to convert the space to %20. Here is the proper solution:
NSString *url = #"http://example.com/process.php";
NSString *kword = #"param";
NSString *word = #"one two";
NSMutableString * urlString = [url mutableCopy];
[urlString appendFormat:#"?%#=%#", kWord, [word stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
I have tried couple exampes here, hope it helps.
NSString *string1 = #"String1";
NSString *string2 = #"String2 String3";
NSMutableString *appendingString=[NSMutableString stringWithString: [string1 stringByAppendingString:string2]];
NSLog(#"String1:%# String2:%#",string1,string2);
NSLog(#"StringwithAppend:%#",appendingString);
//------
NSString *url=#"www.abc.com";
NSMutableString * urlString = [NSMutableString stringWithString:url];
NSLog(#"URL String before append:%#",urlString);
NSString *kWord=#"key";
NSString *word=#"word";
[urlString appendString:[NSString stringWithFormat:#"?%#=%#",kWord,word]];
NSLog(#"URL String after append:%#",urlString);
Console Log:
String1:String1 String2:String2 String3
StringwithAppend:String1String2 String3
URL String before append:www.abc.com
URL String after append:www.abc.com?key=word
Try it this way.
NSString * urlString= [NSString stringWithFormat:#"%#?%#=%#", url, kword, word];

String to UTF8 Char Conversion in Objective-C

I'm trying to save an UTF8 char to a string and print it to a label.
If I hard code it works fine:
NSString *param = #"\uf02e";
NSLog(param);
Result:
2012-10-24 16:09:56.522 i[22996:12c03] 
By the way if I'm saving the char to a string I can't go back.
NSString *myString = [NSString stringWithFormat:#"%#",[item objectForKey:content]];
NSLog(myString);
Result:
2012-10-24 16:18:47.289 i[23105:12c03] \uf02e
Any solution for this? Thanks.
EDIT
item is an NSDictionary and [item objectForKey:content] is a string.
NSString *param = #"\uf02e";
NSDictionary* item = [NSDictionary dictionaryWithObject: param forKey: #"key"];
NSString *myString = [NSString stringWithFormat:#"%#",[item objectForKey: #"key"]];
NSLog(myString);
Works fine for me. So the error is in the value, that you're inserting into the dictionary.

Parsed TouchXML XML file crashes when reading NSString

I'm able to successfully parse the contents of a XML file using TouchXML, but when I try to read an individual NSString, from the NSMutableArray that stores the parsed content, the iPhone app crashes.
My NSLog shows me that the file has been parse as it should, giving this output:
(
{
href = "mms://a19349.l412964549958.c41245496.f.lm.akamaistream.net/D/194359/4125596/v0001/reflector:49944";
},
{
href = "mms://a4322.l4129624350471.c414645296.a.lm.akamaistream.net/D/473432/4129566/v0001/reflector:546441";
} )
Here is the code I'm using to do the parsing:
NSMutableArray *res = [[NSMutableArray alloc] init];
.... Parsing happens here ....
Then I try to retrieve the string from the NSMutableArray, using this code (and the app crashes when trying to read this line of code, posted below NSMutableString *string1 = [NSMutableString stringWithString:url];
NSString *url = [[NSString alloc] init];
url = [res objectAtIndex:0];
NSMutableString *string1 = [NSMutableString stringWithString:url];
[string1 deleteCharactersInRange: [string1 rangeOfString: #"href = "]];
[string1 deleteCharactersInRange: [string1 rangeOfString: #";"]];
NSLog(#"Clean URL: %#", string1);
Please, how can I solve this problem? Thank you!
TouchXML returns you an array of NSDictionaries. In order to extract string you need to take value from this NSDictionary:
NSString *url = [[res objectAtIndex:0] objectForKey:#"href"];

Append string with variable

I'm a java guy coming over to Objective-C. In java, to add a variable to a string you'd have to do something along the lines of:
someString = "This string is equal to " + someNumber + ".";
I can't figure out how to do it in Objective-C though. I have an NSMutableString that I'd like to add to the middle of a string. How do I go about doing this?
I've tried:
NSString *someText = #"Lorem ipsum " + someMutableString;
NSString *someText = #"Lorem ipsum " + [someMutableString stringForm];
and a few other things, none of which seem to work. Also interchanged the +s with ,s.
You can use appendString:, but in general, I prefer:
NSString *someText = [NSString stringWithFormat: #"Lorem ipsum %#", someMutableString];
NSString *someString = [NSString stringWithFormat: #"This is string is equal to %d.", someInt];
NSString *someOtherString = [NSString stringWithFormat: #"This is string is equal to %#.", someNSNumber];
or, alternatively:
NSString *someOtherString = [NSString stringWithFormat: #"This is string is equal to %d.", [someNSNumber intValue]];
etc...
These strings are autoreleased, so take care not to lose their value. If necessary, retain or copy them and release them yourself later.
Try this:
NSMutableString * string1 = [[NSMutableString alloc] initWithString:#"this is my string"];
[string1 appendString:#" with more strings attached"];
//release when done
[string1 release];
You need to use stringByAppendingString
NSString* string = [[NSString alloc] initWithString:#"some string"];
string = [string stringByAppendingString:#" Sweet!"];
Don't forget to [string release]; when your done of course.
NSMutableString *string = [[NSMutableString alloc] init];
[string appendFormat:#"more text %#", object ];