String to UTF8 Char Conversion in Objective-C - 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.

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];

Character encoding in NSString

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);

Objective-c integer to string giving random numbers?

I'm busy working on an iPad application and my web service returns pretty simple JSON data. All seems well and I have other methods doing this same conversion without issue however, I have 1 method that returns a random string when doing a integer -> string conversion.
My userdata object below is a NSDictionary created by the SBJSON parser. The value when debugging of [userdata objectForKey:#"UserID"] is 1.
However when I do this
NSString *userId = [NSString stringWithFormat:#"%d", [userdata objectForKey:#"UserID"]];
The value in userId is or appears to be a random number such as 23425234. I also tried the %d in my format but got the same result.
Because it is an object, not an int, you see the address of the object, instead, you can do that:
NSString *userId = [NSString stringWithFormat:#"%#", [userdata objectForKey:#"UserID"]];
^
Try with:
NSString *userId = [NSString stringWithFormat:#"%d", [[userdata objectForKey:#"UserID"] intValue]];
Or:
NSString *userId = [NSString stringWithFormat:#"%#", [userdata objectForKey:#"UserID"]];
object for key is probably NSNumber and not int...

NSObject to NSString Objective-C

Can someone help me to convert an NSObject to NSString?
I'm trying to do something like this -
NSString *address = [NSString stringWithFormat:ivpObj.addressStr];
But I got an warning - Format is not a string literal and no format arguments
Please some one help
How about this:
NSString *address = [NSString stringWithFormat:#"%#", ivpObj.addressStr];
Simpler still:
NSString *address = [ivpObj.addressStr description];
try this, it is worked for me
NSObject* obj= values[i];
NSString *StringObject= [NSString stringWithFormat:#"%#", obj];
it is giving you a warning because you are useing stringWithFormat and you are not passing in a format you just passing in either an NSString or a CString
so chose from on of the other options
[NSString stringWithString: ivpObj.addressStr]
[NSString stringWithCString: ivpObj.addressStr encoding: String_Encoding of you addressStr here]
[NSString stringWithUTF8String:ivpObj.addressStr]
this should remove your warnings, otherwise if you want to format the string using something similar to Eimantas's answer