oc小结

来源:互联网 发布:大学英语b网络统考作弊 编辑:程序博客网 时间:2024/05/30 23:45

数组:

1.复习不可变数组与可变数组的具体方法实现。

2.初级封装:

(1)数字封装,封装后还原成数字。

int a = 100;

NSNumber *num = [NSNumber numberWithInt:a];

(2)字符串封装,封装后还原字符串。

[int b = num intValue];

字典:

1.字典定义,初始化方法(需注意系统保存格式为key:value,但初始化顺序为value, key);

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"One", @"1", @"Two", @"2", @"Three", @"3", nil];

   *字典中的元素是以键值对的形式存储的。

     键(key)  和      值(value)

     1                       One

     2                       Two

     3                       Three

   *字典可以粗浅的说为是以字符串为下标的数组

   *值(value)  键(key)

2.获取字典内value的方法;

例如我要获取第三个键值对,也就是key = 3;

NSLog(@"%@", [dict objectForKey:@"3"]);

3.字典内对象个数[dic count](dic为当前字典指针名称)

NSLog(@"%@", [dict count]);

4.把全部keys保存在一个数组中,遍历字典value;

NSArray *array = [dict allKeys];

NSLog(@"%@", array);

5.遍历字典时的语法糖:

NSLog(@"%@ : %@", key[i], dic[keys[i]]); = NSLog(@"%@ : %@", [keys objectAtIndex:i], [dic objectForKey:[keys objectAtIndex:i]]);

注意多练习后面的正式写法。

6.字典存取位置不是按照顺序排序,因此无法按照顺序输出字典,获取方法只能通过key查找value。需注意的是字典内不可以存在同名的key。

a)不可变字典(NSDictionary)

1.枚举法遍历字典(分别遍历value和key)

2.快速枚举法,找到想要的value

b)可变字典(NSMutableDictionary)

初始化:NSMutableDictionary *dict = [[NSMutabelDictionary alloc] init];

1.可变字典添加元素

[dict setObject:@"One" forKey@"1"];

2.可变字典删除指定键值对

[dict removeObjectForKey:@"2"];

3.可变字典删除所有键值对

[dict removeAllObjects];

集合:

1.集合的定义,初始化方法。(注意:集合当中的元素,每个只能有一个)

NSSet *set = [[NSSet alloc] initWithObjects:@"One", @"Two", @"Three", nil];

2.判断集合中是否存在某个对象;

判断One是否存在于集合中,若存在,输出contain,若不存在,输出not contain

if ([set containsObject:@"One"]){

        NSLog(@"contain");

} else {

        NSLog(@"not contain");

}

3.集合内对象个数[set count](set为当前集合指针名称)

[set count];

4.将集合内所有的对象添加到数组中,遍历输出数组。

NSArray *array = [set allObjects];

NSLog(@"%@", array);


a)不可变数组(NSSet)

1.集合的对象个数

[set count];

2.判断集合中是否存在某个对象

判断One是否存在于集合中,若存在,输出contain,若不存在,输出not contain

if ([set containsObject:@"One"]){

        NSLog(@"contain");

} else {

        NSLog(@"not contain");

}

3.判断连个集合是否相等,若相等,输出equal,若不相等,输出not equal

创建一个新的集合,命名为set2.

NSSet *set2 = [[NSSet alloc] initWithObjects:@"One", @"Two", @"Three", nil];

if ([set isEqual:set2]){

        NSLog(@"equal");

} else {

        NSLog(@"not equal");

}




4.判断第二个集合是否是第一个集合的子集合,判断一个集合是否是另一个集合的自己和,判断的时集合内的元素

if ([set2 isSubsetOfSet:set]){

       NSLog(@"yes");

} else {

     NSLog(@"no");

}

5.用数组创建集合

NSArray *array = [[NSArray alloc] initWithObjects:@"One", @"Two", @"Three", nil];

NSSet *set = [[NSSet alloc] initWithArray:array];

6.把集合内容取出来,生成一个数组

NSArray *array1 = [set allObjects];



b)可变数组(NSMutableSet)是NSSet的子集

NSMutableSet *set = [[NSMutableSet alloc] initWithObjects:@"One", @"Two", @"Three", @"Four", nil];

1.动态添加/删除元素

将Five这个元素添加到集合中

[set addObject:@"Five"];

2.将第二个集合的元素添加到第一个集合中

定义第二个集合(无论是可变集合还是不可变集合)

NSSet *set1 = [[NSSet alloc] initWithObjects:@"Five", @"Six", @"Seven", nil];

[set unionSet:set1];

3.在原集合中删除第二个集合中的所有元素,包括和第一个集合重复的元素

[set minusSet:set1];




NSIndexSet  指数集合/索引集合(NSIndexSet装得都是数字,是数字的集合)

a)不可变(NSIndexSet)

1.建立一个数组,从某个位置开始,输出到lenth长度的元素(NSRange);

NSIndexSet *indexset = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(2, 3)];

NSArray *array = [[NSArray alloc] initWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", nil];

NSArray *array1 = [array objectsAtIndexes:indexset];

NSLog(@"%@", array1);

b)可变(NSMutableINdexSet)

1.输出指定位置的数组的元素;

NSMutableIndexSet *indexset = [[NSMutableIndexSet alloc] init];

[indexset addIndex:0];

[indexset addIndex:3];

[indexset addIndex:5];

NSArray *array = [[NSArray alloc] initWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", nil];

NSArray *array1 = [array objectsAtIndexes.:indexset];

NSLog(@"%@", array1);


0 0
原创粉丝点击