OC-整理

1.1不可变字符串NSString

//  使用实例方法创建NSString对象

        NSString *string1 = [[NSString alloc] initWithFormat:@"姓名41564564"];

        NSLog(@"%@", string1);

        NSString *string2 = [[NSString alloc] initWithFormat:@"机构名称: %@  成立时间: %d", @"蓝鸥", 2001];

        NSLog(@"%@", string2);

        //  使用类方法创建一个NNString对象

        NSString *string3 = [NSString stringWithFormat:@"姓名 :%@ 年龄 :%d", @"小王", 20];

        NSLog(@"%@", string3);

        //  直接赋值

        NSString *string4 = @"hello WORLD my baby";

        NSLog(@"%@", string4);

        //  获取字符串长度

        NSUInteger string1Length = [string1 length];

        NSLog(@"%ld", string1Length);

        //  判断字符串是否有指定前缀

        BOOL result1 = [string1 hasPrefix:@"姓名"];

        NSLog(@"%d", result1);

        //  后缀

        BOOL result2 = [string2 hasSuffix:@"2011"];

        NSLog(@"%d", result2);

        //  查找字符串位置

        NSRange range1 = [string2 rangeOfString:@"蓝鸥"];

        NSLog(@"location : %ld  length : %ld", range1.location, range1.length);

        //  分割字符串

        NSString *subStirng1 = [string2 substringFromIndex:3];

        NSLog(@"%@", subStirng1);

        NSString *subString2 = [string2 substringToIndex:3];

        NSLog(@"%@", subString2);

        NSRange range2 = NSMakeRange(10, 2);// 建一个NSRange的结构体,注意越界

        NSString *subString3 = [string2 substringWithRange:range2];

        NSLog(@"%@", subString3);

        //  拼接

        NSString *string5 = [string2 stringByAppendingString: @"hello"];

        NSLog(@"%@", string5);

        //  替换

        NSString *string6 = [string2 stringByReplacingOccurrencesOfString:@"蓝鸥" withString:@"海鸥"];

        NSLog(@"%@", string6);

        NSRange range3 = NSMakeRange(3, 3);

        NSString *string7 = [string2 stringByReplacingCharactersInRange:range3 withString:@"HAHA 你中招了"];

        NSLog(@"%@", string7);

        //  比较

        //  比较是否相同

        BOOL isEqual = [string4 isEqualToString:@"hello"];

        NSLog(@"%d", isEqual);

        //  比较大小

        NSComparisonResult result3 = [string2 compare:string4];

        NSLog(@"%ld", result3);

        //  大写<->小写

        NSString *string8 = [string4 lowercaseString];

        NSLog(@"%@", string8);

        NSString *string9 = [string4 uppercaseString];

        NSLog(@"%@", string9);

        NSString *string10 = [string4 capitalizedString];

        NSLog(@"%@", string10);

        //  字符串和数值类型转换

        NSInteger intStr = [@"124" integerValue];

        NSLog(@"%ld", intStr);

        

        double doubleStr = [@"1000.00432" doubleValue];

        NSLog(@"%lf", doubleStr);

 1.2 可变字符串NSMutableString

 //   创建可变字符串

        NSMutableString *mutableStr1 = [[NSMutableString alloc] initWithFormat:@"蓝鸥科技有限公司"];

        NSMutableString *mutableStr2 = [NSMutableString stringWithFormat:@"蓝鸥科技有限公司"];

        NSLog(@"%@, %@", mutableStr1, mutableStr2);

        

        //  字符串拼接(在原串的基础上)

        [mutableStr1 appendString:@".txt"];

        NSLog(@"%@", mutableStr1);

        

        //  插入(在下标的后一位插入)

        [mutableStr1 insertString:@"3G" atIndex:2];

        NSLog(@"%@", mutableStr1);

        

        //  删除

        [mutableStr1 deleteCharactersInRange:NSMakeRange(2, 2)];

        NSLog(@"%@", mutableStr1);

     2.1 不可变数组NSArray

 NSArray数组类(不可变)

        //  1.使用实例方法创建数组

        NSArray *array1 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];//  nil不能省

        NSLog(@"%@", array1); // 可以直接打印数组

        

        //  2.使用类方法创建数组

        NSArray *array2 = [NSArray arrayWithObjects:@"4", @"5", @"6", nil];

        NSLog(@"%@", array2);

        

        //  3.获取数组元素个数

        NSUInteger count = [array1 count];

        NSLog(@"%ld", count);

        

        //  4.根据索引值获取对象(根据方法的返回值来确定用什么类型来接收)

        NSString *p = [array1 objectAtIndex:1];

        NSLog(@"%@", p);

        

        //  5.获取对象在数组中的索引值

        NSUInteger index = [array1 indexOfObject:@"3"];

        NSLog(@"index : %ld", index);

       1.2可变数组 NSMutableArray

        //  1. 使用实例方法创建

        NSMutableArray *mutableArray1 = [[NSMutableArray alloc] initWithObjects:@"aa",@"bb", @"cc", nil];

        NSLog(@"%@", mutableArray1);

        

        //  2. 使用类方法创建

        NSMutableArray *mutableArray2 = [NSMutableArray arrayWithObjects:@"aaa",@"bbb", @"ccc", nil];

        NSLog(@"%@", mutableArray2);

        

        //  3. 添加元素

        [mutableArray1 addObject:@"dd"];

        NSLog(@"%@", mutableArray1);

        

        //  4. 插入元素

        [mutableArray1 insertObject:@"haha" atIndex:1];

        NSLog(@"%@", mutableArray1);

        

        //  5. 删除元素(通常根据索引或者对象值)

        [mutableArray1 removeObject:@"haha"];

        [mutableArray1 removeObjectAtIndex:1];

        NSLog(@"%@", mutableArray1);

        

        //  6. 替换元素

        [mutableArray1 replaceObjectAtIndex:2 withObject:@"DD"];

        NSLog(@"%@", mutableArray1);

        

        //  7. 根据索引值交换指定位置的两个元素

        [mutableArray1 exchangeObjectAtIndex:1 withObjectAtIndex:2];

        NSLog(@"%@", mutableArray1);

        

        //  8. 很据对象交换两个元素的位置(先找到对象的索引值)

        [mutableArray1 exchangeObjectAtIndex: [mutableArray1 indexOfObject:@"cc"] withObjectAtIndex:[mutableArray1 indexOfObject:@"DD"]];

        NSLog(@"%@", mutableArray1);

        

        //  9. 遍历数组对象

