Foundation Kit介紹

一些有用的數(shù)據(jù)類型

1:NSRange
typedef struct _NSRange
{
    unsigned int location;
    unsigned int length;
} NSRange;
location字段存放該范圍的起始位置,而length字段則是該范圍內(nèi)所含元素的個(gè)數(shù)。比如"Objective-C is a cool language"中單詞cool可以用location為17,Length為4的范圍來表示。

創(chuàng)建NSRange 3種方式:
1:直接給字段賦值
NSRange range;
range.location = 17;
range.length = 4;

2:應(yīng)用C語言的聚合結(jié)構(gòu)賦值機(jī)制
NSRange range = {17 , 4};

3:Cocoa提供的一個(gè)快捷函數(shù)NSMakeRange():
NSRange range = NSMakeRange (17 , 4);
使用NSMakeRange好處是可以在任何能夠使用函數(shù)地方使用:
[anObject flarbulateWithRange:NSMakeRange (13 , 15) ];

2:CGPoint 表示的是笛卡爾平面中的一個(gè)坐標(biāo)(x,y)
struct CGPoint 
{
    float x;
    float y;
};
創(chuàng)建數(shù)據(jù)類型的快捷函數(shù):CGPointMake()

3:CGSize 用來存儲(chǔ)長(zhǎng)度和寬度
struct CGSize
{
    float width;
    float height;
}
創(chuàng)建數(shù)據(jù)類型的快捷函數(shù):CGSizeMake()

4:矩形數(shù)據(jù)類型,它由坐標(biāo)和大小復(fù)合而成
struct CGRect
{
    CGPoint origin;
    CGSize  size;
};
創(chuàng)建數(shù)據(jù)類型的快捷函數(shù):CGRectMake()

字符串NSString

1:創(chuàng)建字符串
+(id)stringAWithFormat:(NSString *) format,...;
NSString *height = [NSString stringWithFormat:@"Your is %d",5];

2:類方法
類方法的方法屬于類對(duì)象,通常用于創(chuàng)建新的實(shí)例。我們稱這種用來創(chuàng)建新對(duì)象的類方法為工廠方法。

3:關(guān)于大小:length
length 返回的是字符串中的字符個(gè)數(shù)。  - (NSUInteger)length;
NSUInteger length = [height length];

4:字符串比較 
isEqualToString:返回一個(gè)BOOL值來表示兩個(gè)字符串是否相同。
compare: 聲明如下:- (NSComparisonResult) compare:(NSString *) aString;
compare將接受對(duì)象和傳遞過來的字符串逐個(gè)比較,返回一個(gè)NSComparisonResult 來顯示比較結(jié)果。
enum 
{ 
    NSOrderedAscending = -1,
    NSOrderedSame,
    NSOrderedDescending
};
typedef NSInteger NSComparisonResult;

compare:options  給我們更多的選擇權(quán)

可變字符串NSMutableString

創(chuàng)建:+(id)stringWithCapacity:(NSUInteger)capacity;
NSMutableString *string = [NSMutableString stringWithCapacity:42];
附加新字符串:
- (void)appendString: (NSString *)aString;接受然后復(fù)制到對(duì)象末尾
- (void)appendFormat:(NSString *)format,...;將格式化的字符串附在了接受字符串的末尾
NSMutableString *string = [NSMutableString stringWithCapacity:50];
[string appendString:@"Hello there"];
[string appendFormat:@"Human %d!",39];  string == @"Hello there Human 39"
刪除字符串:- (void)deleteCharactersInRange:(NSRange) aRange;

集合大家族(NSArray和NSDictionary)

NSArray用來存儲(chǔ)對(duì)象的有序列表。你可以在NSArray中放入任意類型的對(duì)象:NSString、Car、Shape或者其他你想要存儲(chǔ)的對(duì)象,甚至可以是其他數(shù)組或字典對(duì)象。
NSArray類有兩個(gè)限制:
1:只能存儲(chǔ)Objective-C對(duì)象(不能存儲(chǔ)C語言基礎(chǔ)數(shù)據(jù)類型,如int、float、enum、struct和NSArray中的隨機(jī)指針)
2:不能在NSArray中存儲(chǔ)nil

2種方法創(chuàng)建新的NSArray。
NSArray *array = [NSArray arrayWithObjects:@"one",@"tw",nil];
NSArray *array2 = @[@"one",@"two"];

獲取它所包含的對(duì)象個(gè)數(shù):
- (NSUInteger)count;
獲取特定索引處的對(duì)象:
- (id)objectAtIndex:(NSUInteger)index;
id *myObject = array1[1];
for (NSInteger i = 0; i < [array count]; i++){
    NSLog (@"index %d has %@."i,[array objectAtIndex:i]);
    ==NSLog(@"index %d has %@."i,array[i]);
}

可變數(shù)組的創(chuàng)建:NSMutableArray

  • +(id)arrayWithCapacity:(NSUInteger) numItems;
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:17];
    注:沒有用來創(chuàng)建NSMutableArray 對(duì)象的字面量語法

在數(shù)組末尾添加對(duì)象:addObject

- (void)addObject:(id)anObject; 
[array addObject:tire];

刪除特定索引處的對(duì)象:-(void)removeObjectAtIndex:(NSUInteger)index;
[array removeObjectAtIndex:1]; 索引從0開始

枚舉

NSEnumerator *enumerator = [array objectEnumerator];
while (id thingie = [enumerator nextObject])
{
    NSLog (@"I found %@",thingie);
}

快速枚舉

for (NSString *string in array)
{  
    NSLog (@"I found %@",string);
}

