object for key based on int number - iphone

int number = 3;
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
#"Apple",[NSNumber numberWithInt:0],
#"Peach",[NSNumber numberWithInt:1],
#"Lemon",[NSNumber numberWithInt:2],
#"Pineapple",[NSNumber numberWithInt:3],
........................,
nil];
NSString *numberToKey = [dict objectForKey:number];
How insert in the string the value of the dict based on int number?

The same way you put the values into the dictionary:
NSString *numberToKey = [dict objectForKey:[NSNumber numberWithInt:number]];

Related

EXC_BAD_ACCESS on NSMutableDictionary

I am beginning with iOS development, I have this code :
First of all I declare the listOfItems NSMutableArray:
#interface SAMasterViewController () {
NSMutableArray *listOfItems;
}
#end
And now, here is the part the code that gives me an "EXC_BAD_ACCESS (code=1, address=0x5fc260000)" error.
The error is given in the last line of the "individual_data" object.
listOfItems = [[NSMutableArray alloc] init];
for(NSDictionary *tweetDict in statuses) {
NSString *text = [tweetDict objectForKey:#"text"];
NSString *screenName = [[tweetDict objectForKey:#"user"] objectForKey:#"screen_name"];
NSString *img_url = [[tweetDict objectForKey:#"user"] objectForKey:#"profile_image_url"];
NSInteger unique_id = [[tweetDict objectForKey:#"id"] intValue];
NSInteger user_id = [[[tweetDict objectForKey:#"user"] objectForKey:#"id"] intValue ];
NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
text, #"text_tweet",
screenName,#"user_name",
img_url, #"img_url",
unique_id, #"unique_id",
user_id, #"user_id", nil];
[listOfItems addObject:individual_data];
}
Thanks in advance.
You can not put NSIntegers or any other non Objective-C class inside of an array or dictionary. You need to wrap them in an NSNumber.
NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
text, #"text_tweet",
screenName,#"user_name",
img_url, #"img_url",
[NSNumber numberWithInteger:unique_id], #"unique_id",
[NSNumber numberWithInteger:user_id], #"user_id", nil];
//Or if you want to use literals
NSMutableDictionary *individual_data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
text, #"text_tweet",
screenName,#"user_name",
img_url, #"img_url",
#(unique_id), #"unique_id",
#(user_id), #"user_id", nil];

Auto-incrementing a key in a NSMutableDictionary

I want to auto-incrementing a key and at to a NSMutableDictionary.
I tried to do it but it wasn't work :
NSMutableDictionary *array = [[NSMutableDictionary alloc] init];
int testAutoIndex = 0;
[array setObject:[NSNumber numberWithInt:testAutoIndex++] forKey:#"index"];
[array release];
Thanks :)
Use this:
NSMutableDictionary *array = [[NSMutableDictionary alloc] init];
int testAutoIndex = 0;
[array setObject:[NSNumber numberWithInt:++testAutoIndex] forKey:#"index"];
[array release];
If you want to create a dictionary with say N entries, then it is possible to do using the code
int N = 100; // or what ever number you want
NSArray *arrayOfObjectsYouWantToPumpIntoDictionary = ....;
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:100];
for(int i=0;i<N;i++){
NSString *key = [NSString stringWithFormat:#"%d"i];
[mutableDictionary setObject:[arrayOfObjectsYouWantToPumpIntoDictionary objectAtIndex:i] forKey:];
}
// Later some where else if you want to retrieve object with key of x (x can be 1, or 2 or what ever value), you can do
objectToBeRetrieved = [mutableDictionary objectForKey:[NSString stringWithFormat:#"%d",x];

Getting strange error in NSDictionary

I am getting strange exception when calling the NSDictionary in the following example:
NSInteger userId = 1;
NSDictionary *postData = [NSDictionary dictionaryWithObjectsAndKeys:
#"3", #"a",
#"0", #"b",
userId, #"c",
nil];
Can someone see what's the problem above?
NSDictionary, as most collections, just accepts real Obj-C objects (id type), but you are passing a NSInteger which is a normal C typedef.
Try with NSNumber userId = [NSNumber numberWithInt:1];

Getting crashed when NSdictionary or NSString values into UILABEL

-(void) httpDataDidFinishLoadingWithData:(NSData *)theData
{
m_activityLoaded=NO;
temp=[[NSString alloc] initWithData:[dataLoader httpData]
encoding:NSUTF8StringEncoding];
NSLog(#"TEMP IS TEMP %#", temp);
NSDictionary *dict = [[NSDictionary alloc]init];
dict = [[temp JSONValue] objectForKey:#"location"];
NSDictionary *dict1 = [[NSDictionary alloc]init];
dict1 = [[temp JSONValue] objectForKey:#"wind"];
NSDictionary *dict2 = [[NSDictionary alloc]init];
dict2 = [dict1 objectForKey:#"direction"];
NSDictionary *dict3 = [[NSDictionary alloc]init];
dict3 = [[temp JSONValue] objectForKey:#"atmosphere"];
NSDictionary *dict4 = [[NSDictionary alloc]init];
dict4 = [[temp JSONValue] objectForKey:#"condition"];
NSDictionary *dict5 = [[NSDictionary alloc]init];
dict5 = [dict4 objectForKey:#"text"];
NSDictionary *dict6 = [[NSDictionary alloc]init];
dict6 = [dict4 objectForKey:#"code"];
NSDictionary *dict7 = [[NSDictionary alloc]init];
dict7 = [dict4 objectForKey:#"temperature"];
temperatureLabel.text = [dict4 objectForKey:#"temperature"];
}
Crash occurs at temperatureLabel.text = [dict4 objectForKey:#"temperature"];
I dont know man, Data is exactly printed in the console, but crashing at UILABEL(temperatureLabel). Help me out, thanks in advance
if you look at the error you are getting it is telling you that the object return for the key temperature is not a NSString or NSDictionary but a NSNumber.
Give this a try:
-(void) httpDataDidFinishLoadingWithData:(NSData *)theData {
m_activityLoaded=NO;
temp=[[NSString alloc] initWithData:[dataLoader httpData]
encoding:NSUTF8StringEncoding];
NSLog(#"TEMP IS TEMP %#", temp);
NSDictionary *dict = [[temp JSONValue] objectForKey:#"location"];
NSDictionary *dict1 = [[temp JSONValue] objectForKey:#"wind"];
NSDictionary *dict2 = [dict1 objectForKey:#"direction"];
NSDictionary *dict3 = [[temp JSONValue] objectForKey:#"atmosphere"];
NSDictionary *dict4 = [[temp JSONValue] objectForKey:#"condition"];
NSDictionary *dict5 = [dict4 objectForKey:#"text"];
NSDictionary *dict6 = [dict4 objectForKey:#"code"];
NSNumber *temperature = [dict4 objectForKey:#"temperature"];
temperatureLabel.text = [NSString stringWithFormat:#"%#", temperature];
}
You might want to look at NSNumberFormatter for formatter the temperature with something like: °F or °C.
Are you sure that your object is of NSString class?
Try putting in something like:
if([[dict4 objectForKey:#"temperature"] isKindOfClass:[NSString class]])
NSLog(#"lalala");
If it does not get logged to the console it means that your object is not an NSString and you could try something like:
temperatureLabel.text = [NSString stringWithFormat:#"%#", [dict4 objectForKey:#"temperature"]];
You should change the %# according to the kind of object you have stored in your dictionnary.
Your dictionary contains an NSNumber instance and you are assigning that to a property of type NSString. The crash message is your tip off there. Use stringValue or some other way to get the number's data into string form.

Creating an NSDictionary

In the following code, the first log statement shows a decimal as expected, but the second logs NULL. What am I doing wrong?
NSDictionary *entry = [[NSDictionary alloc] initWithObjectsAndKeys:
#"x", [NSNumber numberWithDouble:acceleration.x],
#"y", [NSNumber numberWithDouble:acceleration.y],
#"z", [NSNumber numberWithDouble:acceleration.z],
#"date", [NSDate date],
nil];
NSLog([NSString stringWithFormat:#"%#", [NSNumber numberWithDouble:acceleration.x]]);
NSLog([NSString stringWithFormat:#"%#", [entry objectForKey:#"x"]]);
You are exchanging the order in which you insert objects and key: you need to insert first the object, then the key as shown in the following example.
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:#"value1", #"key1", #"value2", #"key2", nil];
new Objective-c supports this new syntax for static initialisation.
#{key:value}
For example:
NSDictionary* dict = #{#"x":#(acceleration.x), #"y":#(acceleration.y), #"z":#(acceleration.z), #"date":[NSDate date]};
NSDictionary Syntax:
NSDictionary *dictionaryName = [NSDictionary dictionaryWithObjectsAndKeys:#"value1",#"key1",#value2",#"key2", nil];
Example:
NSDictionary *importantCapitals = [NSDictionary dictionaryWithObjectsAndKeys:
#"NewDelhi",#"India",#"Tokyo",#"Japan",#"London",#"UnitedKingdom", nil];
NSLog(#"%#", importantCapitals);
Output looking like,
{India = NewDelhi; Japan = Tokyo; UnitedKingdom = London; }