使用for循环遍历

        for (int i = 0; i < [mutableArray1 count]; i ++) {

            NSString *str = [mutableArray1 objectAtIndex:i];

            NSLog(@"遍历结果:%@", str);

        }

使用for in快速遍历

        for (NSString *str in mutableArray1) {

            NSLog(@"快速遍历:%@", str);

        }

数组中使用枚举器(for in 快速遍历的实质)

        NSArray *arr = [NSArray arrayWithObjects:@"abc", @"hdf", @"hshah", nil];

        NSEnumerator *enumerator2 = [arr objectEnumerator];

        NSString *str2 = nil;

        while (str2 = [enumerator2 nextObject]) {

            NSLog(@"str2: %@", str2);

        }

 

 

3.NSNumber (基本数据类型不能放在数组中,解决这个问题,可以把数字封装成对象)

        //  1.创建一个数组对象

        NSMutableArray *array = [NSMutableArrayarrayWithObjects:@"1", @"2", nil];

        //  2.用NSNumber对Integer进行包装

        NSNumber *intNumber = [NSNumber numberWithInteger:3];

        //  3.将包装后烦人NSNumber对象放到数组中

        [array addObject:intNumber];

        NSLog(@"%@", array);

        //  4.根据下标取出对象,用NSNumber类型进行接收

        NSNumber *a = [array objectAtIndex:2];

        NSLog(@"%@", a);

        //  5.把NSNunber转成Integer类型

        NSInteger integerA = [a integerValue];

        NSLog(@"%ld", integerA);