NSArray中添加了通過代碼塊枚舉對(duì)象的方法:
- (void)enumerateObjectsUsingBlock:(void(^) (id obj,NSUInteger idx,BOOL *stop))block
[array enumerateObjectsUsingBlock:^(NSSting *string,NSUInteger index,BOOL *stop) {
NSLog (@"I found %@",string);
}];
注:為什么用代碼塊來代替快速枚舉,因?yàn)橥ㄟ^代碼塊可以讓循環(huán)操作并發(fā)執(zhí)行,而通過快速枚舉,執(zhí)行操作要一項(xiàng)項(xiàng)地線性完成。

NSDictionary 字典 是關(guān)鍵字及其定義的集合

NSDictionary能在給定的關(guān)鍵字(通常是一個(gè)NSString字符串)下存儲(chǔ)一個(gè)數(shù)值(可以是任何類型的Objective-C對(duì)象),字典也被稱為散列表或關(guān)聯(lián)數(shù)組,使用的是鍵查詢的優(yōu)化方式,不需要遍歷整個(gè)數(shù)組。

創(chuàng)建字典對(duì)象

  • 字面量語法:@{key:value,...}
  • dictionaryWithObjectsAndKeys: 先是存儲(chǔ)對(duì)象,然后是關(guān)鍵字
  • 舉例:
    NSDictionary *tires = @{@"front-left":t1,@"front-right":t2};
    NSDictionary *tires = [NSDictionary dictionaryWithObjectsAndKeys:ti,@"front-left",t2,@"front-right",nil];

訪問字典中的數(shù)值

  • -(id)objectForKey:(id)aKey;
  • tires[key];
  • 舉例
    Tire *tire = [tires objectForKey:@"front-left"];
    Tire *tire = tires[@"front-left"];

NSMutableDictionary創(chuàng)建

  • 向NSMutableDictionary類發(fā)送dictionary消息
  • +(id)dictionaryWithCapacity:(NSUInteger)numItems;
  • 注:與NSMutableArray一樣,沒有適用于NSMutableDictionary的字面量初始化語法
  • 舉例
    NSMutableDictionary *tires = [NSMutableDictionary dictionary];
    NSMutableDictionary *tires = [NSMutableDictionary dictionaryWithCapacity:17];

字典元素的添加和修改

  • -(void)setObject:(id)anObject forKey:(id)aKey;
  • 舉例
    [tires setObject:t1 forKey:@"front-left"];
    注:如果對(duì)字典中已有的關(guān)鍵字使用setObject:forKey:方法,那么這個(gè)方法將會(huì)用新值替換掉原有的數(shù)值。

刪除可變字典中的關(guān)鍵字

  • -(void)removeObjectForKey: (id) aKey;
  • 舉例
    [tires removeObjectForKey:@"front-left"];

其他數(shù)值

NSArray和NSDictionary只能存儲(chǔ)對(duì)象,而不能直接存儲(chǔ)任何基本類型的數(shù)據(jù),如int、float和struct。 可以通過用對(duì)象來封裝基本數(shù)值
如果你想使用對(duì)象來處理基本類型,就可以使用NSInteger和NSUInteger。這些類型也要針對(duì)32位和64位處理器對(duì)數(shù)值進(jìn)行統(tǒng)一處理。

NSNumber

Cocoa提供了NSNumber類來封裝(wrap,即以對(duì)象形式來實(shí)現(xiàn))基本數(shù)據(jù)類型。

創(chuàng)建NSNumber對(duì)象

  • +(NSNumber *) numberWithChar:(char) value;
  • +(NSNumber *) numberWithInt: (int) value;
  • +(NSNumber *) numberWithFloat:(float)value;
  • +(NSNumber *) numberWithBool:(BOOL) value;
    或通過字面量語法來創(chuàng)建這些對(duì)象
    NSNumber *number;
    number = @'X' ; //字符型
    number = @12345; //整型
    number =@12345ul; //無符號(hào)長(zhǎng)整型
    number = @12345ll; // long long
    number = @123.45f; //浮點(diǎn)型
    number = @123.45; //雙浮點(diǎn)型
    number = @YES; //布爾值

應(yīng)用(創(chuàng)建完NSNumber之后,可以把它放入一個(gè)字典或數(shù)組中)

NSNumber *number = @42;
[array addObject number];
[dictionary setObject:number forKey:@"Bork"];

封裝到NSNumber中后,可以通過下面的實(shí)例方法來重新獲得它:

  • -(char)charValue;
  • -(int) intValue;
  • -(float) floatValue;
  • -(BOOL)boolValue;
    • (NSString *)stringValue;
      將創(chuàng)建方法和提取方法搭配在一起使用是完成可以的。用numberWithFloat:創(chuàng)建的NSNumber對(duì)象可以用intValue方法來提取數(shù)值。NSNumber會(huì)對(duì)數(shù)據(jù)進(jìn)行適當(dāng)?shù)霓D(zhuǎn)換。
      注:通常將一個(gè)節(jié)本類型的數(shù)據(jù)封裝成對(duì)象的過程被稱為裝箱,從對(duì)象中提取基本類型的數(shù)據(jù)叫做開箱。Objective-C語言不支持自動(dòng)裝箱功能。

NSValue

NSNull

  • +(NSNull *) null;
  • 舉例
    [contact setObject:[NSNull null] forKey:@"home fax machine"];
    id homefax = [contact objectForKey:@"home fax machine"];
    if (homefax == [NSNull null])
    { //。。。確定沒有傳真機(jī)后的行為
    }
    [NSNull null] 總是返回一樣的數(shù)值,所以你可以使用運(yùn)算符==將該值與其他值進(jìn)行比較。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容