objective-c - 如果使用 NSMutableArray 而不是 NSArray,应用程序崩溃
问题描述
plot2.graphPoints = graphPointsMutableArray;
// plot2.graphPoints = @[LMGraphPointMake(CGPointMake(1, 25), @"1", @"34.5")];
如果我将 plot2.graphPoints 直接分配给 NSArray,它就像在第 2 行中一样工作正常,但是如果我像上面显示的第一个那样为它分配一个 NSMutableArray,则应用程序给我SIGABRT
一个如下所示的异常
2018-12-04 02:53:18.079192+0500 ****[10946:258059] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleObjectArrayI point]: unrecognized selector sent to instance 0x60400001f8a0'
*** First throw call stack:
(
0 CoreFoundation 0x000000011020c1cb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010f720f41 objc_exception_throw + 48
2 CoreFoundation 0x000000011028c914 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000011018f178 ___forwarding___ + 1432
4 CoreFoundation 0x000000011018eb58 _CF_forwarding_prep_0 + 120
5 Fandex 0x000000010e144823 -[LMLineGraphView drawPlots] + 1939
6 Fandex 0x000000010e13d678 -[LMLineGraphView drawRect:] + 312
7 UIKit 0x0000000111f75c49 -[UIView(CALayerDelegate) drawLayer:inContext:] + 487
8 QuartzCore 0x0000000111b380d8 -[CALayer drawInContext:] + 267
9 QuartzCore 0x0000000111a4b6cf CABackingStoreUpdate_ + 254
10 QuartzCore 0x0000000111b3e1dd ___ZN2CA5Layer8display_Ev_block_invoke + 44
11 QuartzCore 0x0000000111b37b34 -[CALayer _display] + 1701
12 QuartzCore 0x0000000111ac521f _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 655
13 QuartzCore 0x0000000111af0a14 _ZN2CA11Transaction6commitEv + 500
14 QuartzCore 0x0000000111af1760 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 76
15 CoreFoundation 0x00000001101aedb7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
16 CoreFoundation 0x00000001101aed0e __CFRunLoopDoObservers + 430
17 CoreFoundation 0x0000000110193324 __CFRunLoopRun + 1572
18 CoreFoundation 0x0000000110192a89 CFRunLoopRunSpecific + 409
19 GraphicsServices 0x0000000116ef29c6 GSEventRunModal + 62
20 UIKit 0x0000000111ea523c UIApplicationMain + 159
21 Fandex 0x000000010e1c7aff main + 111
22 libdyld.dylib 0x0000000115336d81 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
这就是我添加对象的方式NSMutableArray
NSMutableArray *graphPointsMutableArray = [[NSMutableArray alloc] init];
[graphPointsMutableArray addObject:@[LMGraphPointMake(CGPointMake(1, 25), @"1", @"34.5")]];
解决方案
你的两条线不等价。
这个:
plot2.graphPoints = @[LMGraphPointMake(CGPointMake(1, 25), @"1", @"34.5")];
分配一个NSArray
包含单个LMGraphPoint
到plot2.graphPoints
而这:
[graphPointsMutableArray addObject:@[LMGraphPointMake(CGPointMake(1, 25), @"1", @"34.5")]];
plot2.graphPoints = graphPointsMutableArray;
分配一个NSMutableArray
包含一个NSArray
包含一个LMGraphPoint
到plot2.graphPoints
你要
[graphPointsMutableArray addObject:LMGraphPointMake(CGPointMake(1, 25), @"1", @"34.5")];
IE。删除@[]
数组文字。
推荐阅读
- mysql - WordPress管理面板重定向并且没有解决方案有效
- ios - 从 iTunesConnect 中(永久)删除应用程序后重新使用相同的捆绑包 ID
- r - 如何对 R 中的 73 个变量进行夏皮罗威尔克斯测试
- c - 为什么 select() 有时会连续返回 0
- react-native - React native:Error:Location ..... 不可移动
- firebase - Firebase 存储设置速率限制
- javascript - WooCommerce:停止滚动到结帐页面上的错误消息
- java - 使用 mongodb 在 Java Spring 中进行国际化
- fonts - latex:公式中统一的字体大小(给定大小的字体),与公式内容无关
- jquery - Wordpress 联系表 7 比较日期