4.1 NSDictionary 不可变字典

        //  key通过哈希算法算出一个数作为一个索引下标把值存到相应的的位置在内存中是散列结构,即无序

        //  使用实例方法创建(键值对的形式) 键和值都必须是对象

        NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", @"v3", @"k3", @"v5", @"k5", @"v4", @"k4", @"value6", @"key6", @"value7", @"ke7", nil];

        NSLog(@"%@", dictionary);

        

        //  使用类方法创建

        NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];

        NSLog(@"%@", dictionary2);

        

        NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];

        NSLog(@"%@", dictionary3);

        

        NSArray *keyArray = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil]; //  存放键的数组

        NSArray *valueArray = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];//  存放值的数组

        NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjects: keyArray forKeys:valueArray];

        NSLog(@"%@", dictionary4);

        

        // 创建空字典

        NSDictionary *dic = [NSDictionary dictionary];

        

        //  使用一个文件创建字典对象 新建文件步骤:command + N -> Resource -> Property List

        NSDictionary *dictionary5 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lanou3g/Desktop/OC/lesson5-20140425/lesson5-20140425/dictionary.plist"];

        NSLog(@"%@", dictionary5);

        

        //  返回字典中键值对的个数

        NSLog(@"%ld", [dictionary5 count]);

        

        //  字典取值 获取指定key对应的value

        NSString *value1 = [dictionary5 objectForKey:@"ly"];

        NSLog(@"%@", value1);

        

        //  返回所有的key数组

        NSArray *allKeysArray = [dictionary5 allKeys];

        NSLog(@"%@", allKeysArray);

        

        //  返回所有的value数组

        NSArray *allValuesArray = [dictionary5 allValues];

        NSLog(@"%@", allValuesArray);

        

        //  使用key枚举器(获取所有key)

        NSEnumerator *enumerator = [dictionary5 keyEnumerator];

        NSString *str = nil;

        while (str = [enumerator nextObject]) {

            NSLog(@"%@", str);

        }

 4.2创建可变字典 NSMutableDictionary

        NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];

        NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v3", @"k3", @"v4", @"k4", @"v5", @"k5", nil];

        

        //  用于整理对象的拼接

        [dic1 addEntriesFromDictionary:dic2];

        NSLog(@"%@", dic1);

        

        //  删除字典中某个对象

        [dic1 removeObjectForKey:@"k1"];

        NSLog(@"%@", dic1);

        

        //  删除字典全部对象

        [dic1 removeAllObjects];

        NSLog(@"%@", dic1);

        

        //  设置字典

        [dic1 setDictionary:dic2];

        NSLog(@"%@", dic1);

 5.1NSSet集合对象 容器类

        //  1. 使用类方法创建对象

        NSSet *set1 = [NSSet set];  //  创建一个空的集合对象

        NSSet *set2 = [NSSet setWithObject:@"abc"];

        NSSet *set3 = [NSSet setWithObjects:@"abc", @"aaa", @"bbb", nil];

        NSLog(@"%@", set3);

        

        NSArray *array = [NSArray arrayWithObjects:@"a",@"b", @"c", nil];

        NSSet *set4 = [NSSet setWithArray:array];   //  使用数组创建

        NSLog(@"%@", set4);

        

        NSSet *set5 = [NSSet setWithSet:set4];      //  使用集合创建

        NSLog(@"%@", set5);

        

        //  2.使用实例方法创建

        NSSet *set6 = [[NSSet alloc] init];

        NSLog(@"%@", set6);

        NSSet *set7 = [[NSSet alloc] initWithObjects:@"hello", @"hhaa", @"bbjdh", nil];

        NSLog(@"%@", set7);

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

        NSLog(@"%@", set8);

        NSSet *set9 = [[NSSet alloc] initWithSet:set7];

        NSLog(@"%@", set9);

        

        //  3.返回几个元素个数

        NSLog(@"%ld", [set7 count]);

        

        //  4.枚举器访问集合元素

        NSEnumerator *enumerator = [set7 objectEnumerator];

        NSString *str = nil;

        while (str = [enumerator nextObject]) {

            NSLog(@"%@", str);

        }

        

        //  5.判断两个几个是否有交集

        BOOL ifhasIntersection = [set2 intersectsSet:set3];

        NSLog(@"%d", ifhasIntersection);

        

        //  6.判断两个集合是否相等

        NSLog(@"%d", [set2 isEqualToSet:set3]);

        

        //  7.判断当前集合是否是子集

        NSLog(@"%d", [set2 isSubsetOfSet:set3]);

 

