OC-字符串-NSComparisonResult

Foundation Kit

Cocoa由两个不同的框架组成 Foundation Kit和Application Kit

Foundation框架中有很多诸如NSString,NSArray等低级类和数据类型

 

 

Objective-c代码   收藏代码
  1. #import <Foundation/Foundation.h>  
  2. int main(int argc, const char *argv[]){  
  3.     NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];  
  4.     //insert code here...  
  5.     NSLog(@"Hello, World!");  
  6.       
  7.     [pool drain];  
  8.     return 0;  
  9. }  

 

通过alloc创建并通过init初始化了一个池,在结尾处排空,这是Cocoa内存管理的预览

 

一些有用的数据类型

 

范围 NSRange

 

Objective-c代码   收藏代码
  1. typedef struct _NSRange{  
  2.     unsigned int location;  
  3.     unsigned int length;  
  4. }NSRange;  

 

表示相关事物的范围,如字符串中的字符范围或数组中的元素范围

创建一个新的NSRange有3种方式

 

Objective-c代码   收藏代码
  1. //1  
  2. NSRange range;  
  3. range.location=17;  
  4. range.length=4;  
  5. //2  
  6. NSRange range={17,4};  
  7. //3  
  8. NSRange range=NSMakeRange(17,4);  

 

第三种方法的好处是可以在任何能够使用函数的地方使用,比如当作参数

 

Objective-c代码   收藏代码
  1. [anObject flarbulateWithRange: NSMakeRange(13,15)];  

 

 

几何数据类型 NSPoint,NSSize

 

Objective-c代码   收藏代码
  1. typedef struct _NSPoint{  
  2.     float x;  
  3.     float y;  
  4. }NSPoint;  
  5.   
  6. typedef struct _NSSize{  
  7.     float width;  
  8.     float height;  
  9. }NSSize;  

 

比如Cocoa提供了矩形数据类型

 

Objective-c代码   收藏代码
  1. typedef struct _NSRect{  
  2.     NSPoint origin;  
  3.     NSSize size;  
  4. }NSRect;  

 

同样提供了NSMakePoint(),NSMakeSize(),NSMakeRect()方法

将这些数据类型作为struct而不是对象的好处是性能更高

 

字符串 NSString

创建字符串

 

Objective-c代码   收藏代码
  1. NSString *height;  
  2. height=[NSString stringWithFormat: @"Your height is %d feet",5];  

 

类方法

我们所创建的大部分方法是实例方法 用前导减号 - 声明

如果方法用于实现常规功能,用前导加好 + 来声明类方法

就如NSString的stringWithFormat方法

 

Objective-c代码   收藏代码
  1. + (id) stringWithFormat: (NSString *) format, ...;  

 

关于大小

 

Objective-c代码   收藏代码
  1. - (unsigned int) length;  

 

使用方式

 

Objective-c代码   收藏代码
  1. unsigned int length=[height length];  

 

该方法可以正确处理国际字符串

比较

 

Objective-c代码   收藏代码
  1. isEqualToString  
  2. - (BOOL) isEqualToString: (NSString *) aString;  

 

使用方式

 

Objective-c代码   收藏代码
  1. NSString *thing1=@"hello 5";  
  2. NSString *thing2;  
  3. thing2=[NSString stringWithFormat: @"hello %d",5];  
  4. if(thing1 isEqualToString: thing2]){  
  5.     NSLog(@"They are the same!");  
  6. }  

 

同样的compare方法

 

Objective-c代码   收藏代码
  1. - (NSCompar isonResult) compare: (NSString *) string;  

 

返回一个NSComparisonResult枚举类型

Objective-c代码   收藏代码
  1. type enum _NSComparisonResult{  
  2.     NSOrderedAscending=-1,  
  3.     NSOrderedSame,  
  4.     NSOrderedDescending  
  5. }NSComparisonResult;  

如果返回NSOrderedAscending 表示左侧小于右侧 其他类似

不区分大小写的比较

Objective-c代码   收藏代码
  1. - (NSComparisonResult) compare: (NSString *) string  
  2.                         options: (unsigned) mask;  

options参数是一个掩码

NSCaseInsensitiveSearch 不区分大小写

NSLiteralSearch 完全比较,区分大小写

