1 數字對象
1. NSNumber:數字對象創建如下:
NSNumber *num, intNum, floatNum;
NSInteger myInt;
//integer
intNum = [NSNumber numberWithInteger:10];
myInt = [intNum integerValue];
NSLog("%li",(long)myInt );
//float
floatNum = [NSNumber numberWithFloat:10.11];
NSLog("%g",[floatNum floatValue]);
//char
num = [NSNumber numberWithChar:'a'];
NSLog("%c",[num charValue]);
//double
num = [NSNumber numberWithDouble:1234e+15];
NSLog("%lg",[num doubleValue]);
//判斷數字是否相等
if( [num isEqualToNumber:floatNum]==YES ){}
//判斷數字大小
if( [num compare:floatNum]==NSOrderedAscending ) //順序
NSInteger和NSUInteger不是NS對象,而是typedef。NSInteger是64位long或32位int。
2. 初始化方式
2 字符串對象
NSString *str = @”hello world”;
NSLog("%@",str);
NSString 使用@符號表示,並使用”%@”格式化輸出,還可以格式化輸出其他對象。
3 description方法
“%@”格式化其實是調用的NSObject的description方法,如果沒有覆寫description方法,只能打印對象的類名和內存地址,NSString已經覆寫了description方法。
4 不可變字符串
<span style="font-weight: normal;">NSString *str1 = @"hello world A";
NSString *str2 = @"hello world B";
NSString *res;
NSRange *range;
//字符串的長度
NSLog("%lu",[str1 length]);
//字符串複製到另一個
res = [NSString stringWithString:str1];
NSLog("%@",res);
//添加字符串到末尾
str2 = [str1 stringByAppendingString:str2];
//判斷是否相等
if([str1 isEqualToString:str2]==YES)
//轉換爲大寫
res = [str1 uppercaseString];
NSLog("%@", [res UTF8String]);NSString *str1 = @"hello world A";
NSString *str2 = @"hello world B";
NSString *res;
//字符串的長度
NSLog("%lu",[str1 length]);
//字符串複製到另一個
res = [NSString stringWithString:str1];
NSLog("%@",res);
//添加字符串到末尾
str2 = [str1 stringByAppendingString:str2];
//判斷是否相等
if([str1 isEqualToString:str2]==YES)
//轉換爲大寫
res = [str1 uppercaseString];
NSLog("%@", [res UTF8String]);
//從字符串提取
res = [str1 substringToIndex:3];
//從index往後截取
res = [str1 substringFromIndex:5];
//截取8到13
res = [str1 substringWithRange:NSMakeRange(8,6)];
//查找字符串 獲取NSRange對象
range = [str1 rangeOfString:@"hello"]</span>
5 可變字符串
NSMutableString是可變的字符串,繼承自NSString。
NSMutableString *mstr;
//創建可變字符串
mstr = [NSMutableString stringWithString:@"hello"];
//插入字符串
[mstr insertString:@"world" atIndex:[mstr length];
//末尾拼接
[mstr appendString:"world"];
//根據範圍刪除字符串
[mstr deleteCharactersInRange:NSMakeRange(3,3)];
//設置字符串
[mstr setString:@"hello world"];
//替換字符串
[mstr replaceCharactersInRange:NSMakeRange(1,1) withString:@"1"];
6 數組NSArray
NSArray *week = [NSArray arrayWithObjects:@"mon",@"tue",@"wed",@"thu",@"fri",@"sat",@"sun"];
for(int i=0; i<7; i++){
NSLog("%@",[week objectAtIndex:i]);
}
7 可變數組
NSMutableArray *nums = [NSMutalbeArray array];
NSNumber *number;
for(int i=0; i<10; i++){
number = [nums addObject:[NSNumber numberWithInteger:i]];
}
8 詞典對象dictionary
詞典對象類似Java的Map,如下:
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:@"字母A" forKey:@"a"];
[dictionary setObject:@"字母B" forKey:@"b"];
NSLog("%@",[dictionary objectForKey:@"a"]);
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectAndKeys:
@"字母A",@"A",@"字母B",@"B"];
for(NSString *key in dictionary){
NSLog("%@",[dictionary objectForKey:key]);
}
9 SET對象
#define INTOBJ(v) [NSNumber numberWithInteger:v];
NSMutalbeSet *set1 = [NSMutableSet setWithObjects:INTOBJ(1),INTOBJ(2),INTOBJ(3),nil];
NSMutalbeSet *set2 = [NSMutableSet setWithObjects:INTOBJ(4),INTOBJ(5),INTOBJ(6),nil];
//是否相等
if( [set1 isEqualToSet:set2]==YES )
//是否包含
if( [set1 containsObject:INTOBJ(10)]==YES )
//添加和刪除
[set1 addObject:INTOBJ(20)];
[set1 removeObject:INTOBJ(1)];
//獲取交集
[set1 intersectSet:set2]
//合併
[set1 unionSet:set2];