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
Related
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
Let's say I have string like this:
NSString *myString = [NSString stringWithFormat:#"%#%#%#%#%#",variable1,variable2,variable3,variable4,variable5];
and if variable2 is nil I don't want that in my string, like this:
NSString *myString = [NSString stringWithFormat:#"%#%#%#%#",variable1,variable3,variable4,variable5];
Question
Is there any way to do this without having a lot of if-statements?
Don't use [NSString stringWithFormat:], and instead create an NSMutableString and append strings as necessary:
NSMutableString *s = [[NSMutableString alloc] init];
if (variable1)
[s appendString:variable1];
if (variable2)
[s appendString:variable2];
if (variable3)
[s appendString:variable3];
if (variable4)
[s appendString:variable4];
if (variable5)
[s appendString:variable5];
(sorry I missed your point about "not having lots of if statements", however I don't think it can be avoided).
The easiest way is to use the compacted ternary operator to replace a null with an empty string. Using a slightly abridged version of your example:
NSString *myString = [NSString stringWithFormat:#"%#%#",variable1 ?: #"",variable2 ?: #""];
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 ;)
Is there any simple way how to initialize String in Objective-C with int such as in Java:
String myStr = 42 + "";
or I have to do
[NSString stringWithFormat:#"%d", 42];
everytime?
You could also use the NSNumber class for that:
NSNumber *number = [[NSNumber alloc] initWithInteger: val];
NSString *string = [number stringValue];
Perhaps not shorter, but it could be eventually faster.
Also you could create as said a helper method, than you wouldn't have to use more code than with the stringWithFormat: method.
Yes you have to do
[NSString stringWithFormat:#"%d", 42];
for Integer to string conversion.
Using a constant, like 42 in your example, you can write
NSString *myString = #"42";
Using a variable or expression, you can write
[NSString stringWithFormat:#"%d", myValue];
I currently have this piece of code:
NSString *strURL = [NSString stringWithFormat:#"http://www.sample.com /phpFile.php?firstname=%#",txtfirstName.text];
NSString *strURL2 = [NSString stringWithFormat:#"http://www.sample.com/phpFile.php?lastname=%#",txtlastName.text];
But the problem is that this creates two different rows and I want the firstname, last name, and everything else on one row.
So would it be possible to do something like:
NSString *strURL = [NSString stringWithFormat:#"http://www.sample.com /phpfile.php?firstname=%#",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];
or do I need to do something more complicated?
Thanks in advance!
NSString *strURL = [NSString stringWithFormat:#"http://www.sample.com/phpfile.php?firstname=%#&txtlastName=%#&txtheight=%#&txtweight=%#",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];
use it and like this
NSString *strURL = [#"http://www.sample.com/phpfile.php" appendStringWithFormat:#"?firstname=%#&txtlastName=%#&txtheight=%#&txtweight=%#",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];
Try this code:
NSString *subStringURL = [NSString stringWithFormat:#"firstname=%#&txtlastName=%#&txtheight=%#&txtweight=%#",txtfirstName.text,txtlastName.text,txtheight.text,txtweight.text];
NSString *strURL = [NSString stringWithFormat:#"http://www.sample.com/phpfile.php?%#",subStringURL];
Refer to this link if you need more help: stringWithFormat: method of NSString Class
Hope this helps you.