尝试检索字符串值时发送到实例的无法识别的选择器

Luc*_*uca 1 objective-c nsmutablearray nsarray ios

我有一个NSMutablearray包含NSArrays(每个数组包含int和String值).当我尝试从第一个数组中检索并显示数据时:对于int值,这是正常的,它已正确显示.

    NSLog(@"%i",[[[lesQuestions objectAtIndex:0] objectAtIndex:0] intValue]);
Run Code Online (Sandbox Code Playgroud)

但是当我尝试显示String值时:

    NSLog(@"%@",[[[lesQuestions objectAtIndex:0] objectAtIndex:1] stringValue]);
Run Code Online (Sandbox Code Playgroud)

我有例外:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString stringValue]: unrecognized selector sent to instance
Run Code Online (Sandbox Code Playgroud)

我绝对肯定int值是第一项(索引0)而String值是第二项(索引1).当我记录持有NSArrays的MutableArray时,我得到了正确的值,所以问题在于我猜的项目的引用.

And*_*sen 6

数组中的对象已经是NSString,因此不需要调用stringValue.NSString没有实现一个名为stringValue的方法,因此你看到了异常.这样做:

NSLog(@"%@",[[lesQuestions objectAtIndex:0] objectAtIndex:1]);
Run Code Online (Sandbox Code Playgroud)

-intValue是一个由NSNumber实现的方法,用于从NSNumber实例中获取整数原语,并且intValue假设数组中的第一个对象是NSNumber(或NSString,它也实现-intValue),您的使用是正确的.

总而言之,我一般认为将不同类的实例存储在同一个数组中是一个好主意,就像你正在做的那样.使用NSDictionary可能会更好,其中每个值都使用唯一键存储,例如@"index"数字和@"name"字符串.