Objective-c how to convert NSURL into NSString? - objective-c

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

Related

Format string that already has format specifier %#

I have a string which already contains a formatter %#.
NSString *str = #"This is an %#";
I need to parse that string and to replace %# with 'example'. If I use
[NSString stringWithFormat:#"%#", str];
I get the following output:
This is an %#
I want output like:
This is an example
NSString *str = #"This is an %#";
str = [str stringByReplacingOccurrencesOfString:#"%#" withString:#"example"];
I would recommand to use the formatted string as "format"
NSString *str = #"This is an %#";
str = [NSString stringWithFormat:str, #"example"];
is working with every type. A better solution than replacing, because you can use unspecified replacings
is very usefull if you use localized.strings with x values you want to add ;)

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.

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

Objective-C string arrays

I have a string array as such:
NSArray *names;
names = [NSArray arrayWithObjects:
#"FirstList",
#"SecondList",
#"ThirdList",
nil];
I'm trying to assign an element of this string array to a string variable as such:
NSString *fileName = names[0]; // "Incompatible types in initialization"
or with casting
NSString *fileName = (NSString)names[0]; // "Conversion to non-scalar type requested"
I'm trying to do this, so I can use the string in a method that takes a string as an argument, such as:
NSString *plistPath = [bundle pathForResource:filetName ofType:#"plist"];
Is there no way to assign an element of a string array to a string variable?
Update from 2014: The code in this post actually would work these days since special syntactic support has been added to the framework and compiler for indexing NSArrays like names[0]. But at the time this question was asked, it gave the error mentioned in this question.
You don't use C array notation to access NSArray objects. Use the -objectAtIndex: method for your first example:
NSString *fileName = [names objectAtIndex:0];
The reason for this is that NSArray is not "part of Objective-C". It's just a class provided by Cocoa much like any that you could write, and doesn't get special syntax privileges.
NSArray is a specialized array class unlike C arrays. To reference its contents you send it an objectAtIndex: message:
NSString *fileName = [names objectAtIndex:0];
If you want to perform an explicit cast, you need to cast to an NSString * pointer, not an NSString:
NSString *fileName = (NSString *)[names objectAtIndex:0];
With the new Objective-C literals is possible to use:
NSString *fileName = names[0];
So your code could look like this:
- (void)test5518658
{
NSArray *names = #[
#"FirstList",
#"SecondList",
#"ThirdList"];
NSString *fileName = names[0];
XCTAssertEqual(#"FirstList", fileName, #"Names doesn't match ");
}
Check Object Subscripting for more information.