首页
社区
课程
招聘
6
[原创]Objective-C基本分析法
发表于: 2012-11-20 21:39 13834

[原创]Objective-C基本分析法

2012-11-20 21:39
13834

以Hello World程序为例:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import <foundation foundation.h="">
 
 
@interface SaySomething :NSObject
- (void) say:(NSString *)phrase;
@end
 
@implementation SaySomething
 
- (void) say:(NSString *)phrase{
    printf("%s\n", [phrase UTF8String]);
}
 
@end
 
int main(){
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    SaySomething *saySomething = [[SaySomething alloc] init];
    [saySomething say:@"Hello World!"];
    [saySomething release];
    [pool release];
    return 0;
}</foundation>



保存为hello.m,使用如下命令编译:



iMAC:ios$ /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2 -o hello hello.m -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/ -framework Foundation -lobjc



查看所使用的dylib:




iMAC:ios$ otool -L hello
hello:
/System/Library/Frameworks/Foundation.framework/Foundation (compatibility version 300.0.0, current version 992.0.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) //包含这个文件则为objective-c编译的文件
/usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 7.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 173.8.0)
/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation (compatibility version 150.0.0, current version 793.0.0)



dump文件中的相关数据信息:



iMAC:ios$ otool -l hello |grep __objc
sectname __objc_methname
sectname __objc_methtype
sectname __objc_classname
sectname __objc_classlist
sectname __objc_imageinfo
sectname __objc_const
sectname __objc_selrefs
sectname __objc_classrefs
sectname __objc_data


Objective-C会存储一部分的类型名和类型、类名参考等等信息到文件中。这些信息可以通过strings获取到:



iMAC:ios$ strings hello
Hello World!
say:
release
init
alloc
UTF8String
autorelease
v12@0:4@8
SaySomething


但是从本质上来说,Objective-C是一种由名称引用数据或方法的方式,即,根据所使用的名称调用对应的处理方法。作为一个相同对比例子,我们来看一个基本上由C语言方式编写的程序,实现相同功能的Hello World。当然,这个也是调用了Objective-C中的方法。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#import <foundation foundation.h="">
 
@interface SaySomething :NSObject
- (void) say:(NSString *)phrase;
@end
 
@implementation SaySomething
 
- (void) say:(NSString *)phrase{
    printf("%s\n", [phrase UTF8String]);
}
 
@end
 
//int main(){
//    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//    SaySomething *saySomething = [[SaySomething alloc] init];
//    [saySomething say:@"Hello World!"];
//    [saySomething release];
//    [pool release];
//    return 0;
//}
 
int main(){
    objc_msgSend(objc_msgSend(objc_getClass("NSAutoreleasePool"), NSSelectorFromString(@"alloc")), NSSelectorFromString(@"init"));
    objc_msgSend(objc_msgSend(objc_msgSend(objc_getClass("SaySomething"), NSSelectorFromString(@"alloc")), NSSelectorFromString(@"init")), NSSelectorFromString(@"say:"), @"Hello World!");
    return 0;
}
</foundation>



常见类型对应的内容:



__objc_methname


__objc_methtype


程序中使用的方法名和方法类型


__objc_classname


__objc_classlist


__objc_nlclslist


程序中使用的各种类名和类列表


__objc_catlist



[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 6
支持
分享
赞赏记录
参与人
雪币
留言
时间
伟叔叔
为你点赞~
2024-5-31 06:27
心游尘世外
为你点赞~
2024-5-31 03:19
QinBeast
为你点赞~
2024-5-31 03:10
飘零丶
为你点赞~
2024-4-1 02:14
shinratensei
为你点赞~
2024-2-2 04:46
PLEBFE
为你点赞~
2023-3-7 00:38
最新回复 (4)
雪    币: 1898
活跃值: (1880)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
2
楼主可以直接列出object-c与C和C++的不同点会更加受人青睐,这样的纯object-c新手语法教学帖看帖不如去看书。又或者楼主发些看书的感悟和历程更好。个人建议,不喜勿喷。
2012-11-20 22:49
0
雪    币: 1413
活跃值: (401)
能力值: (RANK:270 )
在线值:
发帖
回帖
粉丝
3
1. 有本书叫From C++ to Objective-C,还不错

2. 要从逆向的角度写两个语言的不同,很难,也不适合入门

3. 建议不要把C、C++、Obj-C看成同一族语言,这样会对正确理解其中的一些概念有好处。例如,Obj-C中,消息的概念决定了很多独特的特性。
2012-11-21 20:14
0
雪    币: 617
活跃值: (1597)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
学习了。。。
2013-3-25 23:26
0
雪    币: 1234
活跃值: (317)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
牛人,学习了。
2013-9-11 16:56
0
游客
登录 | 注册 方可回帖
返回

账号登录
验证码登录

忘记密码?
没有账号?立即免费注册