Reputation: 225
I have code like below :
1.I have below method as action for one button.
(IBAction)enableDisableFB:(id)sender {//have below local variable
NSMutableDictionary *fbLocationLatitude = [[NSMutableDictionary alloc] init];
2.There is a loop in which i set objects in this dictionary like
[fbLocationLatitude setObject:latitude forKey:locID]; //here latitude and locID both are NSString
3.In Another loop in same method, I am trying to retrieve value but it gives (null) in NSLog
NSLog(@"latitude for key %@ = %@",locID, [fbLocationLatitude objectForKey:locID]);
//above statement gives me output -> latitude for key 114943251851302 = (null)
But right after loop I print complete dictionary using
NSLog(@"location dictionary = %@",fbLocationLatitude);
//which gives me below output. Sample given
location dictionary = {
114943251851302 = "40.4414";
109782595706984 = "38.9966";
108867759137051 = "22.47";
110148382341970 = "25.7877";
107611279261754 = "43.1655";
111803735513513 = "27.1828";
109155699106505 = "18.3167";
103734539665100 = "19.09";
This has the value for key I am searching for but same I am unable to get using objectForKey. Please help me to resolve this problem.
for (NSUInteger i =0; i< [fbLocationID count];i++)
{
// NSLog(@"person creation");
NSString *locID = [NSString stringWithString:[fbLocationID objectAtIndex:i]];
NSLog(@"latitude for key %@ = %@",locID, [fbLocationLatitude objectForKey:locID]);
}
NSLog(@"location dictionary = %@",fbLocationLatitude);
This is how code is. I also tried to print class of keys using below code
for (id ik in [fbLocationLatitude allKeys])
{
const char* classname=class_getName([ik class]);
NSLog(@"class of key %s",classname);
}
and I am getting ans as : class of key NSDecimalNumber Though type casting locID to NSDecimalNumber, I don't get exact value of key but only null. Please help to resolve this.
Upvotes: 0
Views: 240
Reputation: 12190
The keys are NSDecimalNumber, but you are starting with an NSString, so you can extract the data like this:
NSDecimalNumber locIDnumber = [NSDecimalNumber decimalNumberWithString: locID];
id latitude = [fbLocationLatitude objectForKey:locIDnumber];
NSLog(@"latitude for key %@ = %@", locID, latitude);
You say you tried a cast, but this doesn't perform any conversion from one object type to another; you need a method like decimalNumberWithString:
Upvotes: 1
Reputation: 38239
check like this:
if([[fbLocationLatitude objectForKey:locID] isKindofClass:[NSNumber class]])
{
NSNumber *num = fbLocationLatitude objectForKey:locID];
}
else if([[fbLocationLatitude objectForKey:locID] isKindofClass:[NSString class]])
{
NSString *strnum = fbLocationLatitude objectForKey:locID];
}
Upvotes: 0