objective-c - NSArrayM objectAtIndex :]: index 0 beyond bounds for empty array when checking NSPasteboard

标签 objective-c macos cocoa

当我进行检查以确保 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/

相关文章:

swift 3 : Set Finder label color

php - 用我的应用程序打包一个 php 自包含可执行文件

Objective-C 傻瓜书 : How do I get a value out of NSDictionary?

ios - 我们需要取消分配CFArray吗?

ios - 我怎样才能从这个字符串中得到一个 NSDate,这个 NSDate 到 long 或 NSTimeInterval

macos - 如何在 Sublime Text 2 中启用 OS X cmd+ctrl+space 字符菜单?

python - 在 Excel 文件中查找与单独文件中的文本匹配的文本,然后将填充颜色应用于行

xcode - 基于文档的 Cocoa 应用程序中的文档类型

objective-c - LSSetDefaultRoleHandlerFor 优胜美地中的内容类型

objective-c - 哪些 iOS 框架最值得学习?按什么顺序学习?