Objective c NSArrayM objectAtIndex::检查NSPasteboard时索引0超出空数组的界限
当我进行验证以确保copiedItems数组中包含某些内容时,我收到了错误:NSArrayM objectAtIndex::索引0超出了空数组的界限Objective c NSArrayM objectAtIndex::检查NSPasteboard时索引0超出空数组的界限,objective-c,macos,cocoa,Objective C,Macos,Cocoa,当我进行验证以确保copiedItems数组中包含某些内容时,我收到了错误:NSArrayM objectAtIndex::索引0超出了空数组的界限 NSArray *classes = [[NSArray alloc] initWithObjects:[NSString class], nil]; NSDictionary *options = [NSDictionary dictionary]; NSArray *copiedItems = [pasteboard readObjectsFo
NSArray *classes = [[NSArray alloc] initWithObjects:[NSString class], nil]; NSDictionary *options = [NSDictionary dictionary];
NSArray *copiedItems = [pasteboard readObjectsForClasses:classes options:options];
if ([copiedItems objectAtIndex:0]!= nil){
NSString *copiedString = [copiedItems objectAtIndex:0];
结果是我的支票出了差错。通过调用
[copiedItems objectAtIndex:0]
,我当时正在访问数组,而不是先查看它是否有效(我的初衷)。通过将[copiedItems objectAtIndex:0]
更改为if([copiedItems count]>0)
在验证之前,我无法访问copiedItems
从@H2CO3:
[array objectAtIndex:index]的良好扩展nil
没有意义-nsarray不能包含nil以在此基础上展开:[array objectAtIndex:index]!=nil
没有意义-NSArray
s不能包含nil
…也不会objectAtIndex:
返回nil
以指示超出范围的索引。如文件所述,它引发了一个异常。