写OC遇到的一个超级低级错误:

版权声明:欢迎传播,请标明出处。 https://blog.csdn.net/u201011221/article/details/84951673
代码:
IJKDemoLogTrace *demo = [[IJKDemoLogTrace init] alloc];

AppCode提示: class method is called from instance context.

编译ok, 运行报错:

2018-12-11 10:58:01.497336+0800 ARMPlayerDemo[24872:14368023] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[IJKDemoLogTrace<0x10241c110> init]: cannot init a class object.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001074271bb __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x00000001069c5735 objc_exception_throw + 48
    2   CoreFoundation                      0x00000001074465cf +[NSObject(NSObject) init] + 127
    3   ARMPlayerDemo                       0x0000000102401efb -[IJKDemoMainViewController viewDidLoad] + 427
    4   UIKitCore                           0x000000010f5304e1 -[UIViewController loadViewIfRequired] + 1186
    5   UIKitCore                           0x000000010f490104 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 68
    6   UIKitCore                           0x000000010f4903f8 -[UINavigationController _startTransition:fromViewController:toViewController:] + 147
    7   UIKitCore                           0x000000010f49148b -[UINavigationController _startDeferredTransitionIfNeeded:] + 896
    8   UIKitCore                           0x000000010f4927e0 -[UINavigationController __viewWillLayoutSubviews] + 150
    9   UIKitCore                           0x000000010f472600 -[UILayoutContainerView layoutSubviews] + 217
    10  UIKit                               0x000000011ff796c4 -[UILayoutContainerViewAccessibility layoutSubviews] + 42
    11  UIKitCore                           0x0000000110039795 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1441
    12  QuartzCore                          0x0000000105e66b19 -[CALayer layoutSublayers] + 175
    13  QuartzCore                          0x0000000105e6b9d3 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395
    14  QuartzCore                          0x0000000105de47ca _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 342
    15  QuartzCore                          0x0000000105e1b97e _ZN2CA11Transaction6commitEv + 576
    16  UIKitCore                           0x000000010fb6a2d0 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 139
    17  CoreFoundation                      0x000000010738c62c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    18  CoreFoundation                      0x000000010738bde0 __CFRunLoopDoBlocks + 336
    19  CoreFoundation                      0x0000000107386654 __CFRunLoopRun + 1284
    20  CoreFoundation                      0x0000000107385e11 CFRunLoopRunSpecific + 625
    21  GraphicsServices                    0x000000010a82c1dd GSEventRunModal + 62
    22  UIKitCore                           0x000000010fb4f81d UIApplicationMain + 140
    23  ARMPlayerDemo                       0x000000010240d850 main + 112
    24  libdyld.dylib                       0x00000001079c3575 start + 1

最后发现是alloc和init顺序反了, 正确调用方式是

IJKDemoLogTrace *demoLog = [[IJKDemoLogTrace alloc] init];

AppCode给的提示`class method is called from instance context.`很形象.

init是一个类的方法, 不应该在instance context里调用.

猜你喜欢

转载自blog.csdn.net/u201011221/article/details/84951673