object-c 四

Foundation framework classes

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

NSArray

 
 
  1. main.m 
  2.  
  3. #import 
  4.  
  5. #import 
  6.  
  7. #import 
  8.  
  9. #import 
  10.  
  11. #import 
  12.  
  13. void print( NSArray *array ) { 
  14.  
  15. NSEnumerator *enumerator = [array objectEnumerator]; 
  16.  
  17. id obj; 
  18.  
  19. while ( obj = [enumerator nextObject] ) { 
  20.  
  21. printf( "%s\n", [[obj description] cString] ); 
  22.  
  23.  
  24.  
  25. int main( int argc, const char *argv[] ) { 
  26.  
  27. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
  28.  
  29. NSArray *arr = [[NSArray alloc] initWithObjects: 
  30.  
  31. @"Me", @"Myself", @"I", nil]; 
  32.  
  33. NSMutableArray *mutable = [[NSMutableArray alloc] init]; 
  34.  
  35. // enumerate over items 
  36.  
  37. printf( "----static array\n" ); 
  38.  
  39. print( arr ); 
  40.  
  41. // add stuff 
  42.  
  43. [mutable addObject: @"One"]; 
  44.  
  45. [mutable addObject: @"Two"]; 
  46.  
  47. [mutable addObjectsFromArray: arr]; 
  48.  
  49. [mutable addObject: @"Three"]; 
  50.  
  51. // print em 
  52.  
  53. printf( "----mutable array\n" ); 
  54.  
  55. print( mutable ); 
  56.  
  57. // sort then print 
  58.  
  59. printf( "----sorted mutable array\n" ); 
  60.  
  61. [mutable sortUsingSelector: @selector( caseInsensitiveCompare: )]; 
  62.  
  63. print( mutable ); 
  64.  
  65. // free memory 
  66.  
  67. [arr release]; 
  68.  
  69. [mutable release]; 
  70.  
  71. [pool release]; 
  72.  
  73. return 0; 
  74.  

output

 
 
  1. ----static array 
  2.  
  3. Me 
  4.  
  5. Myself 
  6.  
  7.  
  8. ----mutable array 
  9.  
  10. One 
  11.  
  12. Two 
  13.  
  14. Me 
  15.  
  16. Myself 
  17.  
  18.  
  19. Three 
  20.  
  21. ----sorted mutable array 
  22.  
  23.  
  24. Me 
  25.  
  26. Myself 
  27.  
  28. One 
  29.  
  30. Three 
  31.  
  32. 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.  
  3. #import 
  4.  
  5. #import 
  6.  
  7. #import 
  8.  
  9. #import 
  10.  
  11. #import <<> 
  12.  
  13. #import 
  14.  
  15. void print( NSDictionary *map ) { 
  16.  
  17. NSEnumerator *enumerator = [map keyEnumerator]; 
  18.  
  19. id key; 
  20.  
  21. while ( key = [enumerator nextObject] ) { 
  22.  
  23. printf( "%s => %s\n", 
  24.  
  25. [[key description] cString], 
  26.  
  27. [[[map objectForKey: key] description] cString] ); 
  28.  
  29.  
  30.  
  31. int main( int argc, const char *argv[] ) { 
  32.  
  33. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
  34.  
  35. NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: 
  36.  
  37. @"one", [NSNumber numberWithInt: 1], 
  38.  
  39. @"two", [NSNumber numberWithInt: 2], 
  40.  
  41. @"three", [NSNumber numberWithInt: 3], 
  42.  
  43. nil]; 
  44.  
  45. NSMutableDictionary *mutable = [[NSMutableDictionary alloc] init]; 
  46.  
  47. // print dictionary 
  48.  
  49. printf( "----static dictionary\n" ); 
  50.  
  51. print( dictionary ); 
  52.  
  53. // add objects 
  54.  
  55. [mutable setObject: @"Tom" forKey: @"tom@jones.com"]; 
  56.  
  57. [mutable setObject: @"Bob" forKey: @"bob@dole.com" ]; 
  58.  
  59. // print mutable dictionary 
  60.  
  61. printf( "----mutable dictionary\n" ); 
  62.  
  63. print( mutable ); 
  64.  
  65. // free memory 
  66.  
  67. [dictionary release]; 
  68.  
  69. [mutable release]; 
  70.  
  71. [pool release]; 
  72.  
  73. return 0; 
  74.  
  75.  

output

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

优点与缺点

优点

Cateogies

Posing

动态识别

指标计算

弹性讯息传递

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

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

缺点

不支援命名空间

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

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值