2011-05-11 14:06 佚名 otierney 字号:T | T

本文为台湾出版的《Objective-C学习大纲》的翻译文档,系统介绍了Objective-C代码,很多名词为台湾同胞特指词汇,在学习时仔细研读才能体会。

AD:干货来了,不要等!WOT2015 北京站演讲PPT开放下载!

Foundation framework classes

Foundation framework 地位如同 C++ 的 Standard Template Library。不过 Objective-C 是真正的动态识别语言(dynamic types),所以不需要像 C++ 那样肥得可怕的样版(templates)。这个 framework 包含了物件组、网路、执行绪,还有更多好东西。

NSArray

  1. main.m
  2. #import
  3. #import
  4. #import
  5. #import
  6. #import
  7. void print( NSArray *array ) {
  8. NSEnumerator *enumerator = [array objectEnumerator];
  9. id obj;
  10. while ( obj = [enumerator nextObject] ) {
  11. printf( "%s\n", [[obj description] cString] );
  12. }
  13. }
  14. int main( int argc, const char *argv[] ) {
  15. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  16. NSArray *arr = [[NSArray alloc] initWithObjects:
  17. @"Me", @"Myself", @"I", nil];
  18. NSMutableArray *mutable = [[NSMutableArray alloc] init];
  19. // enumerate over items
  20. printf( "----static array\n" );
  21. print( arr );
  22. // add stuff
  23. [mutable addObject: @"One"];
  24. [mutable addObject: @"Two"];
  25. [mutable addObjectsFromArray: arr];
  26. [mutable addObject: @"Three"];
  27. // print em
  28. printf( "----mutable array\n" );
  29. print( mutable );
  30. // sort then print
  31. printf( "----sorted mutable array\n" );
  32. [mutable sortUsingSelector: @selector( caseInsensitiveCompare: )];
  33. print( mutable );
  34. // free memory
  35. [arr release];
  36. [mutable release];
  37. [pool release];
  38. return 0;
  39. }

output

  1. ----static array
  2. Me
  3. Myself
  4. I
  5. ----mutable array
  6. One
  7. Two
  8. Me
  9. Myself
  10. I
  11. Three
  12. ----sorted mutable array
  13. I
  14. Me
  15. Myself
  16. One
  17. Three
  18. Two

阵列有两种(通常是 Foundation classes 中最资料导向的部分),NSArray 跟 NSMutableArray,顾名思义,mutable(善变的)表示可以被改变,而 NSArray 则不行。这表示你可以製造一个 NSArray 但却不能改变它的长度。

你可以用 Obj, Obj, Obj, ..., nil 为参数唿叫建构子来初始化一个阵列,其中 nil 表示结尾符号。

排序(sorting)展示如何用 selector 来排序一个物件,这个 selector 告诉阵列用 NSString 的忽略大小写顺序来排序。如果你的物件有好几个排序方法,你可以使用这个 selector 来选择你想用的方法。

在 print method 裡,我使用了 description method。它就像 Java 的 toString,会回传物件的 NSString 表示法。

NSEnumerator 很像 Java 的列举系统。while ( obj = [array objectEnumerator] ) 行得通的理由是 objectEnumerator 会回传最后一个物件的 nil。在 C 裡 nil 通常代表 0,也就是 false。改用 ( ( obj = [array objectEnumerator] ) != nil ) 也许更好。

NSDictionary

  1. main.m
  2. #import
  3. #import
  4. #import
  5. #import
  6. #import <<>
  7. #import
  8. void print( NSDictionary *map ) {
  9. NSEnumerator *enumerator = [map keyEnumerator];
  10. id key;
  11. while ( key = [enumerator nextObject] ) {
  12. printf( "%s => %s\n",
  13. [[key description] cString],
  14. [[[map objectForKey: key] description] cString] );
  15. }
  16. }
  17. int main( int argc, const char *argv[] ) {
  18. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  19. NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
  20. @"one", [NSNumber numberWithInt: 1],
  21. @"two", [NSNumber numberWithInt: 2],
  22. @"three", [NSNumber numberWithInt: 3],
  23. nil];
  24. NSMutableDictionary *mutable = [[NSMutableDictionary alloc] init];
  25. // print dictionary
  26. printf( "----static dictionary\n" );
  27. print( dictionary );
  28. // add objects
  29. [mutable setObject: @"Tom" forKey: @"tom@jones.com"];
  30. [mutable setObject: @"Bob" forKey: @"bob@dole.com" ];
  31. // print mutable dictionary
  32. printf( "----mutable dictionary\n" );
  33. print( mutable );
  34. // free memory
  35. [dictionary release];
  36. [mutable release];
  37. [pool release];
  38. return 0;
  39. }

