当我使用 NSMutableArray 作为 Singleton 的 属性 时,将一些对象添加到 NSMutableArray 中。它压碎了
When I use a NSMutableArray which as a property of a Singleton , to add some object into the NSMutableArray. It crushed
这是我的示例代码:
-(IBAction)add:(id)sender {
//ShoppingManager sharedManager is a singleton
[[ShoppingManager sharedManager].onlineClassArray addObject:@(1)];
}
和 ShoppingManager.m 中的代码:
//singleton
+ (ShoppingManager *)sharedManager{
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc]init];
});
return instance;
}
- (OnlineClassItemizeArray *)onlineClassArray{
if (!_onlineClassArray) {
_onlineClassArray = [OnlineClassItemizeArray array];
}
return _onlineClassArray;
}
和 ShoppingMananger.h:
@interface ShoppingManager : NSObject
@property(nonatomic,strong) OnlineClassItemizeArray * onlineClassArray;
+ (ShoppingManager*)sharedManager;
@end
我不知道这些有什么问题 codes.when 我在 mutableArray 中添加了一些对象,它停在这一行:(一个所有异常断点)
_onlineClassArray = [OnlineClassItemizeArray array];
比这里压碎:
DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
void
_dispatch_once(dispatch_once_t *predicate,
DISPATCH_NOESCAPE dispatch_block_t block)
{
if (DISPATCH_EXPECT(*predicate, ~0l) != ~0l) {
dispatch_once(predicate, block);//<---crushed here
} else {
dispatch_compiler_barrier();
}
DISPATCH_COMPILER_CAN_ASSUME(*predicate == ~0l);
}
这里是整个错误消息:
2017-03-14 01:01:36.586 tesr1111[7075:1333259] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray initWithCapacity:]: method only defined for abstract class. Define -[OnlineClassItemizeArray initWithCapacity:]!'
*** First throw call stack:
(
0 CoreFoundation 0x00000001100c4d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010fb2621e objc_exception_throw + 48
2 CoreFoundation 0x00000001101357df __CFRequireConcreteImplementation + 255
3 CoreFoundation 0x00000001101241a7 -[NSMutableArray initWithCapacity:] + 39
4 tesr1111 0x000000010f54e6d0 -[ShoppingManager init] + 160
5 tesr1111 0x000000010f54e5f1 __32+[ShoppingManager sharedManager]_block_invoke + 65
6 libdispatch.dylib 0x0000000112eda0cd _dispatch_client_callout + 8
7 libdispatch.dylib 0x0000000112ebf1f8 dispatch_once_f + 501
8 tesr1111 0x000000010f54e588 +[ShoppingManager sharedManager] + 136
9 tesr1111 0x000000010f54de9d -[ViewController add:] + 61
10 UIKit 0x00000001104ea8bc -[UIApplication sendAction:to:from:forEvent:] + 83
11 UIKit 0x0000000110670c38 -[UIControl sendAction:to:forEvent:] + 67
12 UIKit 0x0000000110670f51 -[UIControl _sendActionsForEvents:withEvent:] + 444
13 UIKit 0x000000011066fe4d -[UIControl touchesEnded:withEvent:] + 668
14 UIKit 0x0000000110558545 -[UIWindow _sendTouchesForEvent:] + 2747
15 UIKit 0x0000000110559c33 -[UIWindow sendEvent:] + 4011
16 UIKit 0x00000001105069ab -[UIApplication sendEvent:] + 371
17 UIKit 0x0000000120cca481 -[UIApplicationAccessibility sendEvent:] + 93
18 UIKit 0x0000000110cf372d __dispatchPreprocessedEventFromEventQueue + 3248
19 UIKit 0x0000000110cec463 __handleEventQueue + 4879
20 CoreFoundation 0x0000000110069761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
21 CoreFoundation 0x000000011004e98c __CFRunLoopDoSources0 + 556
22 CoreFoundation 0x000000011004de76 __CFRunLoopRun + 918
23 CoreFoundation 0x000000011004d884 CFRunLoopRunSpecific + 420
24 GraphicsServices 0x0000000113eb0a6f GSEventRunModal + 161
25 UIKit 0x00000001104e8c68 UIApplicationMain + 159
26 tesr1111 0x000000010f54e4df main + 111
27 libdyld.dylib 0x0000000112f2668d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
如果有人能给我任何想法或帮助link。提前致谢。
您classNSMutableArray
的子NSMutableArray
不正确。它是一个 class 集群,需要实现最少的一组方法。
在这种情况下,您正在调用 initWithCapacity:
但尚未覆盖该方法。或者,更有可能的是,您正在调用调用该方法的通用方法 (init
)。
正如异常所述:
2017-03-14 01:01:36.586 tesr1111[7075:1333259] 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:' -[ NSMutableArray initWithCapacity:]:方法仅为抽象定义 class。定义 -[OnlineClassItemizeArray initWithCapacity:]!'
但是,子class集合 classes 是 code smell
。这几乎从未完成过。在极少数情况下它是有意义的;你的是什么?
这是我的示例代码:
-(IBAction)add:(id)sender {
//ShoppingManager sharedManager is a singleton
[[ShoppingManager sharedManager].onlineClassArray addObject:@(1)];
}
和 ShoppingManager.m 中的代码:
//singleton
+ (ShoppingManager *)sharedManager{
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc]init];
});
return instance;
}
- (OnlineClassItemizeArray *)onlineClassArray{
if (!_onlineClassArray) {
_onlineClassArray = [OnlineClassItemizeArray array];
}
return _onlineClassArray;
}
和 ShoppingMananger.h:
@interface ShoppingManager : NSObject
@property(nonatomic,strong) OnlineClassItemizeArray * onlineClassArray;
+ (ShoppingManager*)sharedManager;
@end
我不知道这些有什么问题 codes.when 我在 mutableArray 中添加了一些对象,它停在这一行:(一个所有异常断点)
_onlineClassArray = [OnlineClassItemizeArray array];
比这里压碎:
DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
DISPATCH_SWIFT3_UNAVAILABLE("Use lazily initialized globals instead")
void
_dispatch_once(dispatch_once_t *predicate,
DISPATCH_NOESCAPE dispatch_block_t block)
{
if (DISPATCH_EXPECT(*predicate, ~0l) != ~0l) {
dispatch_once(predicate, block);//<---crushed here
} else {
dispatch_compiler_barrier();
}
DISPATCH_COMPILER_CAN_ASSUME(*predicate == ~0l);
}
这里是整个错误消息:
2017-03-14 01:01:36.586 tesr1111[7075:1333259] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray initWithCapacity:]: method only defined for abstract class. Define -[OnlineClassItemizeArray initWithCapacity:]!'
*** First throw call stack:
(
0 CoreFoundation 0x00000001100c4d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010fb2621e objc_exception_throw + 48
2 CoreFoundation 0x00000001101357df __CFRequireConcreteImplementation + 255
3 CoreFoundation 0x00000001101241a7 -[NSMutableArray initWithCapacity:] + 39
4 tesr1111 0x000000010f54e6d0 -[ShoppingManager init] + 160
5 tesr1111 0x000000010f54e5f1 __32+[ShoppingManager sharedManager]_block_invoke + 65
6 libdispatch.dylib 0x0000000112eda0cd _dispatch_client_callout + 8
7 libdispatch.dylib 0x0000000112ebf1f8 dispatch_once_f + 501
8 tesr1111 0x000000010f54e588 +[ShoppingManager sharedManager] + 136
9 tesr1111 0x000000010f54de9d -[ViewController add:] + 61
10 UIKit 0x00000001104ea8bc -[UIApplication sendAction:to:from:forEvent:] + 83
11 UIKit 0x0000000110670c38 -[UIControl sendAction:to:forEvent:] + 67
12 UIKit 0x0000000110670f51 -[UIControl _sendActionsForEvents:withEvent:] + 444
13 UIKit 0x000000011066fe4d -[UIControl touchesEnded:withEvent:] + 668
14 UIKit 0x0000000110558545 -[UIWindow _sendTouchesForEvent:] + 2747
15 UIKit 0x0000000110559c33 -[UIWindow sendEvent:] + 4011
16 UIKit 0x00000001105069ab -[UIApplication sendEvent:] + 371
17 UIKit 0x0000000120cca481 -[UIApplicationAccessibility sendEvent:] + 93
18 UIKit 0x0000000110cf372d __dispatchPreprocessedEventFromEventQueue + 3248
19 UIKit 0x0000000110cec463 __handleEventQueue + 4879
20 CoreFoundation 0x0000000110069761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
21 CoreFoundation 0x000000011004e98c __CFRunLoopDoSources0 + 556
22 CoreFoundation 0x000000011004de76 __CFRunLoopRun + 918
23 CoreFoundation 0x000000011004d884 CFRunLoopRunSpecific + 420
24 GraphicsServices 0x0000000113eb0a6f GSEventRunModal + 161
25 UIKit 0x00000001104e8c68 UIApplicationMain + 159
26 tesr1111 0x000000010f54e4df main + 111
27 libdyld.dylib 0x0000000112f2668d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
如果有人能给我任何想法或帮助link。提前致谢。
您classNSMutableArray
的子NSMutableArray
不正确。它是一个 class 集群,需要实现最少的一组方法。
在这种情况下,您正在调用 initWithCapacity:
但尚未覆盖该方法。或者,更有可能的是,您正在调用调用该方法的通用方法 (init
)。
正如异常所述:
2017-03-14 01:01:36.586 tesr1111[7075:1333259] 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:' -[ NSMutableArray initWithCapacity:]:方法仅为抽象定义 class。定义 -[OnlineClassItemizeArray initWithCapacity:]!'
但是,子class集合 classes 是 code smell
。这几乎从未完成过。在极少数情况下它是有意义的;你的是什么?