Hi i'm returning a json string as follows:
{data,[{"id":1,"text":"blabla"},{"id":2,"text":"blabla2"}]}
When i try to print with NSLog like so:
NSLog(#"id: %#",[temp objectForKey:#"id"]);
i get large numbers, not the id.
If i enclose the the id's in quotes, things are fine.
Is there a way to decode the string when integers don't have quotes?
thx!
Looks like you have to use integerValue method to convert object to integer.
NSLog(#"id: %d",[[temp objectForKey:#"id"]integerValue]);
thxs for everyone's help.
Your formatting is a bit wonky. Also -objectForKey: takes an NSString for an argument, so be sure to prepend the quotes with # to signify an NSString constant.
NSLog(#"id: %#", [temp objectForKey:#"id"]);
The problem is in your string format. Instead of
NSLog(#"id: %#),[temp objectForKey:#"id"];
Try using this:
NSLog(#"id: %d),[temp objectForKey:#"id"];
%# is only for strings, and in your case, when the ID is not quoted, it's an integer and you should use %d instead.
Related
OK here is nsmutablestring
data = [NSMutableString stringWithFormat:#"&cb_games%5B%5D="];
Now when ever I try to print or use this string I get big number instead of %5B and %5D not sure why this is happeing any help would be apritiated
thanks
The reason you get unexpected output is that '%' is used as conversion specifier in printf and obviously NSLog and NSString formattings. You need to escape '%' if you don't want it to be interpreted as a conversion specifier. You can escape '%' by preceding it with another '%' like '%%'.
Your string should look like,
#"&cb_games%%5B%%5D="
And the #August Lilleaas's answer is also noteworthy.
Try this:
NSString * data = [NSMutableString stringWithFormat:#"&cb_games%%5B%%5D="];
NSLog(#"%#",data);
stringWithFormat is basically printf, and it attempts to replace your percentages with values that you haven't provided, which is why wierd stuff happens.
[NSMutableString stringWithFormat:#"Hello: %d", 123];
// #"Hello: 123"
If you want a mutable string from a string, try this:
[NSMutableString stringWithString:#"Abc %2 %3"];
// #"Abc %2 %3"
The % is used for string formatting and stuff. I imagine you need to escape the character or something, possibly with a slash.
Did you mean to write?
data = [NSMutableString stringWithString#"&cb_games%5B%5D="]
In my iPhone app, I'm reading a csv file. The relevant line is this:
NSString *countrycode = [[[NSString alloc] initWithFormat:#"%#", [arr objectAtIndex:2]]
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
This returns "CN" (which stands for China).
When I do this:
NSLog(#"Manual: %#, country code: %#",#"CN",countryCode);
I get:
Manual: CN, country code: "CN"
One has quotes and the other does not. I don't know why this is.
The reason this is tripping me up is the following:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"countrycode == %# ", #"CN"];
This works fine, and returns China from Core Data.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"countrycode == %# ", countrycode];
This fails to return anything. I am assuming this is because it has quotes around it, or something, although perhaps I am incorrect.
What am I doing wrong here?
Actually the correct way to format a predicate to exclude quotes is the to use %K versus %#. See Predicate Format String Syntax.
Your countryCode variable must have quotes inside of it when it's read back. The first time you assign the literal #"CN" the quotes are removed as they specify that your variable is an NSString. They aren't really inside of the literal string. If you wanted strings inside of the first CN, you'd need to explicitly specify the quotation marks, e.g. #"""CN"""
However, if you want to get rid of any quotations in the second string, you could always do this to the string prior to putting it into your predicate:
[countryCode stringByReplacingOccurrencesOfString:#"""" withString:#""];
In Xcode, if I have an NSString containing a number, ie #"12345", how do I split it into an array representing component parts, ie "1", "2", "3", "4", "5"... There is a componentsSeparatedByString on the NSString object, but in this case there is no delimiter...
There is a ready member function of NSString for doing that:
NSString* foo = #"safgafsfhsdhdfs/gfdgdsgsdg/gdfsgsdgsd";
NSArray* stringComponents = [foo componentsSeparatedByString:#"/"];
It may seem like characterAtIndex: would do the trick, but that returns a unichar, which isn't an NSObject-derived data type and so can't be put into an array directly. You'd need to construct a new string with each unichar.
A simpler solution is to use substringWithRange: with 1-character ranges. Run your string through a simple for (int i=0;i<[myString length];i++) loop to add each 1-character range to an NSMutableArray.
A NSString already is an array of it’s components, if by components you mean single characters. Use [string length] to get the length of the string and [string characterAtIndex:] to get the characters.
If you really need an array of string objects with only one character you will have to create that array yourself. Loop over the characters in the string with a for loop, create a new string with a single character using [NSString stringWithFormat:] and add that to your array. But this usually is not necessary.
In your case, since you have no delimiter, you have to get separate chars by
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange
or this one
- (unichar)characterAtIndex:(NSUInteger) index inside a loop.
That the only way I see, at the moment.
Don't know if this works for what you want to do but:
const char *foo = [myString UTF8String]
char third_character = foo[2];
Make sure to read the docs on UTF8String
I have a long NSString in which I m trying to replace special characters. Part of my string looks like this:
"veau (c\u00f4telette)","veau (filet)","agneau (gigot)","agneau (c\u00f4telette)","b**\u0153**uf (hach\u00e9)","porc (hach\u00e9)"
I would like to replace all the \u0153 with "oe". I've tried:
[response stringByReplacingOccurrencesOfString:#"\u0153" withString:#"oe"];
but it doesn't work.... I don't understand why!
The backslash is an escape character, so if you want to specify the actual backslash character in a string literal, you need to use two backslashes.
NSString *new = [old stringByReplacingOccurrencesOfString: #"\\u0153" withString:#"oe"];
NSString is immutable, so the function generates a new string that you have to store:
NSString *new = [old stringByReplacingOccurrencesOfString:#"\u0153" withString:#"oe"];
I am having a lengthy string which contains alphabets and a special character like "|". i need to split this strings based on the "|" delimiter and store the individual string in to an array. Is there any string function which helps us to do the same.?
Thanks,
Shibin.
Sounds like you need componentsSeparatedByString:
NSString *string = #"hello|how|are|you";
NSArray *array = [string componentsSeparatedByString:#"|"];
NSLog(#"array: %#", array);
Output:
array: (
hello,
how,
are,
you
)