-1

I have the following method:

-(QFFriend*)getFriendById:(NSString*)fid
{
    TFLog(@"NList %@", self.nFlist);
    TFLog(@":%@:%@:", @"3", fid);
    TFLog(@"%@", [self.nFlist valueForKey:@"3"]);
//    TFLog(@"%@", [self.nFlist valueForKey:fid]);
}

And when I run it WITH the comment, I get this result: (and crash because I left out the return)

2013-04-12 07:39:49:972 QF[42881:2311] NList {
    2 = "<QFFriend: 0x1dd61970>";
    3 = "<QFFriend: 0x1dd76570>";
    4 = "<QFFriend: 0x1dd84a70>";
    7 = "<QFFriend: 0x1dd70760>";
}
2013-04-12 07:39:49:974 QF[42881:2311] :3:3:
2013-04-12 07:39:49:976 QF[42881:2311] <QFFriend: 0x1dd76570>

However, If I uncomment that line I get another crash, can someone explain what could be wrong?

2013-04-12 07:43:28:736 QuizFuzz[42912:2311] NList {
    2 = "<QFFriend: 0x1c5888c0>";
    3 = "<QFFriend: 0x1c5a5c30>";
    4 = "<QFFriend: 0x1c5acaf0>";
    7 = "<QFFriend: 0x1c5badf0>";
}
2013-04-12 07:43:28:738 QF[42912:2311] :3:3:
2013-04-12 07:43:28:740 QF[42912:2311] <QFFriend: 0x1c5a5c30>
2013-04-12 07:43:28.741 QF[42912:907] -[__NSCFNumber length]: unrecognized selector sent to instance 0x1d8521b0
2013-04-12 07:43:28:754 QF[42912:2311] ERROR: -[__NSCFNumber length]: unrecognized selector sent to instance 0x1d8521b0
2013-04-12 07:43:28.756 QF[42912:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x1d8521b0'

How can the two last calls not be equivalent? I send in a string in both cases!

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
netdigger
  • 3,659
  • 3
  • 26
  • 49

2 Answers2

3

the error is:

-[__NSCFNumber length]: unrecognized selector sent to instance

which means fid is a NSNumber not NSString

also, you should use objectForKey: rather valueForKey: to get object from dictionary

so try this

[self.nFlist valueForKey:[fid stringValue]]
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
0

I'm not sure why you are getting two different crashes when using what would seem to be very similar code. But if you want to fix the crashes, I believe the call you are looking for is:

[self.nFlist objectForKey:@"3"];

NSDictionary Class Reference

/* update */ Actually, after reading your crash logs more and looking at the TFLogs. I would take a guess that the fid object that is getting passed in is an NSNumber, not an NSString like you expect. To confirm this, you could log out: TFLog(@"fid.class: %@", fid.class); This logs the class, not the description, so you can see what you have. NSNumber with the value 3 and NSString with the value @"3" would both log out as 3.

DonnaLea
  • 8,643
  • 4
  • 32
  • 32