output 

  1. ----static dictionary
  2. 1 => one
  3. 2 => two
  4. 3 => three
  5. ----mutable dictionary
  6. bob@dole.com => Bob
  7. tom@jones.com => Tom

优点与缺点

优点

Cateogies

Posing

动态识别

指标计算

弹性讯息传递

不是一个过度复杂的 C 衍生语言

可透过 Objective-C++ 与 C++ 结合

缺点

不支援命名空间

不支援运算子多载(虽然这常常被视为一个优点,不过正确地使用运算子多载可以降低程式码复杂度)

语言裡仍然有些讨厌的东西,不过不比 C++ 多

Objective-C代码学习大纲(6)的更多相关文章

  1. Objective-C代码学习大纲(3)

    Objective-C代码学习大纲(3) 2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍 ...

  2. Objective-C代码学习大纲(5)

    2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ...

  3. Objective-C代码学习大纲(4)

    2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ...

  4. Objective-C代码学习大纲(2)

    2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ...

  5. Objective-C代码学习大纲(1)

    2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ...

  6. 大数据Python学习大纲

    最近公司在写一个课程<大数据运维实训课>,分为4个部分,linux实训课.Python开发.hadoop基础知识和项目实战.这门课程主要针对刚从学校毕业的学生去应聘时不会像一个小白菜一样被 ...

  7. JVM学习——学习方法论&学习大纲

    2020年02月06日22:25:51 完成了Springboot系列的学习和Kafka的学习,接下来进入JVM的学习阶段 深入理解JVM 学习方法论 如何去学习一门课程--方法论 多讨论,从别人身上 ...

  8. u-boot代码学习内容

    前言  u-boot代码庞大,不可能全部细读,只能有选择的读部分代码.在读代码之前,根据韦东山教材,关于代码学习内容和深度做以下预先划定. 一.Makefile.mkconfig.config.mk等 ...

  9. Linux 系统从入门到精通的学习大纲;

    以前没有接触过Linux,生产环境需要,有时候遇到问题,百度一下,问题解决了,在遇到问题,在百度,有时候问题是如何解决的,为什么会解决有点丈二的和尚摸不着头脑, 为此,想用一段时间,系统的学习下Lin ...

随机推荐

  1. Apach 配置虚拟机时候DocumentRoot参数最后不要加斜杠

    DocumentRoot "D:\baiduyun\webroot\jedi\app\static" 这样是可以的 DocumentRoot "D:\baiduyun\w ...

  2. ecshop的数据库getRow、getAll、getOne区别

    ECShop没有使用一些开源的数据库操作类,比如adodb或者PEAR,而是封装了自己的实现.这样做的好处是实现非常轻量,大大减小了分发包的文件大小.另外,当网站需要做memcached缓存时,也可以 ...

  3. API Management Architecture Notes

    Kong/Tyk/Zuul/strongloop/Ambassador/Gravitee IBM Reference Architecture for API Management: https:// ...

  4. wpa_cli 连接 wifi(转)

    wpa_cli 连接 wifi 转自:http://hi.baidu.com/yyangjjun/item/9dfe8e175439fc7a1009b5ba   1: run wpa_supplica ...

  5. Memcached安装以及PHP的调用

    Memcached安装以及PHP的调用 [南京·10月17日]OSC源创会开始报名:Swift.大型移动项目构架分享 » 一:安装libevent 由于memcached安装时,需要使用libeven ...

  6. Vs code 通用插件

    Vs code 通用插件 转自:https://segmentfault.com/a/1190000006697219 HTML Snippets 超级实用且初级的 H5代码片段以及提示 HTML C ...

  7. CM本地Yum源的搭建

    CM本地Yum源的搭建 以本地yum源安装CM5为例,解释本地yum源的安装和利用本地yum源安装CM5. Cloudera Manager 5(以下简称CM)默认采用在线安装的方式,给不能联互联网或 ...

  8. DLL编写中extern “C”和__stdcall的作用

    动态链接库的使用有两种方式,一种是显式调用.一种是隐式调用. (1)       显式调用:使用LoadLibrary载入动态链接库.使用GetProcAddress获取某函数地址. (2)      ...

  9. oracle autotrace

    --======================= -- 启用 AUTOTRACE功能 --======================= AUTOTRACE是一个SQL*Plus工具,用于跟踪SQL ...

  10. ubuntu 12.04下apache 配置家目录地址

    apache2 最在搞前端相关的东西,上一次也记录了 Linux 下 LAMP环境的搭建,现在记录一下如果改变 apache2 的家目录地址该怎么做,改那个配置文件 修改配置文件 /etc/apach ...