ios - NSArray无法识别的选择器发送到实例

时间:2014-08-13 02:35:57

标签: ios objective-c nsarray

我有一个NSArray类型的实例变量_sessions。 在整个TableViewController方法中,我访问它,它很好。但是在prepareForSegue中,我尝试访问它或它的值,它会抛出这个错误..

相关代码:

@interface SessionsTVC()
{

    NSArray *_sessions;
    JSONDataRetriever *jsonRetriever;
    NSString *requestURL;
}
@end
@implementation中的

_sessions = [[NSArray alloc]initWithArray:_tempArray];

排队给我带来麻烦:

NSLog([_sessions objectAtIndex:1]);

NSLog(_sessions);

甚至更奇怪的是,这是有效的:

NSLog([@(_sessions.count) stringValue]);

编辑:

NSLog([@(_ sessions.count)stringValue]);让我回到2(这是完全正确的)..

完整的错误消息

2014-08-13 23:14:46.548 ReadDatabase [15142:650918] - [会话长度]:无法识别的选择器发送到实例0x7fb5fbf55090 2014-08-13 23:14:46.644 ReadDatabase [15142:650918] *由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [会话长度]:无法识别的选择器已发送例如0x7fb5fbf55090' * 第一次抛出调用堆栈: (     0 CoreFoundation 0x00000001074313e5 exceptionPreprocess + 165     1 libobjc.A.dylib 0x00000001070e1967 objc_exception_throw + 45     2 CoreFoundation 0x00000001074384fd - [NSObject(NSObject)doesNotRecognizeSelector:] + 205     3 CoreFoundation 0x00000001073907ec ___ forwarding _ + 988     4 CoreFoundation 0x0000000107390388 _CF_forwarding_prep_0 + 120     5 CoreFoundation 0x000000010732a39b CFStringAppendFormatCore + 235     6 CoreFoundation 0x0000000107411c00 _CFStringCreateWithFormatAndArgumentsAux2 + 256     7 CoreFoundation 0x0000000107420a4f _CFLogvEx2 + 127     8基金会0x0000000106c7da22 NSLogv + 99     9基金会0x0000000106c7d9a7 NSLog + 148     10 ReadDatabase 0x00000001054a36f6 - [SessionsTVC prepareForSegue:sender:] + 518     11 UIKit 0x0000000105f04716 - [UIStoryboardSegueTemplate _perform:] + 151     12 UIKit 0x0000000105aa0d40 - [UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1242     13 UIKit 0x0000000105aa0eb4 - [UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219     14 UIKit 0x00000001059dc97e _applyBlockToCFArrayCopiedToStack + 314     15 UIKit 0x00000001059dc7f8 _afterCACommitHandler + 516     16 CoreFoundation 0x0000000107366337 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23     17 CoreFoundation 0x0000000107366290 __CFRunLoopDoObservers + 368     18 CoreFoundation 0x000000010735c0c3 __CFRunLoopRun + 1123     19 CoreFoundation 0x000000010735b9f6 CFRunLoopRunSpecific + 470     20 GraphicsServices 0x000000010960c9f0 GSEventRunModal + 161     21 UIKit 0x00000001059b9990 UIApplicationMain + 1282     22 ReadDatabase 0x00000001054a2bf3 main + 115     23 libdyld.dylib 0x0000000109def145 start + 1     24 ??? 0x0000000000000001 0x0 + 1 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)

2 个答案:

答案 0 :(得分:1)

NSLog需要一个字符串作为第一个参数。

试试这个:

NSLog(@"%@", _sessions);

NSLog(@"%@", [_sessions objectAtIndex:1]);

确保_session.count在第二种情况下比一个更大。

答案 1 :(得分:0)

这条线有什么结果:

NSLog([@(_sessions.count) stringValue]);

因为我认为它等同于:

NSLog([[NSNumber numberWithInt:_sessions.count] stringValue]);

问题可能是_sessions为零,因此_sessions.count将返回0,您将在控制台中看到0为字符串。

BTW,这是使用NSLog()打印对象的正确方法:

NSLog(@"%@", [_sessions objectAtIndex:1]);