NSNumericSearch 比较字符个数而不是字符值 比如100应该排在99以后

Objective-c代码   收藏代码
  1. if([thing1 compare: thing2  
  2.         options: NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame){  
  3.             NSLog(@"They match");  
  4.         }  

判断字符串内是否包含其他字符串

Objective-c代码   收藏代码
  1. - (BOOL) hasPrefix: (NSString *) aString;  
  2. - (BOOL) hasSuffix: (NSString *) aString;  

分别检查以特定字符串开头和结尾

Objective-c代码   收藏代码
  1. - (NSRange) rangeOfString: (NSString *) aString;  

返回匹配的位置,如果找不到 则range.start=NSNotFound

 

可变性

NSString是不可变的

NSMutableString是可变字符串

两者间比较类似Java中的String和StringBuffer

创建NSMutableString的方法

Objective-c代码   收藏代码
  1. + (id) stringWithCapacity: (unsigned) capacity;  

该容量只是一个建议 

Objective-c代码   收藏代码
  1. NSMutableString *string;  
  2. string = [NSMutableString stringWithCapacity: 42];  

可以使用一些方法操作该string

Objective-c代码   收藏代码
  1. - (void) appendString: (NSString *) aString;  
  2. - (void) appendFormat: (NSString *) format, ...;  

使用起来非常方便 也很显而易见

Objective-c代码   收藏代码
  1. NSMutableString *string;  
  2. string=[NSMutableString stringWithCapacity: 50];  
  3. [string appendString: @"Hello here"];  
  4. [string appendFormat: @"human %d!",39];  
  5. //得到最后结果Hello here human 39!  

类似的

删除字符串中的字符

Objective-c代码   收藏代码
  1. - (void) deleteCharactersInRange: (NSRange) range;  

NSMutableString是NSString的子类,所以可以使用NSString的所有功能

因此同样可以使用stringWithFormat来创建NSMutableString

 

集合家族 NSArray NSDictionary等

NSArray可以放入任意类型的对象

两个限制

1 只能存储Objective-C对象,而不能是C基础类型int,float,enum,struct等

2 不能存储零值nil NULL值

可以通过类方法arrayWithObjects创建,以逗号分割对象列表,并最后以nil表示列表结束

Ojbective-c代码   收藏代码
  1. NSArray *array;  
  2. array=[NSArray arrayWithObjects: @"one",@"two",@"three",nil];  

获得对象个数

Objective-c代码   收藏代码
  1. - (unsigned) count;  

取得特定索引处对象

Objective-c代码   收藏代码
  1. - (id) objectAtIndex: (unsigned int) index;  

例如遍历一个数组

Objective-c代码   收藏代码
  1. int i;  
  2. for(i=0;i<[array count];i++){  
  3.     NSLog(@"index %d has %@",i,[array objectAtIndex: i]);  
  4. }  

字符串切分成数组

Objective-c代码   收藏代码
  1. -componentsSeparatedByString  

数组合并成字符串

Objective-c代码   收藏代码
  1. -componentsJoinedByString  

可变数组

NSArray是不可变的,类似的NSMutableArray可变

创建新的可变数组

Objective-c代码   收藏代码
  1. + (id) arrayWithCapacity: (unsigned) numItems;  

数组末尾添加对象

Objective-c代码   收藏代码
  1. - (void) addObject: (id) anObject;  

删除特定位置对象

Objective-c代码   收藏代码
  1. - (void) removeObjectAtIndex: (unsigned) index;  

枚举 NSEnumerator

通过objectEnumerator向数组请求枚举器

Objective-c代码   收藏代码
  1. - (NSEnumerator *) objectEnumerator;   

 这似乎类似于Java的迭代器Iterator

使用

Objective-c代码   收藏代码
  1. NSEnumerator *enumerator;  
  2. enumerator=[array objectEnumerator];  

可以从后向前浏览集合 reverseObjectEnumerator

请求下一个对象

Objective-c代码   收藏代码
  1. - (id) nextObject;  

返回nil时表示结束

快速枚举

Objective-c代码   收藏代码
  1. for(NSString *string in array){  
  2.     NSLog(@"I found %@",string);  
  3. }  
 

NSDictionary 有些类似于Map(散列表,关联数组)

类似的NSDictionary不可变,可变的NSMutableDictionary

创建字典的方法

Objective-c代码   收藏代码
  1. + (id) dictionaryWithObjectsAndKeys: (id) firstObject, ...;  

例如

Objective-c代码   收藏代码
  1. Tire *t1=[Tire new];  
  2. Tire *t2=[Tire new];  
  3. Tire *t3=[Tire new];  
  4. Tire *t4=[Tire new];  
  5.   
  6. NSDictionary *tires;  
  7.   
  8. tires=[NSDictionary dictionaryWithObjectsAndKeys: t1, @"front=left", t2, @"front-right", t3, @"back-left", t4, @"back-right", nil];  

使用objectForKey来获取值

Objective-c代码   收藏代码
  1. - (id) objectForKey: (id) aKey;  

例如查找右后轮胎

Objective-c代码   收藏代码
  1. Tire *tire=[tires objectForKey: @"back-right"];  

同样的,对于可变的字典

Objective-c代码   收藏代码
  1. + (id) dictionaryWithCapacity: (unsigned int) numItems;  

为可变字典添加元素

Objective-c代码   收藏代码
  1. - (void) setObject: (id) anObject forKey: (id) aKey;  

如果当前已有值,则新值会替代原有的值

删除

Objective-c代码   收藏代码
  1. - (void) removeObjectForKey: (id) aKey;  

使用但不扩展

不要自己去创建NSString,NSArray,NSDictionary的子类

 

各种数值

就和Java中对int,float等有Integer,Float等对象封装,Objectvie-C也提供了NSNumber的包装类

Objective-c代码   收藏代码
  1. + (NSNumber *) numberWithChar: (char) value;  
  2. + (NSNumber *) numberWithInt: (int) value;  
  3. + (NSNumber *) numberWithFloat: (float) value;  
  4. + (NSNumber *) numberWithBool: (BOOL) value;  

类似的还有long,long long等

例如将一个包装后的数据放入数组

Objective-c代码   收藏代码
  1. NSNumber *number;  
  2. number=[NSNumber numberWithInt: 42];  
  3. [array addObject: number];  
  4. [dictionary setObject: num forKey: @"Bork"];  

从包装类获取值

Objective-c代码   收藏代码
  1. - (char) charValue;  
  2. - (int) intValue;  
  3. - (NSString *) stringValue;  

 等

 

NSValue

NSNumber是NSValue的子类,NSValue可以包装任意值

Objective-c代码   收藏代码
  1. + (NSValue *) valuseWithBytes: (const void *) value  
  2.     objCType: (const char *) type;  

例如,将NSRect放入NSArray

Objective-c代码   收藏代码
  1. NSRect rect=NSMakeRect(1,2,3,4);  
  2.   
  3. NSValue *value;  
  4. value=[NSValue valueWithBytes: &rect  
  5.     objCType: @encode(NSRect)];  
  6. [array addObject: value];  

这里使用@encode编译器指令,它可以接受数据类型的名称并转化为合适的字符串

使用getValue取值

Objective-c代码   收藏代码
  1. - (void) getValue: (void *) value;  

传递的是存储该数值的变量地址

Objective-c代码   收藏代码
  1. value=[array objectAtIndex: 0];  
  2. [value getValue: &rect];  

Cocoa提供了常用的将struct型数据转换成NSValue的方法

Objective-c代码   收藏代码
  1. + (NSValue *) valueWithPoint: (NSPoint) point;  
  2. + (NSValue *) valueWithSize: (NSSize) size;  
  3. + (NSValue *) valueWithRect: (NSRect) rect;  
 

Objective-c代码   收藏代码
  1. - (NSPoint) pointValue;  
  2. - (NSSize) sizeValue;  
  3. - (NSRect) rectValue;  

例如,在NSArray中存储和检索NSRect

Objective-c代码   收藏代码
  1. value=[NSValue valueWithRect: rect];  
  2. [array addObject: value];  
  3. ...  
  4. NSRect anotherRect=[value rectValue];  
 

NSNull

之前提到nil在NSArray和NSDictionary中有特殊的含义,所以不能把nil放入其中,如果要真的表示没有,Objectvie-C提供了NSNull

使用[NSNull null]==来比较是否为空

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值