This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between NSString* mystring and NSString *mystring
Should I put the * for a pointer with the variable or with the class?
i.e.
NSString *string or NSString* string ?
The point for me isn't that both work anyway, but to understand which is correct syntactically and why.
Correct both ways, means the same as well. Just be careful of doing stuff like this:
NSString *a,b;
Should be
NSString *a,*b;
It's all the same but the correct way of writing it is :
NSString *string1;
If you want to declare multiple values on a same line, you will have to write :
NSString *string1, *string2;
and not
NSString *string1, string2;
nor
NSString* string1, string2;
Once you know that, you can do the way you like, and if you miss, the compiler will alert you.
Related
I am getting userid through parsing a link.Again i have to parse it with userid to get the access.
What i am doing
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/fb_redirect_mobile/?accessToken=4546"];
This gives me the userid,now i want to use that userid to parse it again such as:
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/userid/bookmarks"];
In other languages I have seen they are just using:
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/"+userid+"/bookmarks"];
The userid variable takes user id.how i can do this in iPhone.I know my question is lengthy but i tried to make it clear what i want.please help..please also tell me how to store a parsing id into string,such as userid i am going to get after parsing the url./now how i can save it in form of string.
NSString *userId = #"123456";
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/%#/bookmarks", userId];
initWithFormat:/stringWithFormat: follow the general format convention set by printf/scanf
The way you concatenated your userid is not valid syntax in Obj-C
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/"+userid+"/bookmarks"];
What you'd probably want to do is use a format specifier for an Obj-C object (in your case NSString), and use that within your URL (assuming userid is an NSString, which it probably is. If it's a C based string, use %s as your format specifier instead).
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/%#/bookmarks", userid];
Refer to these on how to use stringWithFormat: on an NSString:
Formatting String Objects
String Format Specifiers
you can give like this,
NSString *str=[NSString stringWithFormat:#"http://abc.com/user/%#/bookmarks",userid];
Instead of
NSString *str=[[NSString alloc]initWithFormat:#"http://abc.com/user/"+userid+"/bookmarks"];
You would use
NSString *str=[[NSString alloc] initWithFormat:#"http://abc.com/user/%#/bookmarks",userid];
Well you almost got it:
NSString *usrid = #"4546";
NSString *str=[NSString stringWithFormat:#"http://abc.com/fb_redirect_mobile/?accessToken=%#", userid];
You can use the stringWithFormat: method of NSString for this.
I have this:
partenaire_lat = 48.8160525;
partenaire_lng = 2.3257800;
And obtain a NSString like this:
NSString *endPoint =[NSString stringWithFormat:#"%#,%#", partenaire_lat, partenaire_lng];
and after using this NSString in some context I get this stupid error:
Variable is not a CFString.
But if I create the NSString like this:
endPoint = #"48.8160525,2.3257800" it then works perfect!
For this error Variable is not a CFString I tried the following:
NSString *endPoint1 =[NSString stringWithFormat:#"%#,%#", partenaire_lat, partenaire_lng];
CFStringRef endPoint =(CFStringRef)endPoint1;
and tried to use endPoint but not working neither this way.Anyone any miraculous idea?Thx
EDIT:partenaire_lat and partenaire_lng are both NSString!!
You have
partenaire_lat = 48.8160525;
partenaire_lng = 2.3257800;
You keep saying that the two variables are NSStrings but you aren't assigning NSStrings to them. You need to assign NSString objects to NSString variables - they aren't created for you automatically.
So the answers which are telling you to use formatted strings are correct. You really should be doing it like this:
partenaire_lat = [[NSString stringWithFormat:#"%f", 48.8160525] retain];
partenaire_lng = [[NSString stringWithFormat:#"%f", 2.3257800] retain];
what are lat and lng? i'm assuming float or double..so you should use [NSString stringWithFormat:#"%f,%f", lat, lng]; (or however you want the floats to be formatted)
You code has several potential problems:
%# format specifier expects object parameter, while it looks like you pass plain float (I may be wrong here as there's not enough context to be sure). Change format to %f to fix your problem if that's really the case:
NSString *endPoint1 =[NSString stringWithFormat:#"%f,%f", partenaire_lat, partenaire_lng];
Your endPoint1 string is autoreleased and may become invalid outside of current scope if you don't retain it. So if you try to use your variable in another method you probably should retain it.
All you need to do
NSString *latStr=[[NSNumber numberWithFloat:partenaire_lat] stringValue];
NSString *lngStr=[[NSNumber numberWithFloat:partenaire_lng] stringValue];
and do whatever you want to do with these two string :)
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Storing and retrieving unsigned long long value to/from NSString
I am trying to create a unsigned long long from a string so I can use the value in elsewhere but not having much luck doing so...Here is what I am using to attempt this
-(void)unsignedLongValue{
NSString *theString = [NSString stringWithFormat:#"%llu", [NSNumber
unsignedLongLongValue]];
theString = [[_message objectForKey:#"user"] objectForKey:#"id"];
NSLog(#"%llu");
}
If you have any suggestions or know of any good articles I would be very much appreciative if you could inform me! thanks!
You probably want to do this,
NSString * theString = [[[_message objectForKey:#"user"] objectForKey:#"id"];
NSNumberFormatter * numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
NSNumber * number = [numberFormatter numberFromString:theString];
NSLog(#"%llu", [number unsignedLongLongValue]);
The line
NSLog(#"%llu");
does nothing. You need to tell the log what to print, not just what type it is. Use this instead:
NSLog(#"%llu",numberToPrint);
Also, you re-write theString immediately after defining it, so the initial value from
NSString *theString = [NSString stringWithFormat:#"%llu", [NSNumber unsignedLongLongValue]];
is never used.
I'm getting an NSString from a dictionary that needs to have a variable integer, something like:
"You have %i objects."
How do I put the calculated integer value into the string? I would like to do something like
NSString *string = [NSString stringWithFormat:[dictionary objectForKey:#"string"]
But I don't know how to pass in an argument to stringWithFormat when the %i is tucked away in the dictionary.
Note: I can work around this by using stringByReplaceOccurenceOfString, but I'd like to know if it's possible to do it in the above way.
You can pass a comma separated list of arguments to methods like stringWithFormat:
NSString *string = [NSString stringWithFormat:
[dictionary objectForKey:#"string"] , 7 , 8 , 9];
Only the 7 would be used in your sample format string.
You can access it using this
NSString *string = [NSString stringWithFormat:[dictionary objectForKey:#"string"],YOURINTNUMBER];
It will work
How to connect string "Hello" and string "World" to "HelloWorld"? Looks like "+" doesn't work.
NSString *string = [NSString stringWithFormat:#"%#%#", #"Hello", #"World"];
NSLog(#"%#", string);
That should do the trick, although I am sure there is a better way to do this, just out of memory. I also must say this is untested so forgive me. Best thing is to find the stringWithFormat documentation for NSString.
How about:
NSString *hello = #"Hello";
NSString *world = #"World";
NSString *helloWorld = [hello stringByAppendingString:world];
If you have two literal strings, you can simply code:
NSString * myString = #"Hello" #"World";
This is a useful technique to break up long literal strings within your code.
However, this will not work with string variables, where you'd want to use stringWithFormat: or stringByAppendingString:, as mentioned in the other responses.
there's always NSMutableString..
NSMutableString *myString = [NSMutableString stringWithString:#"Hello"];
[myString appendString: #"World"];
Note:
NSMutableString *myString = #"Hello"; // won't work, literal strings aren't mutable
t3.text=[t1.text stringByAppendingString:t2.text];
Bill, I like yout simple solution and I'd like to note that you can also eliminate the space between the two NSStrings:
NSString * myString = #"Hello"#"World";