当我进行检查以确保 CopyItems 数组中包含某些内容时,我收到错误: NSArrayM objectAtIndex:]: index 0超出空数组的范围
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)
在验证之前我无法访问复制的项目。
@H2CO3 的良好扩展:[array objectAtIndex:index] != nil
没有意义 - NSArrays 不能包含 nil
关于objective-c - NSArrayM objectAtIndex :]: index 0 beyond bounds for empty array when checking NSPasteboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17905299/