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



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值)。

  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.        }  
 // 创建数组 arrayWithObjects:
        NSMutableArray *array;
        array = [NSArray arrayWithObjects:
                 @"one", @"two", @"three", nil]; //列表结尾添加nil表示列表结束
        
        // 获取对象个数 count:
        NSLog(@" the count of array is: %ld",[array count]);
        
        // 获取特定索引处的对象 objectAtIndex:
        int i;
        for (i = 0 ; i < [array count]; i++)
        {
            NSLog(@"index %d has %@",
                  i,[array objectAtIndex:i]);
        }
        
        // 将字符串切分成数组 componentsSeparatedByString:
        NSString *str = @"oop : ack : elf : com : cn : net";
        NSArray *arr = [str componentsSeparatedByString:@" : "];
        NSLog(@"str is : %@",str);
        NSLog(@"arr is :");
        for (NSString *s in arr)
        {
            NSLog(@"\"%@\"",s);
        }
        // 合并数组并创建字符串 componentsJoinedByString:
        NSLog(@"合并数组到NSArray.");
        str = [arr componentsJoinedByString:@" $"];
        NSLog(@"str is : %@",str);
        NSLog(@"arr is :");
        for (NSString *s in arr)
        {
            NSLog(@"\"%@\"",s);
        }

输出结果

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值