如果使用 NSMutableArray 而不是 NSArray,应用程序会崩溃
App crash if use NSMutableArray instead of 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")];
将包含单个 LMGraphPoint
的 NSArray
分配给 plot2.graphPoints
同时:
[graphPointsMutableArray addObject:@[LMGraphPointMake(CGPointMake(1, 25), @"1", @"34.5")]];
plot2.graphPoints = graphPointsMutableArray;
将包含单个 LMGraphPoint
的 NSMutableArray
分配给 plot2.graphPoints
你想要
[graphPointsMutableArray addObject:LMGraphPointMake(CGPointMake(1, 25), @"1", @"34.5")];
即。删除 @[]
数组文字。
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")];
将包含单个 LMGraphPoint
的 NSArray
分配给 plot2.graphPoints
同时:
[graphPointsMutableArray addObject:@[LMGraphPointMake(CGPointMake(1, 25), @"1", @"34.5")]];
plot2.graphPoints = graphPointsMutableArray;
将包含单个 LMGraphPoint
的 NSMutableArray
分配给 plot2.graphPoints
你想要
[graphPointsMutableArray addObject:LMGraphPointMake(CGPointMake(1, 25), @"1", @"34.5")];
即。删除 @[]
数组文字。