当我使用NSMutableArray作为Singleton的属性时,将一些对象添加到NSMutableArray中。它粉碎

时间:2017-03-13 16:10:34

标签: ios objective-c nsmutablearray

这是我的sampleCode:

-(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

我不知道这些代码有什么问题。当我在mutableArray中添加了一些对象时,它停在了这一行:(一个全部异常BreakPoint)

  _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

如果有人能给我任何想法或帮助链接。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您错误地将NSMutableArray子类化。它是一个类集群,需要实现一组最小的方法。

在这种情况下,您正在调用initWithCapacity:但尚未覆盖该方法。或者,更有可能的是,您正在调用调用该方法的泛型方法(init)。

正如异常所说:

2017-03-14 01:01:36.586 tesr1111 [7075:1333259]由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [NSMutableArray initWithCapacity:]:仅为抽象定义的方法类。定义 - [OnlineClassItemizeArray initWithCapacity:]!'

但是,对集合类进行子类化是code smell。它几乎没有完成。在极少数情况下,它是有道理的;你的是什么?