iPhone 开发 (十一)Foundation kit 基础

来源:互联网 发布:制作二代身份证软件 编辑:程序博客网 时间:2024/05/22 19:53

1、字符串

----------------------------------------------------------------------------------


1.1 创建字符串

NNString 的stringWithFormat:方法就是这样通过格式字符串和参数来创建NNString的:

      + (id)stringWithFormat :(NSString *) format,….;

你可以按如下方式创建一个新的字符串

   NNString * height;

    height = [NNString stringWithFormat:@"You height is %d feed, %d inches",5,11];

 得到的字符串是“ You height is %d feed,%d inches”;


1.2 类方法

 stringWithFormat; 的声明中有两个值得讨论的地方。第一个是定义最后的省路号,他告诉我们和编译器这个方法可以接受多个逗号隔开的其他的参数。


1.3 不区分大小写的比较


compare: 进行的是区分大小写的比较。换句话说: @“Bork”和 @"bork" 的比较是不会返回 NSOrderedSame 的。我们还有一个办法 compare :options : 


例如:如果你想进行字符串的比较,要忽略大小写但按字符个数的多少正确排序,那么应该这么做:

  if([ thing1 compare:thing2

                  options: NSCaseinsensitiveSearch

                                | NSNumericSearch]

                                   == NSOrderedSame){

                         NSLog(@"They match!");             

}


----------------------------------------------------------------------------------

2、字符串的可变性


NNString 是不可变的,Cocoa 提供了一个NNString 的子类,叫做NSMutableString 。改变字符串使用这个类。


你可以使用类方法stringWithCapacity:来创建一个新的NSMutableString,声明如下:

 + (id) stringWithCapacity :(unsigned) capacity;

---------------------------------


3、集合家族

-----------------------------------------------------------------------------------

Cocoa 提供了许多的集合类,如NSArray 和 NSDictionary,他们的实例就是为了存储其他对象而存在的


NSArray有两个限制:1、只能存储Objective-C的对象;2、不能在NSArray中存储nil


可以通过类方法arrayWithObjects:创建一个新的NSArray。发生一个以逗号分割的对象列表,在列表结尾添加nil代表列表结束(现在知道NSArray 第二个限制的原因了吧)


NSArray *array;

array = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];


-(unsigned) count;   //获得包含对象的个数


-(id) objectAtIndex:(unsigned int) index; //获得特定索引处的对象


// 输出数组的内容

int i;

 for(i = 0; i<[array count]; i ++){

      NSLog(@"index %d has %@.",i,[array objectAtIndex: i ]);

}


4、可变数组

--------------------------------------------------------

与NSString 一样,NSArray 创建的是不可变对象的数组,但是它的补充类中出现了一个NSMutableArray

+(id) arrayWithCapacity : (unsigned) numItems; 


NSMutableArray *array;

array = [NSMutableArray arrayWithCapacity: 17];    //使用arrayWithCapacity创建可变数组


- (void)addObject: (id) anObject;   //在数组末尾添加对象

for(i = 0; i< 4 ; i++){

    Tire *tire =[Tire new];

    [array addObject: tire]

}    //使用循环代码为数组添加4个轮胎


- (void)  removeObjectAtIndex :(unsigned) index;


5、快速枚举

-----------------------------------

    for (NSString *string in array){

    NSLog(@"I found %@",string);  

}


6、NSDictionary

---------------------------------------------------------------

在编程中,字典 就是关键字及其定义的集合。cocoa 中有一个实现这种功能的集合类NSDictionary。

和前面一样,NSDictionary 也提供了NSMutableDictionary 类,允许随意添加和删除字典元素。

+(id)dictionaryWithObjectsAndKeys;

               (id) fristObject,…..;

可以按照下面的这种方式创建字典


     Tire *t1 = [Tire new];

     Tire *t2 = [Tire new];

     Tire *t3 = [Tire new];

     Tire *t4 = [Tire new];

      NSDictionary *tires;

      

     tires = [NSDictionary dictionaryWithObjectsAndKeys:

             t1,@"front-left", t2,@"front-right",t3,@"back-left",t4,@"back-right",nil ];

使用方法objectForKey: 来获取字典的值,向方法传递之前用来存储该值的关键字:

-(id)objectForKey :(id)key;

所以要查找后轮胎可以这样写:

Tire *tire =[tires objectForKey:@"back-left"];


 可以使用setObject; (id)  anObject forKey: (id) aKey;

下面是另一种创建存储轮胎的字典的方法:

NSMutableDictionary *tires;

tires = [NSMutableDictionary dictionary];


[tires setObject:t1 forKey: @"front-left"];

[tires setObject:t2 forKey: @"front-right"];

[tires setObject:t3 forKey: @"backfront-left"];

[tires setObject:t4 forKey: @"back-right"];


7、各种数值

----------------------------------------------------------------

NSNumber

例; 创建NSNumber 之后,你可以把它放入一个字典或数组中;


NSNumber *number;

number = [NSNumber numberWithInt:42];

[array addObject:number];

[dictionary setObject;number  forKey:@"Bork"];


NSValue

NSNumber 实际上是NSValue的子类,NSValue可以包装任意值。


NSNull


--------------------------------

内容来源与Learn Objective-C on the Mac

---------------------------------

原创粉丝点击