variable is not a CFStringRef - iphone

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

Related

how to add variable into an url

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.

How to selectively trim an NSMutableString?

I would like to know how to selectively trim an NSMutableString. For example, if my string is "MobileSafari_2011-09-10-155814_Jareds-iPhone.plist", how would I programatically trim off everything except the word "MobileSafari"?
Note : Given the term programatically above, I expect the solution to work even if the word "MobileSafari" is changed to "Youtube" for example, or the word "Jared's-iPhone" is changed to "Angela's-iPhone".
Any help is very much appreciated!
Given that you always need to extract the character upto the first underscore; use the following method;
NSArray *stringParts = [yourString componentsSeparatedByString:#"_"];
The first object in the array would be the extracted part you need I would think.
TESTED CODE: 100% WORKS
NSString *inputString=#"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";
NSArray *array= [inputString componentsSeparatedByString:#"_"];
if ([array count]>0) {
NSString *resultedString=[array objectAtIndex:0];
NSLog(#" resultedString IS - %#",resultedString);
}
OUTPUT:
resultedString IS - MobileSafari
If you know the format of the string is always like that, it can be easy.
Just use NSString's componentsSeparatedByString: documented here.
In your case you could do this:
NSString *source = #"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";
NSArray *seperatedSubStrings = [source componentsSeparatedByString:#"_"];
NSString *result = [seperatedSubStrings objectAtIndex:0];
#"MobileSafari" would be at index 0, #"2011-09-10-155814" at index 1, and #"Jareds-iPhone.plist" and at index 2.
Try this :
NSString *strComplete = #"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";
NSArray *arr = [strComplete componentsSeparatedByString:#"_"];
NSString *str1 = [arr objectAtIndex:0];
NSString *str2 = [arr objectAtIndex:1];
NSString *str3 = [arr objectAtIndex:2];
str1 is the required string.
Even if you change MobileSafari to youtube it will work.
So you'll need an NSString variable that'll hold the beginning of the string you want to truncate. After that one way could be to change the string and the variable string values at the simultanously. Say, teh Variable string was "Youtube" not it is changed to "MobileSafari" then the mutable string string should change from "MobileSafari_....." to "YouTube_......". And then you can get the variable strings length and used the following code to truncate the the mutable string.
NSString *beginningOfTheStr;
.....
theMutableStr=[theMutableStr substringToIndex:[beginningOfTheStrlength-1]];
See if tis works for you.

Reusing NSMutableString correctly on the iPhone

Using objective-c on the iPhone, what is wrong with this code?
Is it leaking memory? Why?
How would I do this correctly?
NSMutableString *result = [NSMutableString stringWithFormat:#"the value is %d", i];
... then later in my code... I might need to change this to:
result = [NSMutableString stringWithFormat:#"the value is now %d", i];
I need to use stringWithFormat a 2nd time... but isn't that creating a NEW string and not correctly freeing the old one?
No, it doesn't leak memory because stringWithFormat: returns an autoreleased object.
You could use the instance method "setString" for your already existing NSMutableString, like this:
[ result setString:[NSString stringWithFormat:#"the value is now %d", i] ];
If you really want to reuse the string, you can use something like
[result setString:#""];
[result appendFormat:#"the value is now %d", i];
However, unless you notice a performance/memory problem, just use
NSString *result = [NSString stringWithFormat:#"the value is %d", i];
/* ... */
result = [NSString stringWithFormat:#"the value is now %d", i];
It's generally easier to work with immutable objects because they can't change under your feet.
What you have seems to me to be the natural way to replace a mutable string with new content, unless you have other references to the same mutable string elsewhere.
If you don't have other references to it and you are reusing the string only to improve performance/memory footprint, that sounds like premature optimisation.
By the way, you do not own a string you obtain via stringWithFormat: so you do not need to (Indeed must not) release it.

Using NSString stringWithFormat: with a string from NSDictionary

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 concatenate two strings on iPhone?

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