5.2可变集合 NSMutableSet

        //  创建指定元素个数的一个集合对象

        NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:4];

        [mutableSet addObject:@"aaa"];

        NSLog(@"%@", mutableSet);

        //  类方法创建可变集合

        NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"aaa", @"bbb", @"ccc", nil];

        NSMutableSet *mutableSet2 = [NSMutableSet setWithObject:@"aaa"];

        

        //  添加一个对象到集合

        [mutableSet2 addObject:@"ddd"];

        NSLog(@"%@", mutableSet2);

        

        //  从集合中删除一个对象

        [mutableSet2 removeObject:@"ddd"];

        NSLog(@"%@", mutableSet2);

        

        //  把数组对象添加到集合对象中

        NSArray *arr = [NSArray arrayWithObjects:@"eee", @"fff", nil];

        [mutableSet1 addObjectsFromArray:arr];

        NSLog(@"%@", mutableSet1);

 

        //  得到两个集合的交集 注意intersectSet和intersectsSet的区别,后者是判断是否有交集的方法, 返回的是bool值

        [mutableSet1 intersectSet:mutableSet2];

        NSLog(@"%@", mutableSet1);

        

        //  从一个集合中减去另一个集合

        [mutableSet1 minusSet:mutableSet2];

        NSLog(@"%@", mutableSet1);

        

        //  从一个元素中删除所有元素

        [mutableSet2 removeAllObjects];

        NSLog(@"%@", mutableSet2);

 

        //  取两个集合的并集

        [mutableSet1 unionSet:mutableSet2];

        NSLog(@"%@", mutableSet1);

        NSLog(@"%@", mutableSet1);

 

        //  设置给集合赋值

        [mutableSet1 setSet:mutableSet2];

        NSLog(@"%@", mutableSet1);

 关于数组排序

NSArray *array = [NSArrayarrayWithObjects:@"12", @"454", @"7878", @"77122", @"555", @"9", nil];

NSLog(@"排序前:%@", array);

        //  这样排序是按照字符串大小来排序的不是数值排序

        array = [array sortedArrayUsingSelector:@selector(compare:)];

NSLog(@"排序后:%@", array);

        //  进行数值排序需要用以下的方法

        array = [array sortedArrayUsingFunction:intSort context:NULL];

NSLog(@"排序后:%@", array);

以下是intSort 的方法实现:

NSInteger intSort(id num1, id num2, void *context)

{

int v1 = [num1 intValue];

int v2 = [num2 intValue];

if (v1 < v2)

        returnNSOrderedAscending;

else if (v1 > v2)

        returnNSOrderedDescending;

else

        returnNSOrderedSame;

}

 

关于字典与数组

        /// 字典里可以存数组,数组可以存字典

NSArray *arr = [NSArray arrayWithObjects: @"1", @"2", @"3", nil];

        //  把数组arr放在字典dic里

NSDictionary *dic = [NSDictionary dictionaryWithObject:arr forKey:@"array"];

NSLog(@"%@", dic);

        //  把字典dic和数组arr 放在数组arr2里

NSArray *arr2 = [NSArray arrayWithObjects:arr, dic, nil];

NSLog(@"%@", arr2);

 
原文地址:https://www.cnblogs.com/hezhuangzhuang/p/5112146.html