之前, 我们学习了C语言, 知道了C语言是一种弱语法, 它没有把一些不规范的写法直接报错, 而是只有一个警告, 我们都知道警告是可以编译成功的, 只有在运行时或者链接时才会报错, 那么在OC里又是怎样呢? 下面让我们来看看~~
C语言例子:
int main() { asdasd(); return 0; }
编译的结果:
Cain:2.第二天 Cain$ cc -c 3-OC弱语法.m 3-OC弱语法.m:11:5: warning: implicit declaration of function 'asdasd' is invalid in C99 [-Wimplicit-function-declaration] asdasd(); ^ 1 warning generated.
链接的结果:
Cain:2.第二天 Cain$ cc 3-OC弱语法.o Undefined symbols for architecture x86_64: "_asdasd", referenced from: _main in 3-OC弱语法.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
C语言就是这样子的弱语法, 有不合理不规范的语法, 只会报一个警告, 不会有什么作为, 只有在链接的时候才会报错, 那么OC又如何呢?? 下面让我们来看看~~~
OC语言例子:
#import <Foundation/Foundation.h> @interface Student : NSObject @end @implementation Student @end int main() { Student *stu = [Student new]; [stu abcd]; return 0; }
链接的结果:
Cain:2.第二天 Cain$ cc -c 3-OC弱语法.m 3-OC弱语法.m:13:10: warning: instance method '-abcd' not found (return type defaults to 'id') [-Wobjc-method-access] [stu abcd]; ^~~~ 3-OC弱语法.m:3:12: note: receiver is instance of class declared here @interface Student : NSObject ^ 1 warning generated.
编译的结果:
Cain:2.第二天 Cain$ cc 3-OC弱语法.o -framework Foundation Cain:2.第二天 Cain$
运行的结果:
Cain:2.第二天 Cain$ ./a.out 2015-01-16 20:20:07.086 a.out[13226:1438772] -[Student abcd]: unrecognized selector sent to instance 0x7f98b1c0e060 2015-01-16 20:20:07.090 a.out[13226:1438772] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Student abcd]: unrecognized selector sent to instance 0x7f98b1c0e060' *** First throw call stack: ( 0 CoreFoundation 0x00007fff9587464c __exceptionPreprocess + 172 1 libobjc.A.dylib 0x00007fff9b68b6de objc_exception_throw + 43 2 CoreFoundation 0x00007fff958776bd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x00007fff957bea84 ___forwarding___ + 1028 4 CoreFoundation 0x00007fff957be5f8 _CF_forwarding_prep_0 + 120 5 a.out 0x0000000107d31f54 main + 68 6 libdyld.dylib 0x00007fff955795c9 start + 1 7 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException Abort trap: 6
其实OC和C有一些不一样, OC只会在运行的时候才会检测语法是否有错误, 如果一旦有错误, 才会报错, 否则就会继续运行.
那么这个报错是什么意思呢? 其实在这里那么多信息, 我们只要找到一句就可以了:
2015-01-16 20:20:07.086 a.out[13226:1438772] -[Student abcd]: unrecognized selector sent to instance 0x7f98b1c0e060
意思是: 你所发送给对象的消息无法识别. (大概是这么个意思)
那如果再改一下呢?
#import <Foundation/Foundation.h> @interface Student : NSObject - (void)abcd; @end @implementation Student @end int main() { Student *stu = [Student new]; [stu abcd]; return 0; }
编译结果:
Cain:2.第二天 Cain$ cc -c 3-OC弱语法.m 3-OC弱语法.m:7:17: warning: method definition for 'abcd' not found [-Wincomplete-implementation] @implementation Student ^ 3-OC弱语法.m:4:1: note: method 'abcd' declared here - (void)abcd; ^ 1 warning generated.
链接运行结果:
Cain:2.第二天 Cain$ cc 3-OC弱语法.o -framework Foundation Cain:2.第二天 Cain$ ./a.out 2015-01-16 20:25:14.458 a.out[13240:1440945] -[Student abcd]: unrecognized selector sent to instance 0x7fd1ea40e060 2015-01-16 20:25:14.459 a.out[13240:1440945] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Student abcd]: unrecognized selector sent to instance 0x7fd1ea40e060' *** First throw call stack: ( 0 CoreFoundation 0x00007fff9587464c __exceptionPreprocess + 172 1 libobjc.A.dylib 0x00007fff9b68b6de objc_exception_throw + 43 2 CoreFoundation 0x00007fff958776bd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x00007fff957bea84 ___forwarding___ + 1028 4 CoreFoundation 0x00007fff957be5f8 _CF_forwarding_prep_0 + 120 5 a.out 0x000000010af7ff64 main + 68 6 libdyld.dylib 0x00007fff955795c9 start + 1 7 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException Abort trap: 6
还有一种结果:
#import <Foundation/Foundation.h> @interface Student : NSObject @end @implementation Student - (void)abcd { } @end int main() { Student *stu = [Student new]; [stu abcd]; return 0; }
编译链接运行结果:
Cain:2.第二天 Cain$ cc -c 3-OC弱语法.m Cain:2.第二天 Cain$ cc 3-OC弱语法.o -framework Foundation Cain:2.第二天 Cain$
在OC中, 可以不声明, 直接实现方法
这对初学者是非常的不利, 没有严格的语法规范, 那么在学习的过程中会有很多的错误写法, 所以我才严格要求大家要书写规范, 只有严格要求自己, 才能成为一个优秀的程序员.
好了这次就讲到这里, 下次我们继续~~~~~