这篇文章主要为大家展示了“OC中API库常见函数有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“OC中API库常见函数有哪些”这篇文章吧。
NSMutableArray 可变数组
//initWithCapacity: 创建一个数组
// - (instancetype)initWithCapacity:(NSUInteger)numItems
NSMutableArray * array = [[NSMutableArray alloc]initWithCapacity:1];//1表示刚开始给数组分配一个对象大小的内存,当数组已经有一个对象时,再放一个对象时,数组会自动添加1个对象大小的内存,(n代表追加的内存个数). 相同的对象可以被多次存入数组中
NSLog(@"%@", array);
addObject: 给数组中添加元素(对象)
- (void)addObject:(id)anObject
NSMutableArray * array = [[NSMutableArray alloc]initWithCapacity:1];
//1表示刚开始给数组分配一个对象大小的内存,当数组已经有一个对象时,再放一个对象时,数组会自动添加1个对象大小的内存,(n代表追加的内存个数). 相同的对象可以被多次存入数组中
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(@"在下标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(@"删除给定个范围内出现的对象%@", array);
//removeObjectAtIndex:
[array removeObjectAtIndex:1];
NSLog(@"删除给定下标元素%@", array);
//replaceObjectAtIndex:withObject:
[array replaceObjectAtIndex:0 withObject:p3];
NSLog(@"替换后%@", array);
//exchangeObjectAtIndex:withObjectAtIndex:
[array addObject:p1];[array addObject:p2];
[array exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"交换下标1和下标2元素%@", array);
打印结果:
2015-01-15 22:53:04.514 OC 1月15号[3452:303] (
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.516 OC 1月15号[3452:303] 在下标2插入p2(
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U674e2 \U7537 22",
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.517 OC 1月15号[3452:303] 将最后元素删除(
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U674e2 \U7537 22"
)
2015-01-15 22:53:04.517 OC 1月15号[3452:303] 将p2元素删除(
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.518 OC 1月15号[3452:303] 添加后(
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U738b3 \U5973 63",
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.556 OC 1月15号[3452:303] 删除给定个范围内出现的对象(
"\U674e2 \U7537 22",
"\U738b3 \U5973 63"
)
2015-01-15 22:53:04.556 OC 1月15号[3452:303] 删除给定下标元素(
"\U674e2 \U7537 22"
)
2015-01-15 22:53:04.557 OC 1月15号[3452:303] 替换后(
"\U738b3 \U5973 63"
)
2015-01-15 22:53:04.557 OC 1月15号[3452:303] 交换下标1和下标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];
//检查对象是否是某个类的对象
// isKindOfClass
if ([obj isKindOfClass:[NSString class]]) {
NSString * newstr = [obj stringByAppendingString:@"蓝鸥"];
NSLog(@"%@", newstr);
}
}
2015-01-15 22:57:53.527 OC 1月15号[3466:303] (
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U738b3 \U5973 63",
1111,
222
)
2015-01-15 22:57:53.529 OC 1月15号[3466:303] 1111蓝鸥
2015-01-15 22:57:53.530 OC 1月15号[3466:303] 222蓝鸥
//*************NSNumber***************
//NSNumber 数值类 把基本数值型转换成对象性,或者是数值对象转成基本型
//intValue 把对象转化为数值.
// intValue 和 NSNumber numberWithInt: 是一对
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);
打印结果:
2015-01-15 22:59:36.037 OC 1月15号[3474:303] (
5,
10
)
2015-01-15 22:59:36.039 OC 1月15号[3474:303] result = 15
//NSDictionary 不可修改的字典
//1. 键值对 2. 字典无序 3. 存对象 4. 通过key索引对应的值 5. key唯一
//initWithObjectsAndKeys:
NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"张三",@"name",@"男",@"sex",@"25",@"年龄", nil];
NSLog(@"%@", dic);
2015-01-15 23:00:33.258 OC 1月15号[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: 类方法,将值和键分别对应,返回一个字典
//+ (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 //得到键值对的个数
NSLog(@"%ld",[dic1 count]);
//allKeys 得到所有的键
NSLog(@"%@",[dic1 allKeys]);
//allValues 得到所有的值
NSLog(@"%@", [dic1 allValues]);
打印结果:
2015-01-15 23:01:17.213 OC 1月15号[3492:303] {
age = 30;
name = zhangsan;
sex = M;
}
2015-01-15 23:01:17.216 OC 1月15号[3492:303] {
age = 30;
name = zhangsan;
sex = W;
}
2015-01-15 23:01:17.216 OC 1月15号[3492:303] 3
2015-01-15 23:01:17.217 OC 1月15号[3492:303] (
name,
age,
sex
)
2015-01-15 23:01:17.217 OC 1月15号[3492:303] (
zhangsan,
30,
W
)
//**************************objectForKey: 重要***********************************//
//objectForKey: 根据Key来索引数据
//- (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号[3524:303] zhangsan
2015-01-15 23:05:29.030 OC 1月15号[3524:303] 遍历1 zhangsan
2015-01-15 23:05:29.031 OC 1月15号[3524:303] 遍历1 30
2015-01-15 23:05:29.031 OC 1月15号[3524:303] 遍历1 W
2015-01-15 23:05:29.032 OC 1月15号[3524:303] 遍历2 zhangsan
2015-01-15 23:05:29.032 OC 1月15号[3524:303] 遍历2 30
2015-01-15 23:05:29.032 OC 1月15号[3524:303] 遍历2 W
//************************** NSMutableDictionary*********************
// NSMutableDictionary
// dictionaryWithCapacity: 创建一个可以修改内容的字典
// 一个Key对应一个value
// 一个value可以对应多个key
NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:1];
NSLog(@"dic = %@", dic);
//setObject:forKey: 往字典里添加键值对
//- (void)setObject:(id)anObjectforKey:(id<NSCopying>)aKey
[dic setObject:@"zhangsan" forKey:@"name"];
[dic setObject:@"zhangsi" forKey:@"name"];//键一样时 , 会将键指的内容替换掉
[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]; //移除多个键对应的值
NSLog(@"%@", dic);
2015-01-15 23:08:49.741 OC 1月15号[3538:303] dic = {
}
2015-01-15 23:08:49.743 OC 1月15号[3538:303] {
name = zhangsi;
value1 = 20;
value2 = 20;
}
2015-01-15 23:08:49.744 OC 1月15号[3538:303] {
name = zhangsi;
value1 = 20;
}
2015-01-15 23:08:49.744 OC 1月15号[3538:303] {
}
//*************************OC之二维数组***********************//
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 遍历数组(两层循环, 数组里面套数组)
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(@"遍历数组之结果:%@", a);
}
}
2015-01-15 23:09:37.710 OC 1月15号[3546:303] (
(
111,
222,
333,
444
),
(
AAA,
BBB,
CCC
),
(
name,
sex,
age
)
)
2015-01-15 23:09:37.713 OC 1月15号[3546:303] 遍历数组之结果:111
2015-01-15 23:09:37.713 OC 1月15号[3546:303] 遍历数组之结果:222
2015-01-15 23:09:37.713 OC 1月15号[3546:303] 遍历数组之结果:333
2015-01-15 23:09:37.714 OC 1月15号[3546:303] 遍历数组之结果:444
2015-01-15 23:09:37.714 OC 1月15号[3546:303] 遍历数组之结果:AAA
2015-01-15 23:09:37.715 OC 1月15号[3546:303] 遍历数组之结果:BBB
2015-01-15 23:09:37.715 OC 1月15号[3546:303] 遍历数组之结果:CCC
2015-01-15 23:09:37.715 OC 1月15号[3546:303] 遍历数组之结果:name
2015-01-15 23:09:37.716 OC 1月15号[3546:303] 遍历数组之结果:sex
2015-01-15 23:09:37.716 OC 1月15号[3546:303] 遍历数组之结果:age
// 2 数组里面套字典
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号[3557:303] (
{
age = 30;
name = zhangsan;
sex = M;
},
{
age = 25;
name = lisi;
sex = M;
}
)
2015-01-15 23:10:27.427 OC 1月15号[3557:303] 遍历后:zhangsan
2015-01-15 23:10:27.427 OC 1月15号[3557:303] 遍历后:M
2015-01-15 23:10:27.428 OC 1月15号[3557:303] 遍历后:30
2015-01-15 23:10:27.428 OC 1月15号[3557:303] 遍历后:lisi
2015-01-15 23:10:27.429 OC 1月15号[3557:303] 遍历后:M
2015-01-15 23:10:27.429 OC 1月15号[3557:303] 遍历后:25
<<3>>字典里放数组
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++) {//外层循环遍历字典,
for (int j = 0; j < [[[dic allValues]objectAtIndex:i] count]; j++) {//内层循环遍历小数组
id a = [[[dic allValues] objectAtIndex:i] objectAtIndex:j];//取出数组里的内容
NSLog(@"遍历后%@", a);
}
}
2015-01-15 23:11:20.271 OC 1月15号[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号[3565:303] 遍历后111
2015-01-15 23:11:20.274 OC 1月15号[3565:303] 遍历后222
2015-01-15 23:11:20.275 OC 1月15号[3565:303] 遍历后333
2015-01-15 23:11:20.275 OC 1月15号[3565:303] 遍历后444
2015-01-15 23:11:20.275 OC 1月15号[3565:303] 遍历后AAA
2015-01-15 23:11:20.276 OC 1月15号[3565:303] 遍历后BBB
2015-01-15 23:11:20.276 OC 1月15号[3565:303] 遍历后CCC
2015-01-15 23:11:20.277 OC 1月15号[3565:303] 遍历后name
2015-01-15 23:11:20.277 OC 1月15号[3565:303] 遍历后sex
2015-01-15 23:11:20.277 OC 1月15号[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++) {
//外层循环控制大字典键值对数
for (int j = 0; j < [[[bigDic allValues]objectAtIndex:i] count]; j++) {
//内层循环控制小字典键值对数!!!注意要先取出大字典的值集( allValue ),用objiectAtIndex: 得出小字典,再求小字典的值集,用objiectAtIndex: 求出具体内容
id a = [[[[bigDic allValues]objectAtIndex:i] allValues] objectAtIndex:j];
NSLog(@"遍历字典(里套小字典)后%@", a);
}
}
2015-01-15 23:12:36.630 OC 1月15号[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号[3573:303] 遍历字典(里套小字典)后zhangsan
2015-01-15 23:12:36.633 OC 1月15号[3573:303] 遍历字典(里套小字典)后M
2015-01-15 23:12:36.633 OC 1月15号[3573:303] 遍历字典(里套小字典)后30
2015-01-15 23:12:36.634 OC 1月15号[3573:303] 遍历字典(里套小字典)后lisi
2015-01-15 23:12:36.634 OC 1月15号[3573:303] 遍历字典(里套小字典)后M
2015-01-15 23:12:36.634 OC 1月15号[3573:303] 遍历字典(里套小字典)后30
以上是“OC中API库常见函数有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。