IOS开发笔记(8)Foundation Kit 学习

来源:互联网 发布:伤感网络歌曲排行榜 编辑:程序博客网 时间:2024/06/15 09:34


http://blog.csdn.net/xiaofansong/article/details/8423630




//“==”运算符只判断两个字符串的指针数值,而不是它们所指的对象,以下例子请区别:

NSString *thing1 = @"hello,8";

NSString *thing2 = [NSString stringWithFormat:

@"hello,%d",8];

if ([thing1 isEqual: thing2]){

NSLog(@"Yes,they are the same.");

else {

NSLog(@"NO,they are not the same.");

}

if (thing1 == thing2){

NSLog(@"thing1 == thing2");

else{

NSLog(@"thing1 != thing2");

}

输出:

2012-12-24 09:42:36.345 Kit学习[459:403] Yes,they are the same.

2012-12-24 09:42:36.345 Kit学习[459:403] thing1 != thing2

2.集合
集合类:NSArray 、NSDictionary

1)NSArray
NSArray是Cocoa的一个类,用来存储对象的有序列表,可以在NSArray中放入任意类型的对象。
NSArray中只能存储Objective-C的对象,而不能存储C语言中的基本数据类型,如int,float,enum,struct,或者NSArray中的随机指针。同时,也不能在NSArray中存储nil(对象的零值或NULL值)。

[plain] view plaincopyprint?
  1. // 创建数组 arrayWithObjects:  
  2.        NSMutableArray *array;  
  3.        array = [NSArray arrayWithObjects:  
  4.                 @"one", @"two", @"three", nil]; //列表结尾添加nil表示列表结束  
  5.          
  6.        // 获取对象个数 count:  
  7.        NSLog(@" the count of array is: %ld",[array count]);  
  8.          
  9.        // 获取特定索引处的对象 objectAtIndex:  
  10.        int i;  
  11.        for (i = 0 ; i < [array count]; i++)  
  12.        {  
  13.            NSLog(@"index %d has %@",  
  14.                  i,[array objectAtIndex:i]);  
  15.        }  
  16.          
  17.        // 将字符串切分成数组 componentsSeparatedByString:  
  18.        NSString *str = @"oop : ack : elf : com : cn : net";  
  19.        NSArray *arr = [str componentsSeparatedByString:@" : "];  
  20.        NSLog(@"str is : %@",str);  
  21.        NSLog(@"arr is :");  
  22.        for (NSString *s in arr)  
  23.        {  
  24.            NSLog(@"\"%@\"",s);  
  25.        }  
  26.        // 合并数组并创建字符串 componentsJoinedByString:  
  27.        NSLog(@"合并数组到NSArray.");  
  28.        str = [arr componentsJoinedByString:@" $"];  
  29.        NSLog(@"str is : %@",str);  
  30.        NSLog(@"arr is :");  
  31.        for (NSString *s in arr)  
  32.        {  
  33.            NSLog(@"\"%@\"",s);  
  34.        }  

输出结果

2012-12-24 10:22:56.310 Kit学习[688:403] index 0 has one

2012-12-24 10:22:56.311 Kit学习[688:403] index 1 has three

2012-12-24 10:22:56.312 Kit学习[688:403]  the count of array is: 3

2012-12-24 10:22:56.312 Kit学习[688:403] index 0 has one

2012-12-24 10:22:56.313 Kit学习[688:403] index 1 has two

2012-12-24 10:22:56.313 Kit学习[688:403] index 2 has three

2012-12-24 10:22:56.314 Kit学习[688:403] str is : oop : ack : elf : com : cn : net

2012-12-24 10:22:56.314 Kit学习[688:403] arr is :

2012-12-24 10:22:56.315 Kit学习[688:403] "oop"

2012-12-24 10:22:56.315 Kit学习[688:403] "ack"

2012-12-24 10:22:56.315 Kit学习[688:403] "elf"

2012-12-24 10:22:56.316 Kit学习[688:403] "com"

2012-12-24 10:22:56.316 Kit学习[688:403] "cn"

2012-12-24 10:22:56.317 Kit学习[688:403] "net"

2012-12-24 10:22:56.317 Kit学习[688:403]合并数组到NSArray.

2012-12-24 10:22:56.318 Kit学习[688:403] str is : oop $ack $elf $com $cn $net

2012-12-24 10:22:56.318 Kit学习[688:403] arr is :

2012-12-24 10:22:56.333 Kit学习[688:403] "oop"

2012-12-24 10:22:56.334 Kit学习[688:403] "ack"

2012-12-24 10:22:56.334 Kit学习[688:403] "elf"

2012-12-24 10:22:56.335 Kit学习[688:403] "com"

2012-12-24 10:22:56.335 Kit学习[688:403] "cn"

2012-12-24 10:22:56.336 Kit学习[688:403] "net"


0 0
原创粉丝点击