ITeye博客

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

mmdev · 2012-12-24 10:19 · Markdown

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

NSString*thing1 =@"hello,8";

NSString*thing2 = [NSStringstringWithFormat:

@"hello,%d",8];

if([thing1isEqual: 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值)。

 // 创建数组 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)

暂无评论。

评论功能维护中,当前仅展示历史评论。