Use existing string as valueForKey value - ios

I am trying to use a string ("levelNumberString") as a valueForKey in an NSDictionary. When I use a string like #"1", it all works perfectly, but when I decide to swap out that kind of string for levelNumberString, it gives me a SIGABRT error. The integer levelInt is an integer that decides what level the app is at. When the view is loaded, it is worth 1, and its value is taken up by the "levelNumberString" string.
This is my code (it's in viewDidLoad, if that helps at all):
NSString *levelNumberString = [NSString stringWithFormat:#"i%", levelInt];
NSDictionary *packDict = [NSDictionary dictionaryWithDictionary:[rootDict valueForKey:levelNumberString]];
The above code is what is giving me the error.
Thanks in advance!

The problem is over here #"i%" it should be #"%i"
NSString *levelNumberString = [NSString stringWithFormat:#"%i", levelInt];
NSDictionary *packDict = [NSDictionary dictionaryWithDictionary:[rootDict valueForKey:levelNumberString]];

Related

Convert NSNumber to long

I am trying to convert a NSNumber to long but I get this error:
[__NSSingleObjectArrayI intValue]: unrecognized selector sent to
instance
Here is my code:
NSNumber *dbversion = [settings valueForKey:#"Version"];
long dbver = [dbversion longValue];
What am I doing wrong here?
*settings is a NSArray and "Version" is the key for a long value.
You are caught in the Key-Value Coding trap.
In some cases the result of valueForKey is an array which the error message clearly states.
Don't Never use valueForKey(unless you know what KVC does and you need KVC), use key subscription.
And as settings is an array you might get the first item
NSNumber *dbversion = settings[0][#"Version"];
and int is not long
long dbver = [dbversion longValue];
However on a 64-bit machine I recommend to use NSInteger
NSInteger dbver = dbversion.integerValue;
//I think you are storing string from dictionary "settings" to NSNumber so it's showing error like you mention in question.
Please Try with solution. May this help you.
NSNumber *dbversion = [NSNumber numberWithLong:[[settings valueForKey:#"Version"] longLongValue]];
int dbver = [dbversion longValue];

How to get the value for key #int from NSDictionary?

I am Creating a NSDictionary and adding a key value pair as below
NSDictionary* dictionary = #{ #0: #"I am the value" };
and retrieving the value as below
NSString* value = [dictionary valueForKey:#0];
Application crashed for doing this, I don't understand the reason, I am giving the same data type and value.
What is the datatype of #0, I guess it is NSNumber, If not correct me.
Yes, it's NSNumber type however the problem is somewhere else.
You should call objectForKey instead of valueForKey which is mainly used for KVO.
NSString *value = [dictionary objectForKey:#0];
or better:
NSString *value = dictionary[#0];
valueForKey: is not the proper method for what you are trying to achieve.
you should use:
NSString* value = [dictionary objectForKey:#0];
ValueForKey is for key value coding and expect a NSString as parameters

Separate a string into different Array

I have a string coming from server , i want to show some part of string in a view and rest part of string in other view .
Thanks for help.
i want to get the last word of last line of first view. i have searched ,but nothing seems to help me. If any one can suggest me something i would be glad to him/her.
NSString *fromIndex = [_categoryWiseNews.article substringWithRange:NSMakeRange(0, lastWordOfFirstView)];
i have tried with substringWithRange but with this you have to know the lastWordOfFirstView index number for breaking the string into two parts, for which i have no idea.
I really don't know what it is that you're asking lastWordOfFirstView might make sense to you because it is relevant to the thing you are working on. But to us it means nothing.
You can separate a string into words in an array doing this...
NSString *string = #"Hello world, this is a string";
NSArray *array = [string componentsSeparatedByString:#" "];
// array is now... [#"Hello", #"world,", #"this", #"is", #"a", #"string"];
Then you can get the last word from it...
NSString *lastWord = [array lastObject];
Use this to get the last word from a sentence:
NSString *lastWord = [[fullSentenceString componentsSeparatedByString:#" "] lastObject];

Conversion issue from NSNumber to NSString

I'm parsing data from a JSON webservice and adding it to the database so when I insert an int, I have to convert it to NSNumber in this way it's working fine: 24521478
NSString *telephone = [NSString stringWithFormat:#"%#",[user objectForKey:#"telephone"]];
int telephoneInt = [telephone intValue];
NSNumber *telephoneNumber = [NSNumber numberWithInt:telephoneInt];
patient.telephone = telephoneNumber;
but when I want to display it and convert the NSNumber to NSString I'm getting wrong numbers: -30197
NSString *telephoneString = [NSString stringWithFormat:#"%d", [user.telephone intValue], nil];
labelTelephone.text =telephoneString ;
Can someone explain this?
NSNumber comes with dedicated methods for each data type.If you want to convert NSNumber to NSString use:
NSString *telephoneString = [user.telephone stringValue];
The issue may coming because of the data type you used for the variable patient.telephone
If you have control over the web service, then you should return the telephone number as string in the JSON
Reasons
if the telephone number begin with zero then the NSNumber will remove that zero as it has no value (ex: 00123456789 will be 123456789 which will wrong data)
You will not be able to display the telephone number is a user friendly way by adding "+" and "-" (ex: +123-456-789)
You really should make the json return the number as string if you have a control over that

Output to UITextField

I have a NSDictionary with some parameters i want to display in a UITextField.
but
firstname.text = [userdata objectForKey:#"firstname"];
throws an exeption.
If i use NSLog on [userdata objectForKey:#"firstname"]; it shows the right value. What seems to be the problem here?
Just use the following code:
firstname.text = [NSString stringWithFormat:#"%#",
  [userdata objectForKey:#"firstname"]];
go on .. :)
When you NSLog something it uses [object description] to convert the object to a string for output. If the object stored in your dictionary is not an NSString and you try to assign to firstname.text you will get an exception.
Another problem you may be facing is your firstname object may not be pointing at what you think it is. This may occur, for example, if you are not using ARC and you forgot to retain it or you already released it.
The exact exception you are seeing will determine which of these you are encountering.
Tried to do something like
[NSString stringWithFormat:#"%#",[userdata objectForKey:#"firstname"]];
firstname.text = nameString;
?

Resources