您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“OC中API庫(kù)常見函數(shù)有哪些”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“OC中API庫(kù)常見函數(shù)有哪些”這篇文章吧。
NSMutableArray 可變數(shù)組
//initWithCapacity: 創(chuàng)建一個(gè)數(shù)組
// - (instancetype)initWithCapacity:(NSUInteger)numItems
NSMutableArray * array = [[NSMutableArray alloc]initWithCapacity:1];//1表示剛開始給數(shù)組分配一個(gè)對(duì)象大小的內(nèi)存,當(dāng)數(shù)組已經(jīng)有一個(gè)對(duì)象時(shí),再放一個(gè)對(duì)象時(shí),數(shù)組會(huì)自動(dòng)添加1個(gè)對(duì)象大小的內(nèi)存,(n代表追加的內(nèi)存?zhèn)€數(shù)). 相同的對(duì)象可以被多次存入數(shù)組中
NSLog(@"%@", array);
addObject: 給數(shù)組中添加元素(對(duì)象)
- (void)addObject:(id)anObject
NSMutableArray * array = [[NSMutableArray alloc]initWithCapacity:1];
//1表示剛開始給數(shù)組分配一個(gè)對(duì)象大小的內(nèi)存,當(dāng)數(shù)組已經(jīng)有一個(gè)對(duì)象時(shí),再放一個(gè)對(duì)象時(shí),數(shù)組會(huì)自動(dòng)添加1個(gè)對(duì)象大小的內(nèi)存,(n代表追加的內(nèi)存?zhèn)€數(shù)). 相同的對(duì)象可以被多次存入數(shù)組中
Person * p1 = [[Person alloc]initWithName:@"張1" sex:@"男" age:71];
Person * p2 = [[Person alloc]initWithName:@"李2" sex:@"男" age:22];
Person * p3 = [[Person alloc]initWithName:@"王3" sex:@"女" age:63];
[array addObject:p1];[array addObject:p2];[array addObject:p1];
NSLog(@"%@", array);
//insertObject:atIndex:
[array insertObject:p2 atIndex:2];
NSLog(@"在下標(biāo)2插入p2%@", array);
//removeLastObject
[array removeLastObject];
NSLog(@"將最后元素刪除%@", array);
//removeObject:
[array removeObject:p2];
NSLog(@"將p2元素刪除%@", array);
//removeObject:inRange:
[array addObject:p2]; [array addObject:p3];
[array addObject:p1];
NSLog(@"添加后%@", array);
[array removeObject:p1 inRange:NSMakeRange(0, 4)];
NSLog(@"刪除給定個(gè)范圍內(nèi)出現(xiàn)的對(duì)象%@", array);
//removeObjectAtIndex:
[array removeObjectAtIndex:1];
NSLog(@"刪除給定下標(biāo)元素%@", array);
//replaceObjectAtIndex:withObject:
[array replaceObjectAtIndex:0 withObject:p3];
NSLog(@"替換后%@", array);
//exchangeObjectAtIndex:withObjectAtIndex:
[array addObject:p1];[array addObject:p2];
[array exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"交換下標(biāo)1和下標(biāo)2元素%@", array);
打印結(jié)果:
2015-01-15 22:53:04.514 OC 1月15號(hào)[3452:303] (
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.516 OC 1月15號(hào)[3452:303] 在下標(biāo)2插入p2(
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U674e2 \U7537 22",
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.517 OC 1月15號(hào)[3452:303] 將最后元素刪除(
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U674e2 \U7537 22"
)
2015-01-15 22:53:04.517 OC 1月15號(hào)[3452:303] 將p2元素刪除(
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.518 OC 1月15號(hào)[3452:303] 添加后(
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U738b3 \U5973 63",
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.556 OC 1月15號(hào)[3452:303] 刪除給定個(gè)范圍內(nèi)出現(xiàn)的對(duì)象(
"\U674e2 \U7537 22",
"\U738b3 \U5973 63"
)
2015-01-15 22:53:04.556 OC 1月15號(hào)[3452:303] 刪除給定下標(biāo)元素(
"\U674e2 \U7537 22"
)
2015-01-15 22:53:04.557 OC 1月15號(hào)[3452:303] 替換后(
"\U738b3 \U5973 63"
)
2015-01-15 22:53:04.557 OC 1月15號(hào)[3452:303] 交換下標(biāo)1和下標(biāo)2元素(
"\U738b3 \U5973 63",
"\U674e2 \U7537 22",
"\U5f201 \U7537 71"
)
Person * p1 = [[Person alloc]initWithName:@"張1" sex:@"男" age:71];
Person * p2 = [[Person alloc]initWithName:@"李2" sex:@"男" age:22];
Person * p3 = [[Person alloc]initWithName:@"王3" sex:@"女" age:63];
NSArray * array = [NSArray arrayWithObjects:p1,p2,p3,@"1111", @"222", nil];
NSLog(@"%@",array);
for (int i = 0; i < [array count]; i++) {
id obj = [array objectAtIndex:i];
//檢查對(duì)象是否是某個(gè)類的對(duì)象
// isKindOfClass
if ([obj isKindOfClass:[NSString class]]) {
NSString * newstr = [obj stringByAppendingString:@"藍(lán)鷗"];
NSLog(@"%@", newstr);
}
}
2015-01-15 22:57:53.527 OC 1月15號(hào)[3466:303] (
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U738b3 \U5973 63",
1111,
222
)
2015-01-15 22:57:53.529 OC 1月15號(hào)[3466:303] 1111藍(lán)鷗
2015-01-15 22:57:53.530 OC 1月15號(hào)[3466:303] 222藍(lán)鷗
//*************NSNumber***************
//NSNumber 數(shù)值類 把基本數(shù)值型轉(zhuǎn)換成對(duì)象性,或者是數(shù)值對(duì)象轉(zhuǎn)成基本型
//intValue 把對(duì)象轉(zhuǎn)化為數(shù)值.
// intValue 和 NSNumber numberWithInt: 是一對(duì)
int n = 5;
NSNumber * n1 = [NSNumber numberWithInt:n];
NSArray * array = [NSArray arrayWithObjects:n1,[NSNumber numberWithInt:10], nil];
NSLog(@"%@", array);
int result = [[array firstObject] intValue] + [[array lastObject] intValue];
NSLog(@"result = %d", result);
打印結(jié)果:
2015-01-15 22:59:36.037 OC 1月15號(hào)[3474:303] (
5,
10
)
2015-01-15 22:59:36.039 OC 1月15號(hào)[3474:303] result = 15
//NSDictionary 不可修改的字典
//1. 鍵值對(duì) 2. 字典無(wú)序 3. 存對(duì)象 4. 通過(guò)key索引對(duì)應(yīng)的值 5. key唯一
//initWithObjectsAndKeys:
NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"張三",@"name",@"男",@"sex",@"25",@"年齡", nil];
NSLog(@"%@", dic);
2015-01-15 23:00:33.258 OC 1月15號(hào)[3484:303] {
name = "\U5f20\U4e09";
sex = "\U7537";
"\U5e74\U9f84" = 25;
}
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name", @"M",@"sex", @"30", @"age", nil];
NSLog(@"%@", dic);
//dictionaryWithObjects:forKeys: 類方法,將值和鍵分別對(duì)應(yīng),返回一個(gè)字典
//+ (instancetype)dictionaryWithObjects:(NSArray *)objectsforKeys:(NSArray *)keys
NSArray * arrayValue = [NSArray arrayWithObjects:@"zhangsan",@"30",@"W", nil];
NSArray * keyArray = [NSArray arrayWithObjects:@"name", @"age", @"sex", nil];
NSDictionary * dic1 = [NSDictionary dictionaryWithObjects:arrayValue forKeys:keyArray];
NSLog(@"%@", dic1);
//count //得到鍵值對(duì)的個(gè)數(shù)
NSLog(@"%ld",[dic1 count]);
//allKeys 得到所有的鍵
NSLog(@"%@",[dic1 allKeys]);
//allValues 得到所有的值
NSLog(@"%@", [dic1 allValues]);
打印結(jié)果:
2015-01-15 23:01:17.213 OC 1月15號(hào)[3492:303] {
age = 30;
name = zhangsan;
sex = M;
}
2015-01-15 23:01:17.216 OC 1月15號(hào)[3492:303] {
age = 30;
name = zhangsan;
sex = W;
}
2015-01-15 23:01:17.216 OC 1月15號(hào)[3492:303] 3
2015-01-15 23:01:17.217 OC 1月15號(hào)[3492:303] (
name,
age,
sex
)
2015-01-15 23:01:17.217 OC 1月15號(hào)[3492:303] (
zhangsan,
30,
W
)
//**************************objectForKey: 重要***********************************//
//objectForKey: 根據(jù)Key來(lái)索引數(shù)據(jù)
//- (id)objectForKey:(id)aKey
NSArray * arrayValue = [NSArray arrayWithObjects:@"zhangsan",@"30",@"W", nil];
NSArray * keyArray = [NSArray arrayWithObjects:@"name", @"age", @"sex", nil];
NSDictionary * dic1 = [NSDictionary dictionaryWithObjects:arrayValue forKeys:keyArray];
NSString * str = [dic1 objectForKey:@"name"];
NSLog(@"%@", str);
//遍歷字典,取出字典所有的值
for (int i = 0; i < [dic1 count]; i++) {
NSString * key = [[dic1 allKeys] objectAtIndex:i];
NSString * value = [dic1 objectForKey:key];
NSLog(@"遍歷1 %@", value);
}
//遍歷2
NSArray * array = [dic1 allValues];
for (int i= 0; i < [dic1 count]; i++) {
NSString * str = array[i];
NSLog(@"遍歷2 %@", str);
}
2015-01-15 23:05:29.028 OC 1月15號(hào)[3524:303] zhangsan
2015-01-15 23:05:29.030 OC 1月15號(hào)[3524:303] 遍歷1 zhangsan
2015-01-15 23:05:29.031 OC 1月15號(hào)[3524:303] 遍歷1 30
2015-01-15 23:05:29.031 OC 1月15號(hào)[3524:303] 遍歷1 W
2015-01-15 23:05:29.032 OC 1月15號(hào)[3524:303] 遍歷2 zhangsan
2015-01-15 23:05:29.032 OC 1月15號(hào)[3524:303] 遍歷2 30
2015-01-15 23:05:29.032 OC 1月15號(hào)[3524:303] 遍歷2 W
//************************** NSMutableDictionary*********************
// NSMutableDictionary
// dictionaryWithCapacity: 創(chuàng)建一個(gè)可以修改內(nèi)容的字典
// 一個(gè)Key對(duì)應(yīng)一個(gè)value
// 一個(gè)value可以對(duì)應(yīng)多個(gè)key
NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:1];
NSLog(@"dic = %@", dic);
//setObject:forKey: 往字典里添加鍵值對(duì)
//- (void)setObject:(id)anObjectforKey:(id<NSCopying>)aKey
[dic setObject:@"zhangsan" forKey:@"name"];
[dic setObject:@"zhangsi" forKey:@"name"];//鍵一樣時(shí) , 會(huì)將鍵指的內(nèi)容替換掉
[dic setObject:@"20" forKey:@"value1"];
[dic setObject:@"20" forKey:@"value2"];
NSLog(@"%@", dic);
//removeObjectForKey:
[dic removeObjectForKey:@"value2"];
NSLog(@"%@", dic);
// [dic removeAllObjects];
// NSLog(@"%@", dic);
NSArray * array = [NSArray arrayWithObjects:@"value1",@"name" , nil];
[dic removeObjectsForKeys:array]; //移除多個(gè)鍵對(duì)應(yīng)的值
NSLog(@"%@", dic);
2015-01-15 23:08:49.741 OC 1月15號(hào)[3538:303] dic = {
}
2015-01-15 23:08:49.743 OC 1月15號(hào)[3538:303] {
name = zhangsi;
value1 = 20;
value2 = 20;
}
2015-01-15 23:08:49.744 OC 1月15號(hào)[3538:303] {
name = zhangsi;
value1 = 20;
}
2015-01-15 23:08:49.744 OC 1月15號(hào)[3538:303] {
}
//*************************OC之二維數(shù)組***********************//
NSArray * firstArray = [NSArray arrayWithObjects:@"111", @"222", @"333", @"444", nil];
NSArray * secondArray = [NSArray arrayWithObjects:@"AAA", @"BBB", @"CCC", nil];
NSArray * thirdArray = [NSArray arrayWithObjects:@"name", @"sex", @"age", nil];
NSArray * bigArray = [NSArray arrayWithObjects:firstArray, secondArray,thirdArray, nil];
NSLog(@"%@", bigArray);
// 1 遍歷數(shù)組(兩層循環(huán), 數(shù)組里面套數(shù)組)
for (int i = 0; i < [bigArray count]; i++) {
for (int j = 0; j < [[bigArray objectAtIndex:i] count]; j++) {
id a = [[bigArray objectAtIndex:i] objectAtIndex:j];
NSLog(@"遍歷數(shù)組之結(jié)果:%@", a);
}
}
2015-01-15 23:09:37.710 OC 1月15號(hào)[3546:303] (
(
111,
222,
333,
444
),
(
AAA,
BBB,
CCC
),
(
name,
sex,
age
)
)
2015-01-15 23:09:37.713 OC 1月15號(hào)[3546:303] 遍歷數(shù)組之結(jié)果:111
2015-01-15 23:09:37.713 OC 1月15號(hào)[3546:303] 遍歷數(shù)組之結(jié)果:222
2015-01-15 23:09:37.713 OC 1月15號(hào)[3546:303] 遍歷數(shù)組之結(jié)果:333
2015-01-15 23:09:37.714 OC 1月15號(hào)[3546:303] 遍歷數(shù)組之結(jié)果:444
2015-01-15 23:09:37.714 OC 1月15號(hào)[3546:303] 遍歷數(shù)組之結(jié)果:AAA
2015-01-15 23:09:37.715 OC 1月15號(hào)[3546:303] 遍歷數(shù)組之結(jié)果:BBB
2015-01-15 23:09:37.715 OC 1月15號(hào)[3546:303] 遍歷數(shù)組之結(jié)果:CCC
2015-01-15 23:09:37.715 OC 1月15號(hào)[3546:303] 遍歷數(shù)組之結(jié)果:name
2015-01-15 23:09:37.716 OC 1月15號(hào)[3546:303] 遍歷數(shù)組之結(jié)果:sex
2015-01-15 23:09:37.716 OC 1月15號(hào)[3546:303] 遍歷數(shù)組之結(jié)果:age
// 2 數(shù)組里面套字典
NSDictionary * stuDic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan", @"name", @"M", @"sex",@"30", @"age", nil];
NSDictionary * stuDic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"lisi", @"name", @"M", @"sex", @"25", @"age", nil];
NSArray * array = [NSArray arrayWithObjects:stuDic1, stuDic2, nil];
NSLog(@"%@", array);
for (int i = 0; i < [array count]; i++) {
for (int j = 0; j < [[array objectAtIndex:i] count]; j++) {
id result = [[[array objectAtIndex:i]allValues] objectAtIndex:j];
NSLog(@"遍歷后:%@", result);
}
}
2015-01-15 23:10:27.425 OC 1月15號(hào)[3557:303] (
{
age = 30;
name = zhangsan;
sex = M;
},
{
age = 25;
name = lisi;
sex = M;
}
)
2015-01-15 23:10:27.427 OC 1月15號(hào)[3557:303] 遍歷后:zhangsan
2015-01-15 23:10:27.427 OC 1月15號(hào)[3557:303] 遍歷后:M
2015-01-15 23:10:27.428 OC 1月15號(hào)[3557:303] 遍歷后:30
2015-01-15 23:10:27.428 OC 1月15號(hào)[3557:303] 遍歷后:lisi
2015-01-15 23:10:27.429 OC 1月15號(hào)[3557:303] 遍歷后:M
2015-01-15 23:10:27.429 OC 1月15號(hào)[3557:303] 遍歷后:25
<<3>>字典里放數(shù)組
NSArray * firstArray = [NSArray arrayWithObjects:@"111", @"222", @"333", @"444", nil];
NSArray * secondArray = [NSArray arrayWithObjects:@"AAA", @"BBB", @"CCC", nil];
NSArray * thirdArray = [NSArray arrayWithObjects:@"name", @"sex", @"age", nil];
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:firstArray, @"first",secondArray, @"second",thirdArray, @"third", nil];
NSLog(@"%@", dic);
for (int i = 0; i < [dic count]; i++) {//外層循環(huán)遍歷字典,
for (int j = 0; j < [[[dic allValues]objectAtIndex:i] count]; j++) {//內(nèi)層循環(huán)遍歷小數(shù)組
id a = [[[dic allValues] objectAtIndex:i] objectAtIndex:j];//取出數(shù)組里的內(nèi)容
NSLog(@"遍歷后%@", a);
}
}
2015-01-15 23:11:20.271 OC 1月15號(hào)[3565:303] {
first = (
111,
222,
333,
444
);
second = (
AAA,
BBB,
CCC
);
third = (
name,
sex,
age
);
}
2015-01-15 23:11:20.274 OC 1月15號(hào)[3565:303] 遍歷后111
2015-01-15 23:11:20.274 OC 1月15號(hào)[3565:303] 遍歷后222
2015-01-15 23:11:20.275 OC 1月15號(hào)[3565:303] 遍歷后333
2015-01-15 23:11:20.275 OC 1月15號(hào)[3565:303] 遍歷后444
2015-01-15 23:11:20.275 OC 1月15號(hào)[3565:303] 遍歷后AAA
2015-01-15 23:11:20.276 OC 1月15號(hào)[3565:303] 遍歷后BBB
2015-01-15 23:11:20.276 OC 1月15號(hào)[3565:303] 遍歷后CCC
2015-01-15 23:11:20.277 OC 1月15號(hào)[3565:303] 遍歷后name
2015-01-15 23:11:20.277 OC 1月15號(hào)[3565:303] 遍歷后sex
2015-01-15 23:11:20.277 OC 1月15號(hào)[3565:303] 遍歷后age
// 4 字典里套子典
NSDictionary * stuDic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan", @"name", @"M", @"sex",@"30", @"age", nil];
NSDictionary * stuDic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"lisi", @"name", @"M", @"sex", @"30", @"age", nil];
NSDictionary * bigDic = [NSDictionary dictionaryWithObjectsAndKeys:stuDic1,@"first", stuDic2, @"second", nil];
NSLog(@"%@", bigDic);
for (int i = 0; i < [bigDic count]; i++) {
//外層循環(huán)控制大字典鍵值對(duì)數(shù)
for (int j = 0; j < [[[bigDic allValues]objectAtIndex:i] count]; j++) {
//內(nèi)層循環(huán)控制小字典鍵值對(duì)數(shù)!!!注意要先取出大字典的值集( allValue ),用objiectAtIndex: 得出小字典,再求小字典的值集,用objiectAtIndex: 求出具體內(nèi)容
id a = [[[[bigDic allValues]objectAtIndex:i] allValues] objectAtIndex:j];
NSLog(@"遍歷字典(里套小字典)后%@", a);
}
}
2015-01-15 23:12:36.630 OC 1月15號(hào)[3573:303] {
first = {
age = 30;
name = zhangsan;
sex = M;
};
second = {
age = 30;
name = lisi;
sex = M;
};
}
2015-01-15 23:12:36.632 OC 1月15號(hào)[3573:303] 遍歷字典(里套小字典)后zhangsan
2015-01-15 23:12:36.633 OC 1月15號(hào)[3573:303] 遍歷字典(里套小字典)后M
2015-01-15 23:12:36.633 OC 1月15號(hào)[3573:303] 遍歷字典(里套小字典)后30
2015-01-15 23:12:36.634 OC 1月15號(hào)[3573:303] 遍歷字典(里套小字典)后lisi
2015-01-15 23:12:36.634 OC 1月15號(hào)[3573:303] 遍歷字典(里套小字典)后M
2015-01-15 23:12:36.634 OC 1月15號(hào)[3573:303] 遍歷字典(里套小字典)后30
以上是“OC中API庫(kù)常見函數(shù)有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。