开发者

How to Resolve NSMutableArray may not respond to ObjectForKey

How to resolve the following type of warning?

  • NSMutableArray may not respond to objectForKey.
  • UISwitch may not respond to setAlternateColors:

My code for switch is:

[mySwitch setAlternateColors:YES];

and For Array is:

NSMutableArray *tmp1 = [sortedIndexedArray  objectAtIndex:indexPath.section];
NSMutableArray *tmpDict2 = [tmp1 objectAtIndex:indexPath.row];
NSString *companyNameString =  [tmpDict2 objectForKey:@"firstname"];
NSString *flName = [tmpDict2 objectForKey:@"title"];
[searchCustomCell setDataForEvent1:com开发者_运维问答panyNameString venue:flName];


NSMutableArray doesn't respond to -objectForKey:. Your variable name "tmpDict2" is misleading; an array is not a dictionary.

Array store an ordered list of objects; dictionaries store an unordered mapping of keys to values (objects). Thus -objectForKey: only applies to a NSDictionary.

Perhaps you want this? I can't really be sure based on what you've provided...

NSDictionary *tmp1 = [sortedIndexedArray objectAtIndex:indexPath.section];
NSDictionary *tmpDict2 = [tmp1 objectAtIndex:indexPath.row];

As for your switch, perhaps you made a custom subclass of UISwitch which responds to -setAlternateColors:? In that case, try using [(MyUISwitchSubclass *)mySwitch setAlternateColors:YES] or just make the variable mySwitch have type MyUISwitchSubclass *. On the other hand, if this is a private method, you should avoid using it in your app.

All in all, I can't really tell exactly what you're trying to do unless you provide some more information and context.


objectForKey is an instance method of NSDictionary. You are using it on NSMutableArray.


Try by this code...

NSMutableArray *tmp1 = [sortedIndexedArray  objectAtIndex:indexPath.section];

NSDictionary *tmpDict2 = [tmp1 objectAtIndex:indexPath.row];

NSString *companyNameString =  [tmpDict2 objectForKey:@"firstname"];

NSString *flName = [tmpDict2 objectForKey:@"title"];

[searchCustomCell setDataForEvent1:companyNameString venue:flName];

Thanks...


The compiler is complaining for UISwitch because that object doesn't contain a setAlternateColors message. setAlternateColors is actually part of a private API, so if you use this message, your app will be rejected by Apple.

As the other answers have pointed out objectForKey is a message on NSDictionary, not NSMutableArray.


NSMutableArray doesn't have objectForKey method. Use NSMutableDictionary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