前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >写OC遇到的一个超级低级错误:

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

作者头像
望天
发布2018-12-25 11:49:28
6980
发布2018-12-25 11:49:28
举报
文章被收录于专栏:along的开发之旅
代码语言:javascript
复制
代码:
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顺序反了, 正确调用方式是

代码语言:javascript
复制
IJKDemoLogTrace *demoLog = [[IJKDemoLogTrace alloc] init];

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年12月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档