一. 字符串 API

1. NSString 用法简介

(1) NSString API 介绍

NSString 功能 :

-- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 String 开头的方法;

  1. // init 开头方法创建字符串
  2. unichar data[5] = {97, 98, 99, 100, 101};
  3. NSString * str = [[NSString alloc] initWithCharacters : data length : 5];

  1. // string 开头方法创建字符串
  2. char * con_str = "Hello World";
  3. NSString * str1 = [NSString stringWithUTF8String : con_str];

-- 字符串获取 : 读取文件 或 网络 URL 初始化字符串;

-- 字符串写出 : 字符串内容 写入 文件 或 URL;

-- 长度获取 : 获取字符串长度, 既可获取字符串内包含的字符个数, 又可获取字符串包含的字节个数;

  1. // 字符串个数 字节数统计
  2. NSLog(@"str char count is : %lu", [str length]);
  3. NSLog(@"str utf8 byte count is : %lu", [str lengthOfBytesUsingEncoding : NSUTF8StringEncoding]);

-- 获取子字符串 : 既可以获取指定位置的字符, 又可以获取指定范围的字符串;

  1. // 获取 前 5 个字符组成的字符串
  2. NSString * str5 = [str3 substringToIndex : 5];
  3. // 获取 从 第五个字符开始的字符串
  4. NSString * str6 = [str3 substringFromIndex : 5];
  5. // 获取 从 第五个 到 第九个 字符串
  6. NSString * str7 = [str3 substringWithRange : NSMakeRange(5, 9)];

-- 获取 C 字符串 : 获取 NSString * 对应的 char * 字符串;

-- 连接字符串 : 将 2 个字符串连接;

  1. //字符串拼接
  2. NSString * str3 = [str2 stringByAppendingString : @", append this"];
  3. NSString * str4 = [str2 stringByAppendingFormat : @", append %@", @"format"];

-- 分隔字符串 : 将 一个 字符串分成两个;

-- 查找 字符 或 子字符串 : 查找字符串内指定的字符 和 子字符串;

  1. // 获取 "append" 出现的位置
  2. NSRange pos = [str3 rangeOfString : @"append"];
  3. NSLog(@"append position from %ld, length %ld", pos.location, pos.length);

-- 替换字符串 :

-- 比较字符串 :

-- 字符串大小比较 :

-- 对字符串中得字符进行大小写转换 :

  1. // 大小写转换
  2. NSString * str8 = [str2 uppercaseString];
  3. NSLog(@"str8 : %@", str8);

(2) NSString 示例代码

示例代码 :

  1. /*************************************************************************
  2. > File Name: OCNSStringDemo.m
  3. > Author: octopus
  4. > Mail: octopus_truth.163.com
  5. > Created Time: 二 10/ 6 17:04:29 2015
  6. ************************************************************************/
  7. #import <Foundation/Foundation.h>
  8.  
  9. int main(int argc, char * argv[])
  10. {
  11. @autoreleasepool {
  12. // init 开头方法创建字符串
  13. unichar data[5] = {97, 98, 99, 100, 101};
  14. NSString * str = [[NSString alloc] initWithCharacters : data length : 5];
  15.  
  16. // string 开头方法创建字符串
  17. char * con_str = "Hello World";
  18. NSString * str1 = [NSString stringWithUTF8String : con_str];
  19.  
  20. NSLog(@"str : %@, str1 : %@", str, str1);
  21.  
  22. // 将字符串写入文件
  23. [str1 writeToFile : @"octopus.txt"
  24. atomically : YES
  25. encoding : NSUTF8StringEncoding
  26. error : nil];
  27.  
  28. // 从文件读取字符串
  29. NSString * str2 = [NSString stringWithContentsOfFile : @"octopus.txt"
  30. encoding : NSUTF8StringEncoding
  31. error : nil];
  32.  
  33. NSLog(@"str2 : %@", str2);
  34.  
  35. //字符串拼接
  36. NSString * str3 = [str2 stringByAppendingString : @", append this"];
  37. NSString * str4 = [str2 stringByAppendingFormat : @", append %@", @"format"];
  38.  
  39. NSLog(@"str3 : %@, str4 : %@", str3, str4);
  40.  
  41. // 字符串个数 字节数统计
  42. NSLog(@"str char count is : %lu", [str length]);
  43. NSLog(@"str utf8 byte count is : %lu", [str lengthOfBytesUsingEncoding : NSUTF8StringEncoding]);
  44.  
  45. // 获取 前 5 个字符组成的字符串
  46. NSString * str5 = [str3 substringToIndex : 5];
  47. // 获取 从 第五个字符开始的字符串
  48. NSString * str6 = [str3 substringFromIndex : 5];
  49. // 获取 从 第五个 到 第九个 字符串
  50. NSString * str7 = [str3 substringWithRange : NSMakeRange(5, 9)];
  51.  
  52. NSLog(@"str5 : %@, str6 : %@, str7 : %@", str5, str6, str7);
  53.  
  54. // 获取 "append" 出现的位置
  55. NSRange pos = [str3 rangeOfString : @"append"];
  56. NSLog(@"append position from %ld, length %ld", pos.location, pos.length);
  57.  
  58. // 大小写转换
  59. NSString * str8 = [str2 uppercaseString];
  60. NSLog(@"str8 : %@", str8);
  61.  
  62. }
  63. }

-- 执行结果 :

  1. bogon:6.6 octopus$ clang -fobjc-arc -framework Foundation OCNSStringDemo.m
  2. bogon:6.6 octopus$ ./a.out
  3. 2015-10-06 17:52:40.007 a.out[1095:507] str : abcde, str1 : Hello World
  4. 2015-10-06 17:52:40.018 a.out[1095:507] str2 : Hello World
  5. 2015-10-06 17:52:40.018 a.out[1095:507] str3 : Hello World, append this, str4 : Hello World, append format
  6. 2015-10-06 17:52:40.019 a.out[1095:507] str char count is : 5
  7. 2015-10-06 17:52:40.020 a.out[1095:507] str utf8 byte count is : 5
  8. 2015-10-06 17:52:40.020 a.out[1095:507] str5 : Hello, str6 : World, append this, str7 : World, a
  9. 2015-10-06 17:52:40.021 a.out[1095:507] append position from 13, length 6
  10. 2015-10-06 17:52:40.021 a.out[1095:507] str8 : HELLO WORLD

2. NSMutableString 可变字符串

(1) NSMutableString 简介

NSString 与 NSMutableString 对比 :

-- NSString 缺陷 : NSString 字符串一旦被创建, 字符串序列是不可改变的;

-- NSMutableString 优点 : NSString 中所有的方法, NSMutableString 都可以调用, NSMutableString 对象可以当做 NSString 对象使用;

(2) NSMutableString 源码示例

源码示例 :

  1. /*************************************************************************
  2. > File Name: OCNSMutableStringTest.m
  3. > Author: octopus
  4. > Mail: octopus_truth.163.com
  5. > Created Time: 二 10/ 6 18:03:28 2015
  6. ************************************************************************/
  7.  
  8. #import <Foundation/Foundation.h>
  9.  
  10. int main(int argc, char * argv[])
  11. {
  12. @autoreleasepool {
  13. NSMutableString * str = [NSMutableString stringWithString : @"Hello World"];
  14.  
  15. NSLog(@"str : %@", str);
  16.  
  17. // 字符串追加
  18. [str appendString : @" Octopus"];
  19. NSLog(@"str : %@", str);
  20.  
  21. [str insertString : @"Insert " atIndex : 6];
  22. NSLog(@"str : %@", str);
  23.  
  24. [str deleteCharactersInRange : NSMakeRange (6, 2)];
  25. NSLog(@"str : %@", str);
  26.  
  27. [str replaceCharactersInRange : NSMakeRange(6, 9)
  28. withString : @" Replace "];
  29. NSLog(@"str : %@", str);
  30. }
  31. }

-- 运行结果 :

  1. bogon:6.6 octopus$ clang -fobjc-arc -framework Foundation OCNSMutableStringTest.m
  2. bogon:6.6 octopus$ ./a.out
  3. 2015-10-06 18:44:04.498 a.out[1180:507] str : Hello World
  4. 2015-10-06 18:44:04.500 a.out[1180:507] str : Hello World Octopus
  5. 2015-10-06 18:44:04.500 a.out[1180:507] str : Hello Insert World Octopus
  6. 2015-10-06 18:44:04.501 a.out[1180:507] str : Hello sert World Octopus
  7. 2015-10-06 18:44:04.501 a.out[1180:507] str : Hello Replace d Octopus

二. 日期 时间 API

1. 日期 时间 示例

示例源码 :

  1. /*************************************************************************
  2. > File Name: OCNSDateTest.m
  3. > Author: octopus
  4. > Mail: octopus_truth.163.com
  5. > Created Time: 二 10/ 6 21:12:10 2015
  6. ************************************************************************/
  7.  
  8. #import <Foundation/Foundation.h>
  9.  
  10. int main(int argc, char * argv[])
  11. {
  12. @autoreleasepool {
  13. //当前时间
  14. NSDate * date = [NSDate date];
  15. NSLog(@"date : %@", date);
  16.  
  17. //一天后的时间
  18. NSDate * date1 = [[NSDate alloc] initWithTimeIntervalSinceNow : 3600 * 24];
  19. NSLog(@"date1 : %@", date1);
  20.  
  21. //3天前时间
  22. NSDate * date2 = [[NSDate alloc] initWithTimeIntervalSinceNow : -3 * 3600 * 24];
  23. NSLog(@"date2 : %@", date2);
  24.  
  25. //获取从 1970年1月1日 开始 20年后的日期
  26. NSDate * date3 = [NSDate dateWithTimeIntervalSince1970 : 3600 * 24 * 366 * 20];
  27. NSLog(@"date3 : %@", date3);
  28.  
  29. //获取当前 Local 下对应的字符串
  30. NSLocale * cn = [NSLocale currentLocale];
  31. NSLog(@"date1 : %@", [date1 descriptionWithLocale : cn]);
  32.  
  33. //日期比较
  34. NSDate * date_earlier = [date earlierDate : date1];
  35. NSDate * date_later = [date laterDate : date1];
  36. NSLog(@"date_earlier : %@, date_later : %@", date_earlier, date_later);
  37.  
  38. //比较结果 枚举值 之前 NSOrderAscending 相同 NSOrderdSame 之后 NSOrderdDescending
  39. switch ([date compare : date1])
  40. {
  41. case NSOrderedAscending :
  42. NSLog(@"NSOrderedAscending");
  43. break;
  44. case NSOrderedSame :
  45. NSLog(@"NSOrderedSame");
  46. break;
  47. case NSOrderedDescending :
  48. NSLog(@"NSOrderedDescending");
  49. break;
  50. }
  51.  
  52. //比较时间差
  53. NSLog(@"date - date1 : %g second", [date timeIntervalSinceDate : date1]);
  54. NSLog(@"date - now : %g second", [date timeIntervalSinceNow]);
  55. }
  56. }

-- 执行结果 :

  1. bogon:~ octopus$ clang -fobjc-arc -framework Foundation OCNSDateTest.m
  2. bogon:~ octopus$ ./a.out
  3. 2015-10-06 21:45:23.441 a.out[1400:507] date : 2015-10-06 13:45:23 +0000
  4. 2015-10-06 21:45:23.442 a.out[1400:507] date1 : 2015-10-07 13:45:23 +0000
  5. 2015-10-06 21:45:23.442 a.out[1400:507] date2 : 2015-10-03 13:45:23 +0000
  6. 2015-10-06 21:45:23.442 a.out[1400:507] date3 : 1990-01-16 00:00:00 +0000
  7. 2015-10-06 21:45:23.444 a.out[1400:507] date1 : 2015107 星期三 中国标准时间下午9:45:23
  8. 2015-10-06 21:45:23.444 a.out[1400:507] date_earlier : 2015-10-06 13:45:23 +0000, date_later : 2015-10-07 13:45:23 +0000
  9. 2015-10-06 21:45:23.445 a.out[1400:507] NSOrderedAscending
  10. 2015-10-06 21:45:23.445 a.out[1400:507] date - date1 : -86400 second
  11. 2015-10-06 21:45:23.445 a.out[1400:507] date - now : -0.011359 second

2. 日期格式器

(1) NSDateFormatter 作用

NSDateFormatter 作用 : 使 NSDate 与 NSString 对象之间相互转化;

NSString 与 NSDate 转换步骤 :

-- 1.创建 NSDateFormatter 对象 :

-- 2.设置格式 : 调用 NSDateFormatter 的 "setDateStyle :", "setTimeStyle :" 方法设置时间日期格式;

-- 3. NSDate -> NSString : 调用 NSDateFormatter 的 "stringFromDate :" 方法;

-- 4. NSString -> NSDate : 调用 NSDateFormatter 的 "dateFromString :" 方法;

(2) NSDateFormatter 时间日期格式

时间 日期格式 枚举 :

-- NSDateFormatterNoStyle : 不显示日期, 时间;

-- NSDateFormatterShortStyle : 显示短日期;

-- NSDateFormatterMediumStyle : 显示中等日期;

-- NSDateFormatterLongStyle : 显示长日期;

-- NSDateFormatterFullStyle : 显示完整日期;

-- 自定义模板 : 调用 NSDateFormatter 的 "setDateFormat :" 方法 设置 时间 日期格式;

(3) NSDateFormatter 代码示例

示例代码 :

  1. /*************************************************************************
  2. > File Name: OCNSDateFormatterTest.m
  3. > Author: octopus
  4. > Mail: octopus_truth.163.com
  5. > Created Time: 三 10/ 7 14:10:32 2015
  6. ************************************************************************/
  7.  
  8. #import <Foundation/Foundation.h>
  9.  
  10. int main(int argc, char * argv[])
  11. {
  12. @autoreleasepool {
  13. //创建日期
  14. NSDate * date = [NSDate date];
  15.  
  16. //初始化 Locale 信息
  17. NSLocale * cn = [[NSLocale alloc] initWithLocaleIdentifier : @"zh_CN"];
  18. NSLocale * en = [[NSLocale alloc] initWithLocaleIdentifier : @"en_US"];
  19.  
  20. //创建并设置 NSDateFormatter
  21. NSDateFormatter * style_short = [[NSDateFormatter alloc] init];
  22. [style_short setDateStyle : NSDateFormatterShortStyle];
  23.  
  24. NSDateFormatter * style_medium = [[NSDateFormatter alloc] init];
  25. [style_medium setDateStyle : NSDateFormatterMediumStyle];
  26.  
  27. NSDateFormatter * style_long = [[NSDateFormatter alloc] init];
  28. [style_long setDateStyle : NSDateFormatterLongStyle];
  29.  
  30. NSDateFormatter * style_full = [[NSDateFormatter alloc] init];
  31. [style_full setDateStyle : NSDateFormatterFullStyle];
  32.  
  33. //打印中国格式的日期信息
  34. NSLog(@"cn short date : %@", [style_short stringFromDate : date]);
  35. NSLog(@"cn medium date : %@", [style_medium stringFromDate : date]);
  36. NSLog(@"cn long date : %@", [style_long stringFromDate : date]);
  37. NSLog(@"cn full date : %@", [style_full stringFromDate : date]);
  38.  
  39. //将四个 NSDateFormatter 设置成美国格式
  40. [style_short setLocale : en];
  41. [style_medium setLocale : en];
  42. [style_long setLocale : en];
  43. [style_full setLocale : en];
  44.  
  45. //打印美国格式的日期字符串
  46. NSLog(@"en short date : %@", [style_short stringFromDate : date]);
  47. NSLog(@"en medium date : %@", [style_medium stringFromDate : date]);
  48. NSLog(@"en long date : %@", [style_long stringFromDate : date]);
  49. NSLog(@"en full date : %@", [style_full stringFromDate : date]);
  50. }
  51. }

-- 执行结果 :

  1. localhost:oc_object octopus$ clang -fobjc-arc -framework Foundation OCNSDateFormatterTest.m
  2. localhost:oc_object octopus$ ./a.out
  3. 2015-10-07 18:54:08.207 a.out[825:507] cn short date : 15-10-7
  4. 2015-10-07 18:54:08.209 a.out[825:507] cn medium date : 2015107
  5. 2015-10-07 18:54:08.209 a.out[825:507] cn long date : 2015107
  6. 2015-10-07 18:54:08.210 a.out[825:507] cn full date : 2015107 星期三
  7. 2015-10-07 18:54:08.211 a.out[825:507] en short date : 10/7/15
  8. 2015-10-07 18:54:08.212 a.out[825:507] en medium date : Oct 7, 2015
  9. 2015-10-07 18:54:08.212 a.out[825:507] en long date : October 7, 2015
  10. 2015-10-07 18:54:08.213 a.out[825:507] en full date : Wednesday, October 7, 2015

3. 日历 (NSCalendar) 日期 (NSDateComponents) 组件

(1) 两个类简介

NSCalendar 与 NSDateComponents 简介 :

-- 主要作用 : 月, 日, 年 等数值转化为 NSDate, 从 NSDate 对象中提取 月, 日, 年 数值;

-- NSCalendar 作用 : NSDate 与 NSDateComponents 转化媒介;

-- NSDateComponents 作用 : 专门封装 年月日时分秒 个字段信息, 包含了 year, month, day, hour, minute, second, week, weekday 等 字段的 getter 和 setter 方法;

(2) NSCalendar 常用方法

NSCalendar 常用方法 :

-- NSDate -> 数据 : "(NSDateComponents *) components : FromDate :", 从 NSDate 中提取 年月日时分秒 各个字段数据;

-- 数据 -> NSDate : "dateFromComponents : (NSDateComponents *) comps", 使用 年月日时分秒 数据创建 NSDate;

(3) 获取 NSDate 中数据

NSDate 获取 日期数值数据 :

-- 1.创建 NSCalendar 对象 :

-- 2.获取 NSDateComponents 对象 : 调用 NSCalendar 的 "components : fromDate :" 方法获取 NSDateComponents 对象;

-- 3.获取具体数值 : 调用 NSDateComponents 对象的 getter 方法获取各字段具体数值;

(4) 获取 NSDate 中的数值数据

根据 日志数值数据 创建 NSDate :

-- 1.创建 NSCalendar 对象 :

-- 2.创建 NSDateComponents 对象 : 调用对象的 setter 方法, 将具体的数值设置到字段中去;

-- 3.创建 NSDate 对象 : 调用 NSCalendar 的 "dateFromComponents : (NSDateComponents *)" 初始化 NSDate 对象;

(5) NSCalendar 和 NSDateComponents 示例代码

示例代码 :

  1. //
  2. // main.m
  3. // 01.Calendar
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. //创建 Calendar 对象
  17. NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier : NSGregorianCalendar];
  18. //获取当前日期
  19. NSDate * date = [NSDate date];
  20. //指定 NSDateComponents 中含有的字段信息
  21. unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
  22. NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit;
  23. //获取 NSDateComponents 对象
  24. NSDateComponents * components = [calendar components:unitFlags fromDate:date];
  25. //将 NSDateComponents 对象中得字段打印出来
  26. NSLog(@"%ld 年 %ld 月 %ld 日, %ld 时, %ld 分, %ld 秒, 星期 %ld", components.year, components.month, components.day, components.hour, components.minute, components.second, components.weekday);
  27.  
  28. //根据 NSDateComponents 创建 NSDate 对象
  29. components.year = 3000;
  30. NSDate * date1 = [calendar dateFromComponents:components];
  31. NSLog(@"date1 : %@", date1);
  32. }
  33.  
  34. }

-- 执行结果 :

  1. 2015-10-08 09:56:28.695 01.Calendar[1255:303] 2015 10 8 日, 9 时, 56 分, 28 秒, 星期 9223372036854775807
  2. 2015-10-08 09:56:28.701 01.Calendar[1255:303] date1 : 3000-10-08 01:56:28 +0000
  3. Program ended with exit code: 0

4. 定时器

(1) 定时器创建

创建定时器 :

-- 两个创建方法 : 调用 NSTimer 的 "scheduledTimeWithTimeInterval : repeats : " 方法, 或者 scheduledTimerWithTimeInterval : target : selector : userInfo : repeats : " 方法;

-- timeInterval 参数 : 指定执行周期, 每隔多少时间执行一次;

-- target 与 selector 参数 : 指定重复执行任务, 如果指定 target 或者 selector 参数, 则指定使用 target 的 selector 方法为执行的任务;

-- Invocation 参数 : 传入一个 NSInvocation 对象, 该 NSInvocation 对象也是封装了一个 target 和 selector;

-- userInfo 参数 : 传入额外的附加信息;

-- repeats 参数 : 指定一个 BOOL 值, 指定是否需要循环执行任务;

(2) 定时器流程

定时器使用流程 :

-- 创建定时器 :

  1. [NSTimer scheduledTimerWithTimeInterval:0.5
  2. target:self
  3. selector:NSSelectorFromString(@"info:")
  4. userInfo:nil
  5. repeats: YES];

-- 编写任务方法 :

-- 销毁定时器 :

  1. [timer invalidate];

三. 对象拷贝

1. 拷贝方法 copy 与 mutableCopy

(1) 方法简介

拷贝方法简介  :

-- copy 方法 : 复制对象的副本, 一般返回对象不可修改的副本; 假如被复制的对象是可修改的 NSMutableString, 复制后成为 NSString 不可修改;

-- mutableCopy 方法 : 复制对象的可变副本, 返回对象的可变副本; 假如被复制对象不可修改 如 NSString, 使用该方法复制后为 NSMutableString 可以修改;

(2) 示例代码

示例代码 :

  1. //
  2. // main.m
  3. // 03.Copy
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSString * str = @"octopus";
  17. NSMutableString * str_mutable = [NSMutableString stringWithString:@"octopus"];
  18.  
  19. //复制可变字符串
  20. NSMutableString * str_mutable1 = [str mutableCopy];
  21. NSMutableString * str_mutable2 = [str_mutable mutableCopy];
  22.  
  23. //验证可变字符串
  24. [str_mutable1 appendString:@" mutable"];
  25. [str_mutable2 appendString:@" mutable"];
  26. NSLog(@"str_mutable1 : %@, str_mutable2 : %@", str_mutable1, str_mutable2);
  27.  
  28. //复制不可变字符串
  29. NSString * str1 = [str copy];
  30. NSMutableString * str2 = [str_mutable copy];
  31. [str2 appendString:@" mutable"];
  32. NSLog(@"str_mutable2 : %@", str_mutable2);
  33. }
  34. return 0;
  35. }

-- 执行结果 : 后面的复制 不能被修改, 所以报错;

  1. 2015-10-08 20:14:57.690 03.Copy[760:303] str_mutable1 : octopus mutable, str_mutable2 : octopus mutable
  2. 2015-10-08 20:14:57.692 03.Copy[760:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'
  3. *** First throw call stack:
  4. (
  5. 0 CoreFoundation 0x00007fff8fda225c __exceptionPreprocess + 172
  6. 1 libobjc.A.dylib 0x00007fff8e6a0e75 objc_exception_throw + 43
  7. 2 CoreFoundation 0x00007fff8fda210c +[NSException raise:format:] + 204
  8. 3 CoreFoundation 0x00007fff8fd71dbe mutateError + 110
  9. 4 03.Copy 0x0000000100000dfc main + 348
  10. 5 libdyld.dylib 0x00007fff84bb85fd start + 1
  11. )
  12. libc++abi.dylib: terminating with uncaught exception of type NSException
  13. (lldb)

2. 自定义 拷贝方法

(1) 自定义类拷贝方法

调用 copy 方法前提 :

-- 1.该类实现了 NSCopying 协议;

-- 2.该类实现了 copyWithZone: 方法;

调用 mutableCopy 方法前提 :

-- 1.该类实现了 NSMutableCopying 协议;

-- 2.该类实现了 mutableCopyWithZone 方法;

(2) 代码示例

自定义类赋值代码示例 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, strong) NSString * name;
  13. @property (assign, nonatomic) int age;
  14. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) copyWithZone : (NSZone *) zone
  16. {
  17. OCCat * cat = [[[self class] allocWithZone:zone]init];
  18. cat.name = self.name;
  19. cat.age = self.age;
  20. return cat;
  21. }
  22.  
  23. @end

-- main.m :

  1. //
  2. // main.m
  3. // 03.Copy
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16. OCCat * cat1 = [[OCCat alloc] init];
  17. cat1.name = [NSMutableString stringWithString:@"Cat One"];
  18. cat1.age = 18;
  19.  
  20. OCCat * cat2 = [cat1 copy];
  21. cat2.name = [NSMutableString stringWithString:@"Cat Two"];
  22. cat2.age = 20;
  23.  
  24. NSLog(@"cat1.name : %@, cat1.age : %d, cat2.name : %@, cat2.age : %d", cat1.name, cat1.age, cat2.name, cat2.age);
  25. }
  26. return 0;
  27. }

-- 执行结果 :

  1. 2015-10-08 20:41:08.584 03.Copy[941:303] cat1.name : Cat One, cat1.age : 18, cat2.name : Cat Two, cat2.age : 20
  2. Program ended with exit code: 0

3. 浅复制 深复制

(1) 浅拷贝示例

浅拷贝示例 : 复制 cat2 对象是从 cat1 复制而来, 但是 cat1 中得 name 是一个 NSString * 引用对象, 这两个 cat1 cat2 都指向同一个 name 对象;

-- 修改下之前的 main 函数 :

  1. //
  2. // main.m
  3. // 03.Copy
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16. OCCat * cat1 = [[OCCat alloc] init];
  17. cat1.name = [NSMutableString stringWithString:@"Cat One"];
  18. cat1.age = 18;
  19.  
  20. OCCat * cat2 = [cat1 copy];
  21. [(NSMutableString *)cat2.name replaceCharactersInRange:NSMakeRange(0, 7) withString:@"Cat Two"];
  22. cat2.age = 20;
  23.  
  24. NSLog(@"cat1.name : %@, cat1.age : %d, cat2.name : %@, cat2.age : %d", cat1.name, cat1.age, cat2.name, cat2.age);
  25. }
  26. return 0;
  27. }

-- 执行结果 : 

  1. 2015-10-09 17:13:56.329 03.Copy[1744:303] cat1.name : Cat Two, cat1.age : 18, cat2.name : Cat Two, cat2.age : 20
  2. Program ended with exit code: 0

(2) 深拷贝示例

深拷贝完整示例 :

-- OCCat.h : 与之前的相同;

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, strong) NSString * name;
  13. @property (assign, nonatomic) int age;
  14. @end

-- OCCat.m : 只是在 copyWithZone 方法中, 将 name 赋值 改为 name copy 后赋值;

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) copyWithZone : (NSZone *) zone
  16. {
  17. OCCat * cat = [[[self class] allocWithZone:zone]init];
  18. cat.name = [self.name mutableCopy];
  19. cat.age = self.age;
  20. return cat;
  21. }
  22.  
  23. @end

-- main.m : 与之前相同;

  1. //
  2. // main.m
  3. // 03.Copy
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16. OCCat * cat1 = [[OCCat alloc] init];
  17. cat1.name = [NSMutableString stringWithString:@"Cat One"];
  18. cat1.age = 18;
  19.  
  20. OCCat * cat2 = [cat1 copy];
  21. [(NSMutableString *)cat2.name replaceCharactersInRange:NSMakeRange(0, 7) withString:@"Cat Two"];
  22. cat2.age = 20;
  23.  
  24. NSLog(@"cat1.name : %@, cat1.age : %d, cat2.name : %@, cat2.age : %d", cat1.name, cat1.age, cat2.name, cat2.age);
  25. }
  26. return 0;
  27. }

-- 执行结果 :

  1. 2015-10-09 17:27:22.302 03.Copy[1809:303] cat1.name : Cat One, cat1.age : 18, cat2.name : Cat Two, cat2.age : 20
  2. Program ended with exit code: 0

4. setter 的 copy 参数

(1) copy 参数

copy 参数用处 :

-- 示例 :

  1. @property (nonatomic, copy) NSMutableString * name;

-- 作用 : 为 name 属性 赋值时, 将赋值对象 copy 一份, 将副本赋值给 name 属性;

-- 使用 copy 指令, 相当于将 setter 方法设置成如下状态 :

  1. - (void) setName:(NSMutableString *)name1
  2. {
  3. this.name = [name1 copy];
  4. }

(2) copy 参数示例

copy 代码错误使用示例 : copy 的属性, 赋值后 是不可变的, 如 NSMutableString 接收一个 NSString 赋值, 如果在对赋值后的属性执行 NSMutableString 的操作, 就会报错;

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSMutableString * name;
  13. @property (assign, nonatomic) int age;
  14. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. @end

-- main.h :

  1. //
  2. // main.m
  3. // 03.Copy
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16. OCCat * cat1 = [[OCCat alloc] init];
  17. cat1.name = [NSMutableString stringWithString:@"Cat One"];
  18. cat1.age = 18;
  19.  
  20. [cat1.name appendString:@" append"];
  21. }
  22. return 0;
  23. }

-- 执行结果 : 

  1. 2015-10-09 23:17:01.469 03.Copy[2095:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'
  2. *** First throw call stack:
  3. (
  4. 0 CoreFoundation 0x00007fff8fda225c __exceptionPreprocess + 172
  5. 1 libobjc.A.dylib 0x00007fff8e6a0e75 objc_exception_throw + 43
  6. 2 CoreFoundation 0x00007fff8fda210c +[NSException raise:format:] + 204
  7. 3 CoreFoundation 0x00007fff8fd71dbe mutateError + 110
  8. 4 03.Copy 0x0000000100000d86 main + 246
  9. 5 libdyld.dylib 0x00007fff84bb85fd start + 1
  10. )
  11. libc++abi.dylib: terminating with uncaught exception of type NSException
  12. (lldb)

四. NSArray NSMutableArray 数组集合

Objective-C 集合概述 : 

-- NSArray : 有序, 可重复集合;

-- NSSet : 无序, 不可重复集合;

-- NSDictionary : 有映射关系的集合;

1. NSArray 基本功能用法

(1) NSArray 创建

NSArray 简介 : 有序, 可重复集合;

-- 特点 : 每个元素都有对应的顺序索引, 允许重复元素, 通过索引来访问指定位置元素, 索引从 0 开始;

NSArray 对象创建 :

-- "array" 方法 : 创建空得 NSArray 对象;

-- "arrayWithContentsOfFile : " 方法 : 读取文件内容创建 NSArray 对象;

-- "initWithContentsOfFile : " 方法 :  读取文件内容创建 NSArray 对象;

-- "arrayWithObject : " 方法 : 创建只包含单个元素的 NSArray 对象;

-- "initWithObject : " 方法 : 创建只包含单个元素的 NSArray 对象;

-- "arrayWithObjects : " 方法 : 创建包含 N 个元素的 NSArray 对象, 最后一个 nil 元素, 表示 NSArray 集合结束;

  1. NSArray * array = [NSArray arrayWithObjects:@"Smoke", @"Drink", @"Perm", nil];

-- "initWithObjects : " 方法 : 创建包含 N 个元素的 NSArray 对象;

(2) NSArray 基本方法

NSArray 集合主要方法 :

-- 查索引 : 查询集合元素在 NSArray 中索引;

  1. //获取元素在集合中得位置
  2. NSLog(@"Index Of Drink at subArray is : %ld", [subArray indexOfObject:@"Drink"]);
  3. NSLog(@"Index Of Drink at array is : %ld", [array indexOfObject:@"Drink" inRange:NSMakeRange(1, 2)]);

-- 查元素 : 根据索引值 取出 NSArray 集合中得元素;

  1. // 获取 第 0 1 最后一个元素 并打印
  2. NSLog(@"First : %@", [array objectAtIndex:0]);
  3. NSLog(@"Second : %@", [array objectAtIndex:1]);
  4. NSLog(@"Last : %@", [array lastObject]);

-- 整体调用 : 对集合元素整体调用的方法;

-- 排序 : 对 NSArray 集合进行排序;

-- 截取元素 : 取出 NSArray 部分元素组成新集合;

  1. //截取 1 2 个元素组成新 NSArray 集合对象
  2. NSArray * subArray = [array objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)]];

-- 追加元单个素 : "arrayByAddingObject : " 追加单个元素;

  1. array = [array arrayByAddingObject:@"Run"];

-- 追加数组 : "arrayWithObjects : " 追加一个数组;

  1. array = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"Jump", @"Fuck", nil]];

(3) NSArray 基本方法 示例代码

示例代码 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14. - (id) initWithName : (NSString *) _name age : (int) _age;
  15. - (void) purr : (NSString *) content;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14. - (id) initWithName:(NSString *)_name age:(int)_age
  15. {
  16. self = [super init];
  17. self.name = _name;
  18. self.age = _age;
  19. return self;
  20. }
  21.  
  22. - (void) purr:(NSString *)content
  23. {
  24. NSLog(@"cat name : %@, age %d is purr", self.name, self.age);
  25. }
  26.  
  27. - (BOOL) isEqual : (id) other
  28. {
  29. //如果地址相同, 那么比较结果肯定相同
  30. if(self == other)
  31. {
  32. return YES;
  33. }
  34.  
  35. //对象对比之前首先保证类型相同
  36. if([other class] == OCCat.class)
  37. {
  38. OCCat * cat = (OCCat *)other;
  39. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  40. }
  41. return NO;
  42. }
  43.  
  44. //%@ 打印的时候输出的内容
  45. - (NSString *) description
  46. {
  47. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  48. }
  49.  
  50. @end

-- main.m :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. //创建 NSArray 对象
  17. NSArray * array = [NSArray arrayWithObjects:@"Smoke", @"Drink", @"Perm", nil];
  18.  
  19. // 获取 第 0 1 最后一个元素 并打印
  20. NSLog(@"First : %@", [array objectAtIndex:0]);
  21. NSLog(@"Second : %@", [array objectAtIndex:1]);
  22. NSLog(@"Third: %@", array[2]);
  23. NSLog(@"Last : %@", [array lastObject]);
  24.  
  25. // 打印整个 array
  26. NSLog(@"array : \n%@", array);
  27.  
  28. //截取 1 2 个元素组成新 NSArray 集合对象, NSIndexSet 集合用于保存索引值
  29. NSArray * subArray = [array objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)]];
  30.  
  31. //获取元素在集合中得位置
  32. NSLog(@"Index Of Drink at subArray is : %ld", [subArray indexOfObject:@"Drink"]);
  33. NSLog(@"Index Of Drink at array is : %ld", [array indexOfObject:@"Drink" inRange:NSMakeRange(1, 2)]);
  34.  
  35. //添加单个元素
  36. array = [array arrayByAddingObject:@"Run"];
  37. //追加一个 NSArray 集合, nil 结尾表示 NSArray 结束
  38. array = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"Jump", @"Fuck", nil]];
  39. //遍历 array
  40. for (int i = 0; i < array.count; i ++) {
  41. NSLog(@"array %d : %@", i, [array objectAtIndex:i]);
  42. }
  43.  
  44. //将字符串写出到文件中
  45. [array writeToFile:@"array.txt" atomically:YES];
  46.  
  47. //创建 OCCat 的 array
  48. NSArray * array2 = [NSArray arrayWithObjects:
  49. [[OCCat alloc] initWithName : @"Tom" age : 18],
  50. [[OCCat alloc] initWithName : @"Jerry" age : 20],
  51. nil];
  52. //打印 array, 验证 重写的 description 方法
  53. NSLog(@"array2 is : \n%@", array2);
  54. OCCat* cat = [[OCCat alloc] initWithName : @"Tom" age : 18];
  55. //获取 元素 在 array 中的位置, 验证 isEqual 方法
  56. NSUInteger index = [array2 indexOfObject:cat];
  57. NSLog(@"index Of Tom is : %ld", index);
  58.  
  59. }
  60. return 0;
  61. }

-- 执行结果 :

  1. 2015-10-14 17:16:12.748 04.NSArray[7155:303] First : Smoke
  2. 2015-10-14 17:16:12.750 04.NSArray[7155:303] Second : Drink
  3. 2015-10-14 17:16:12.751 04.NSArray[7155:303] Third: Perm
  4. 2015-10-14 17:16:12.751 04.NSArray[7155:303] Last : Perm
  5. 2015-10-14 17:16:12.751 04.NSArray[7155:303] array :
  6. (
  7. Smoke,
  8. Drink,
  9. Perm
  10. )
  11. 2015-10-14 17:16:12.752 04.NSArray[7155:303] Index Of Drink at subArray is : 0
  12. 2015-10-14 17:16:12.752 04.NSArray[7155:303] Index Of Drink at array is : 1
  13. 2015-10-14 17:16:12.753 04.NSArray[7155:303] array 0 : Smoke
  14. 2015-10-14 17:16:12.753 04.NSArray[7155:303] array 1 : Drink
  15. 2015-10-14 17:16:12.753 04.NSArray[7155:303] array 2 : Perm
  16. 2015-10-14 17:16:12.754 04.NSArray[7155:303] array 3 : Run
  17. 2015-10-14 17:16:12.754 04.NSArray[7155:303] array 4 : Jump
  18. 2015-10-14 17:16:12.755 04.NSArray[7155:303] array 5 : Fuck
  19. 2015-10-14 17:16:12.769 04.NSArray[7155:303] array2 is :
  20. (
  21. "<OCCat [name = Tom, age = 18]>",
  22. "<OCCat [name = Jerry, age = 20]>"
  23. )
  24. 2015-10-14 17:16:12.769 04.NSArray[7155:303] index Of Tom is : 0
  25. Program ended with exit code: 0

2. NSArray 集合整体调用

(1) NSArray 整体元素调用简介

调用所有元素方法 :

-- "makeObjectPerformSelector : " 方法 : 按顺序 依次 调用 NSArray 中集合的每个元素, 需要传入 SEL 参数, 该参数指定需要调用的方法;

-- "makeObjectPerformSelector : withObject : " 方法 : 参数一 SEL 代表的方法, 参数二 方法的参数, 参数三 控制是否终止迭代;

-- "enumerateObjectsUsingBlock : " 方法 : 遍历集合中元素, 使用 集合中的元素 执行指定代码块;

-- "enumerateObjectsWithOptions : usingBlock : " 方法 : 遍历元素, 依次使用元素执行代码块, 可以额外传入一个参数控制遍历选项;

-- "enumerateObjectsAtIndexs : options : usingBlock : " 方法 : 遍历指定范围元素, 依次使用元素执行代码块, 传入最后的参数用于控制遍历;

(2) NSArray 整体元素调用代码示例

代码示例 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14. - (id) initWithName : (NSString *) _name age : (int) _age;
  15. - (void) purr : (NSString *) content;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14. - (id) initWithName:(NSString *)_name age:(int)_age
  15. {
  16. self = [super init];
  17. self.name = _name;
  18. self.age = _age;
  19. return self;
  20. }
  21.  
  22. - (void) purr:(NSString *)content
  23. {
  24. NSLog(@"cat name : %@, age %d is purr", self.name, self.age);
  25. }
  26.  
  27. - (BOOL) isEqual : (id) other
  28. {
  29. //如果地址相同, 那么比较结果肯定相同
  30. if(self == other)
  31. {
  32. return YES;
  33. }
  34.  
  35. //对象对比之前首先保证类型相同
  36. if([other class] == OCCat.class)
  37. {
  38. OCCat * cat = (OCCat *)other;
  39. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  40. }
  41. return NO;
  42. }
  43.  
  44. //%@ 打印的时候输出的内容
  45. - (NSString *) description
  46. {
  47. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  48. }
  49.  
  50. @end

-- main.m :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. //创建 OCCat 的 array
  17. NSArray * array = [NSArray arrayWithObjects:
  18. [[OCCat alloc] initWithName : @"Tom" age : 18],
  19. [[OCCat alloc] initWithName : @"Jerry" age : 20],
  20. nil];
  21.  
  22. NSLog(@"array is : \n%@", array);
  23.  
  24. [array makeObjectsPerformSelector:@selector(purr:) withObject:@" FUCK YOU "];
  25.  
  26. }
  27. return 0;
  28. }

-- 执行结果 :

  1. 2015-10-15 10:50:49.902 04.NSArray[8086:303] array is :
  2. (
  3. "<OCCat [name = Tom, age = 18]>",
  4. "<OCCat [name = Jerry, age = 20]>"
  5. )
  6. 2015-10-15 10:50:49.905 04.NSArray[8086:303] cat name : Tom, age 18 is purr
  7. 2015-10-15 10:50:49.905 04.NSArray[8086:303] cat name : Jerry, age 20 is purr
  8. Program ended with exit code: 0

3. NSArray 排序

(1) NSArray 排序简介

NSArray 排序 :

-- "sortArrayUsingFunction : context : " 方法 : 使用排序函数对集合进行排序, 函数会返回 NSOrderedDescending, NSOrderedAscending, NSOrderedSame 枚举值, 同时会返回一个 排序号的 NSArray 集合;

-- "sortArrayUsingSelector : " 方法 : 使用集合元素自身的方法对集合进行排序, 该方法返回 NSOrderedDescending, NSOrderedAscending, NSOrderedSame 枚举值, 同时还会返回一个 排序号的 NSArray 集合;

-- "sortArrayUsingComparator : " 方法 : 使用代码块对集合进行排序, 代码块 返回 NSOrderedDescending, NSOrderedAscending, NSOrderedSame 枚举值, 同时返回排序号的 NSArray 集合;

(2) NSArray 排序代码示例

NSArray 排序示例代码 :

-- main.c :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. NSInteger intSort(id num1, id num2, void * context)
  12. {
  13. int v1 = [num1 intValue];
  14. int v2 = [num2 intValue];
  15.  
  16. if(v1 < v2)
  17. {
  18. return NSOrderedAscending;
  19. }
  20.  
  21. if(v1 > v2)
  22. {
  23. return NSOrderedDescending;
  24. }
  25.  
  26. return NSOrderedSame;
  27. }
  28.  
  29. int main(int argc, const char * argv[])
  30. {
  31.  
  32. @autoreleasepool {
  33.  
  34. //创建一个 NSarray 集合, 其中放置 NSString 元素
  35. NSArray * array = [NSArray arrayWithObjects:@"Tom", @"Jerry", @"Fuck", nil];
  36. NSLog(@"array : \n%@\n", array);
  37.  
  38. //使用 compare 方法进行排序
  39. array = [array sortedArrayUsingSelector:@selector(compare:)];
  40. NSLog(@"array sort : \n%@\n", array);
  41.  
  42. //创建一个 NSNumber 元素的 NSarray 集合
  43. NSArray * array_num = [NSArray arrayWithObjects:
  44. [NSNumber numberWithInt:18],
  45. [NSNumber numberWithInt:16],
  46. [NSNumber numberWithInt:20],
  47. [NSNumber numberWithInt:-1],
  48. nil];
  49. NSLog(@"array_num : \n%@\n", array_num);
  50.  
  51. //使用 intSort 方法进行排序
  52. array_num = [array_num sortedArrayUsingFunction:intSort context:nil];
  53. NSLog(@"array_num sort : \n%@\n", array_num);
  54.  
  55. //使用代码块进行排序
  56. array_num = [array_num sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
  57. {
  58. if([obj2 intValue] < [obj1 intValue])
  59. {
  60. return NSOrderedAscending;
  61. }
  62.  
  63. if([obj2 intValue] > [obj1 intValue])
  64. {
  65. return NSOrderedDescending;
  66. }
  67.  
  68. return NSOrderedSame;
  69. }];
  70. NSLog(@"array_num : \n%@\n", array_num);
  71.  
  72. }
  73. return 0;
  74. }

-- 执行结果 :

  1. 2015-10-15 11:52:51.335 04.NSArray[8465:303] array :
  2. (
  3. Tom,
  4. Jerry,
  5. Fuck
  6. )
  7. 2015-10-15 11:52:51.336 04.NSArray[8465:303] array sort :
  8. (
  9. Fuck,
  10. Jerry,
  11. Tom
  12. )
  13. 2015-10-15 11:52:51.337 04.NSArray[8465:303] array_num :
  14. (
  15. 18,
  16. 16,
  17. 20,
  18. "-1"
  19. )
  20. 2015-10-15 11:52:51.337 04.NSArray[8465:303] array_num sort :
  21. (
  22. "-1",
  23. 16,
  24. 18,
  25. 20
  26. )
  27. 2015-10-15 11:52:51.338 04.NSArray[8465:303] array_num :
  28. (
  29. 20,
  30. 18,
  31. 16,
  32. "-1"
  33. )
  34. Program ended with exit code: 0

4. NSArray 枚举器遍历

(1) NSArray 获取枚举器

NSArray 枚举器 NSEnumerator 简介 :

-- "objectEnumerator : " 方法 : 返回 NSArray 集合顺序枚举器;

-- "reverseObjectEnumerator : "  方法 : 返回 NSArray 集合的逆序枚举器;

(2) NSEnumerator 枚举器

NSEnumerator 方法简介 :

-- "allObjects : " 方法 : 获取集合中的所有元素;

-- "nextObjects : " 方法 : 获取集合中的下一个元素;

(3) NSEnumerator 枚举器示例代码

NSEnumerator 示例代码 :

-- main.m 代码 :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. //创建一个 NSarray 集合, 其中放置 NSString 元素
  17. NSArray * array = [NSArray arrayWithObjects:@"Tom", @"Jerry", @"Fuck", nil];
  18. NSLog(@"array : \n%@\n", array);
  19.  
  20. //顺序枚举
  21. NSEnumerator * enumerator = [array objectEnumerator];
  22. id object;
  23. while (object = [enumerator nextObject]) {
  24. NSLog(@"%@", object);
  25. }
  26.  
  27. NSLog(@"\n");
  28.  
  29. //逆序枚举
  30. NSEnumerator * reverseEnumerator = [array reverseObjectEnumerator];
  31. while (object = [reverseEnumerator nextObject]) {
  32. NSLog(@"%@", object);
  33. }
  34.  
  35. }
  36. return 0;
  37. }

-- 执行结果 :

  1. 2015-10-15 12:09:59.170 04.NSArray[8590:303] array :
  2. (
  3. Tom,
  4. Jerry,
  5. Fuck
  6. )
  7. 2015-10-15 12:09:59.176 04.NSArray[8590:303] Tom
  8. 2015-10-15 12:09:59.177 04.NSArray[8590:303] Jerry
  9. 2015-10-15 12:09:59.177 04.NSArray[8590:303] Fuck
  10. 2015-10-15 12:09:59.178 04.NSArray[8590:303]
  11. 2015-10-15 12:09:59.178 04.NSArray[8590:303] Fuck
  12. 2015-10-15 12:09:59.178 04.NSArray[8590:303] Jerry
  13. 2015-10-15 12:09:59.179 04.NSArray[8590:303] Tom
  14. Program ended with exit code: 0

5. NSArray 快速枚举

(1) NSArray 快速枚举

快速枚举简介 : 快速枚举无需获取集合长度, 也不用根据索引访问集合元素;

-- 语法 :

  1. for (id object in array)
  2. {
  3. //对 object 进行操作
  4. }

(2) NSArray 快速枚举示例

NSArray 快速枚举示例 :

-- main.m :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. //创建一个 NSarray 集合, 其中放置 NSString 元素
  17. NSArray * array = [NSArray arrayWithObjects:@"Tom", @"Jerry", @"Fuck", nil];
  18. NSLog(@"array : \n%@\n", array);
  19.  
  20. //快速枚举
  21. for(id object in array)
  22. {
  23. NSLog(@"%@", object);
  24. }
  25.  
  26. }
  27. return 0;
  28. }

-- 执行结果 :

  1. 2015-10-15 12:17:36.096 04.NSArray[8642:303] array :
  2. (
  3. Tom,
  4. Jerry,
  5. Fuck
  6. )
  7. 2015-10-15 12:17:36.098 04.NSArray[8642:303] Tom
  8. 2015-10-15 12:17:36.098 04.NSArray[8642:303] Jerry
  9. 2015-10-15 12:17:36.099 04.NSArray[8642:303] Fuck
  10. Program ended with exit code: 0

6. NSMutableArray 可变数组

(1) NSMutableArray 简介

NSMutableArray 简介 :

-- NSMutableArray 与 NSArray 对比 : NSArray 是不可变数组, 一旦创建不能增删替换元素, NSMutableArray 可以随时增加 删除 替换 元素;

-- "addXX" 方法 : 添加集合元素的方法;

-- "removeXX" 方法 : 删除集合元素的方法;

-- "replaceXX" 方法 : 替换集合元素的方法;

-- "sortXX" 方法 : 对集合本身排序的方法;

(2) NSMutableArray 代码示例

NSMutableArray 代码示例 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14. - (id) initWithName : (NSString *) _name age : (int) _age;
  15. - (void) purr : (NSString *) content;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14. - (id) initWithName:(NSString *)_name age:(int)_age
  15. {
  16. self = [super init];
  17. self.name = _name;
  18. self.age = _age;
  19. return self;
  20. }
  21.  
  22. - (void) purr:(NSString *)content
  23. {
  24. NSLog(@"cat name : %@, age %d is purr", self.name, self.age);
  25. }
  26.  
  27. - (BOOL) isEqual : (id) other
  28. {
  29. //如果地址相同, 那么比较结果肯定相同
  30. if(self == other)
  31. {
  32. return YES;
  33. }
  34.  
  35. //对象对比之前首先保证类型相同
  36. if([other class] == OCCat.class)
  37. {
  38. OCCat * cat = (OCCat *)other;
  39. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  40. }
  41. return NO;
  42. }
  43.  
  44. //%@ 打印的时候输出的内容
  45. - (NSString *) description
  46. {
  47. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  48. }
  49.  
  50. @end

-- main.m :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSMutableArray * array = [NSMutableArray arrayWithObjects:
  17. [[OCCat alloc] initWithName : @"Tom" age : 18],
  18. [[OCCat alloc] initWithName : @"Jerry" age: 20],
  19. [[OCCat alloc] initWithName : @"Jay" age : 180],
  20. nil];
  21. NSLog(@"array 原始数组 : \n%@\n", array);
  22.  
  23. //添加单个元素
  24. [array addObject:[[OCCat alloc] initWithName:@"octopus" age:26]];
  25. NSLog(@"array 追加单个元素 : \n%@\n", array);
  26.  
  27. //添加数组
  28. [array addObjectsFromArray:[NSArray arrayWithObjects:
  29. [[OCCat alloc] initWithName:@"HanShuliang" age:26],
  30. [[OCCat alloc] initWithName:@"Bill" age:60],
  31. nil]];
  32. NSLog(@"array 追加集合 : \n%@\n", array);
  33.  
  34. //插入单个元素
  35. [array insertObject : [[OCCat alloc] initWithName:@"XiJinping" age:26] atIndex:0];
  36. NSLog(@"array 插入单个元素 : \n%@\n", array);
  37.  
  38. //插入集合
  39. [array insertObjects: [NSArray arrayWithObjects:
  40. [[OCCat alloc] initWithName:@"LiKeqiang" age:26],
  41. [[OCCat alloc] initWithName:@"Son" age:60],
  42. nil]
  43. atIndexes: [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 2)]];
  44. NSLog(@"array 插入集合 : \n%@\n", array);
  45.  
  46. //删除最后一个元素
  47. [array removeLastObject];
  48. NSLog(@"array 删除最后一个元素 : \n%@\n", array);
  49.  
  50. //删除第3个元素
  51. [array removeObjectAtIndex:3];
  52. NSLog(@"array 删除第三个元素 : \n%@\n", array);
  53.  
  54. //删除第3,4个元素
  55. [array removeObjectsInRange:NSMakeRange(3, 2)];
  56. NSLog(@"array 删除第3,4个元素 : \n%@\n", array);
  57.  
  58. //替换第0个元素
  59. [array replaceObjectAtIndex:0 withObject:[[OCCat alloc] initWithName:@"HanShuliang" age:28]];
  60. NSLog(@"array 替换第0个元素 : \n%@\n", array);
  61.  
  62. }
  63. return 0;
  64. }

-- 执行结果 :

  1. 2015-10-15 12:51:43.749 04.NSArray[9033:303] array 原始数组 :
  2. (
  3. "<OCCat [name = Tom, age = 18]>",
  4. "<OCCat [name = Jerry, age = 20]>",
  5. "<OCCat [name = Jay, age = 180]>"
  6. )
  7. 2015-10-15 12:51:43.752 04.NSArray[9033:303] array 追加单个元素 :
  8. (
  9. "<OCCat [name = Tom, age = 18]>",
  10. "<OCCat [name = Jerry, age = 20]>",
  11. "<OCCat [name = Jay, age = 180]>",
  12. "<OCCat [name = octopus, age = 26]>"
  13. )
  14. 2015-10-15 12:51:43.752 04.NSArray[9033:303] array 追加集合 :
  15. (
  16. "<OCCat [name = Tom, age = 18]>",
  17. "<OCCat [name = Jerry, age = 20]>",
  18. "<OCCat [name = Jay, age = 180]>",
  19. "<OCCat [name = octopus, age = 26]>",
  20. "<OCCat [name = HanShuliang, age = 26]>",
  21. "<OCCat [name = Bill, age = 60]>"
  22. )
  23. 2015-10-15 12:51:43.753 04.NSArray[9033:303] array 插入单个元素 :
  24. (
  25. "<OCCat [name = XiJinping, age = 26]>",
  26. "<OCCat [name = Tom, age = 18]>",
  27. "<OCCat [name = Jerry, age = 20]>",
  28. "<OCCat [name = Jay, age = 180]>",
  29. "<OCCat [name = octopus, age = 26]>",
  30. "<OCCat [name = HanShuliang, age = 26]>",
  31. "<OCCat [name = Bill, age = 60]>"
  32. )
  33. 2015-10-15 12:51:43.753 04.NSArray[9033:303] array 插入集合 :
  34. (
  35. "<OCCat [name = XiJinping, age = 26]>",
  36. "<OCCat [name = Tom, age = 18]>",
  37. "<OCCat [name = LiKeqiang, age = 26]>",
  38. "<OCCat [name = Son, age = 60]>",
  39. "<OCCat [name = Jerry, age = 20]>",
  40. "<OCCat [name = Jay, age = 180]>",
  41. "<OCCat [name = octopus, age = 26]>",
  42. "<OCCat [name = HanShuliang, age = 26]>",
  43. "<OCCat [name = Bill, age = 60]>"
  44. )
  45. 2015-10-15 12:51:43.755 04.NSArray[9033:303] array 删除最后一个元素 :
  46. (
  47. "<OCCat [name = XiJinping, age = 26]>",
  48. "<OCCat [name = Tom, age = 18]>",
  49. "<OCCat [name = LiKeqiang, age = 26]>",
  50. "<OCCat [name = Son, age = 60]>",
  51. "<OCCat [name = Jerry, age = 20]>",
  52. "<OCCat [name = Jay, age = 180]>",
  53. "<OCCat [name = octopus, age = 26]>",
  54. "<OCCat [name = HanShuliang, age = 26]>"
  55. )
  56. 2015-10-15 12:51:43.756 04.NSArray[9033:303] array 删除第三个元素 :
  57. (
  58. "<OCCat [name = XiJinping, age = 26]>",
  59. "<OCCat [name = Tom, age = 18]>",
  60. "<OCCat [name = LiKeqiang, age = 26]>",
  61. "<OCCat [name = Jerry, age = 20]>",
  62. "<OCCat [name = Jay, age = 180]>",
  63. "<OCCat [name = octopus, age = 26]>",
  64. "<OCCat [name = HanShuliang, age = 26]>"
  65. )
  66. 2015-10-15 12:51:43.757 04.NSArray[9033:303] array 删除第3,4个元素 :
  67. (
  68. "<OCCat [name = XiJinping, age = 26]>",
  69. "<OCCat [name = Tom, age = 18]>",
  70. "<OCCat [name = LiKeqiang, age = 26]>",
  71. "<OCCat [name = octopus, age = 26]>",
  72. "<OCCat [name = HanShuliang, age = 26]>"
  73. )
  74. 2015-10-15 12:51:43.757 04.NSArray[9033:303] array 替换第0个元素 :
  75. (
  76. "<OCCat [name = HanShuliang, age = 28]>",
  77. "<OCCat [name = Tom, age = 18]>",
  78. "<OCCat [name = LiKeqiang, age = 26]>",
  79. "<OCCat [name = octopus, age = 26]>",
  80. "<OCCat [name = HanShuliang, age = 26]>"
  81. )
  82. Program ended with exit code: 0

7. NSArray 的 KVO KVC 用法

(1) NSArray KVC 简介

NSArray KVC 简介 : NSArray 可以对元素进行整体 KVC 编码;

-- "setValue : forKey : " 方法 : 将所有元素的制定 key 变量设置为 某个值;

-- "valueForKey : " 方法 : 返回 所有元素指定变量值组成的 NSArray 集合;

(2) NSArray KVO 简介

NSArray KVO 简介 : NSArray 中可以对所有元素进行 KVO 编程;

-- "addObserver : forKeyPath : options : context : " 方法 : 为集合中所有元素添加 KVO 监听器;

-- "removeObserver : forKeyPath : " 方法 : 为集合中所有元素删除 KVO 监听器;

-- "addObserver : toObjectAtIndexes : forKeyPath : options : context : " 方法 : 为集合中指定元素添加 KVO 监听;

-- "removeObserver : fromObjectsAtIndexes : forKeyPath : " 方法 : 为集合中指定元素删除 KVO 监听;

(3) NSArray KVC KVO 示例代码

NSArray KVC KVO 示例代码 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14. - (id) initWithName : (NSString *) _name age : (int) _age;
  15. - (void) purr : (NSString *) content;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14. - (id) initWithName:(NSString *)_name age:(int)_age
  15. {
  16. self = [super init];
  17. self.name = _name;
  18. self.age = _age;
  19. return self;
  20. }
  21.  
  22. - (void) purr:(NSString *)content
  23. {
  24. NSLog(@"cat name : %@, age %d is purr", self.name, self.age);
  25. }
  26.  
  27. - (BOOL) isEqual : (id) other
  28. {
  29. //如果地址相同, 那么比较结果肯定相同
  30. if(self == other)
  31. {
  32. return YES;
  33. }
  34.  
  35. //对象对比之前首先保证类型相同
  36. if([other class] == OCCat.class)
  37. {
  38. OCCat * cat = (OCCat *)other;
  39. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  40. }
  41. return NO;
  42. }
  43.  
  44. //%@ 打印的时候输出的内容
  45. - (NSString *) description
  46. {
  47. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  48. }
  49.  
  50. @end

-- main.m :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSMutableArray * array = [NSMutableArray arrayWithObjects:
  17. [[OCCat alloc] initWithName : @"Tom" age : 18],
  18. [[OCCat alloc] initWithName : @"Jerry" age: 20],
  19. [[OCCat alloc] initWithName : @"Jay" age : 180],
  20. nil];
  21. NSLog(@"array 原始数组 : \n%@\n", array);
  22.  
  23. NSArray * str_array = [array valueForKeyPath:@"name"];
  24. NSLog(@"str_array : \n%@\n", str_array);
  25.  
  26. [array setValue:@"octopus" forKeyPath:@"name"];
  27. NSLog(@"array 设置 KVC 后的数组 : \n%@\n", array);
  28.  
  29. }
  30. return 0;
  31. }

-- 执行结果 :

  1. 2015-10-15 13:07:04.116 04.NSArray[9135:303] array 原始数组 :
  2. (
  3. "<OCCat [name = Tom, age = 18]>",
  4. "<OCCat [name = Jerry, age = 20]>",
  5. "<OCCat [name = Jay, age = 180]>"
  6. )
  7. 2015-10-15 13:07:04.118 04.NSArray[9135:303] str_array :
  8. (
  9. Tom,
  10. Jerry,
  11. Jay
  12. )
  13. 2015-10-15 13:07:04.118 04.NSArray[9135:303] array 设置 KVC 后的数组 :
  14. (
  15. "<OCCat [name = octopus, age = 18]>",
  16. "<OCCat [name = octopus, age = 20]>",
  17. "<OCCat [name = octopus, age = 180]>"
  18. )
  19. Program ended with exit code: 0

五. NSSet 与 NSmutableSet 集合

1. NSSet 功能与用法

(1) NSSet 简介

NSSet 功能简介 :

-- 基本属性 : 无序, 不可重复; 如果将两个相同的元素放在同一个 NSSet 中, 只会保留一个;

-- 性能分析 : NSSet 使用 hash 方法存储集合中的元素, 存取 和 查找性能很好;

(2) NSSet 与 NSArray 的相同之处

NSSet 与 NSArray 相同之处 :

-- 获取元素数量 : 调用 count 方法获取集合元素数量;

-- 快速枚举 : 都可以通过 for (id object in collection) 快速枚举来遍历元素;

-- 枚举器 : 都可以通过 objectEnumerator 方法获取 NSEnumerator 枚举器对集合元素进行遍历;

-- 方法遍历 : 都可以通过 "makeObjectPerformSelector : " 和 "makeObjectPerformSelector : withObject : " 方法对集合元素整体调用某个方法;

-- 代码块遍历 : 都可以通过 "enumerateObjectUsingBlock : " 和 "enumerateObjectWithOptions : usingBlock : " 使用代码块遍历执行集合元素;

-- KVC 编程 : 都可以通过 "valueForKey : " 和 "setValue : forKey : " 进行所有元素的 KVC 编程;

-- KVO 编程 : 都可以通过 "addObserver : forKeyPath : options : context : " 等方法进行 KVO 编程;

(3) NSSet 常用方法

NSSet 常用方法 :

-- "setByAddingObject : " 方法 : 向集合中添加一个新元素, 返回新集合;

-- "setByAddingObjectFromSet : " 方法 : 向 NSSet 集合中添加一个 NSSet 集合, 返回新集合;

-- "setByAddingObjectFromArray : " 方法 : 向 NSSet 集合中添加一个 NSArray 集合, 返回新集合;

-- "allObjects : " 方法 : 将 NSSet 集合中所有元素组成 NSArray , 并返回 NSArray 集合;

-- "anyObject : " 方法 : 返回 NSSet 集合中的某个元素;

-- "containsObject : " 方法 : 判断是否包含 某元素;

-- "member : "方法 : 判断是否包含指定元素, 如果包含返回该元素, 否则返回 nil;

-- "objectPassingTest : " 方法 : 使用代码块过滤集合元素, 通过验证的元素组成新的 NSSet 集合并返回该集合;

-- "objectWithOptions : passingTest : " 方法 : 在 "objectPassingTest : " 基础上额外传入一个 NSEnumerationOptions 参数遍历元素;

-- "isSubsetOfSet : " 方法 : 判断当前 NSSet 集合是否是另外一个集合的子集合;

-- "intersectsSet : " 方法 : 计算两个集合是否有交集;

-- "isEqualToSet : " 方法 : 判断两个集合元素是否相等;

(4) NSSet 代码示例

NSSet 代码示例 :

-- 代码 :

  1. //
  2. // main.m
  3. // 05.NSSet
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSSet * set = [NSSet setWithObjects:@"Tom", @"Jerry", @"Hank", nil];
  17.  
  18. //输出集合大小
  19. NSLog(@"set 集合大小 : %ld", [set count]);
  20.  
  21. NSSet * set1 = [NSSet setWithObjects:@"Angle", @"Devil", nil];
  22. //添加单个元素
  23. set1 = [set1 setByAddingObject:@"Han"];
  24. //添加整个集合
  25. set1 = [set1 setByAddingObjectsFromSet:set];
  26.  
  27. //输出合并后的 NSSet
  28. NSLog(@"set 与 set1 合并后的 NSSet : \n%@", set1);
  29.  
  30. //判断交集
  31. BOOL b1 = [set intersectsSet:set1];
  32. NSLog(@"set1 与 set 是否有交集 : %d", b1);
  33.  
  34. //判断 set 是否是 set1 的子集
  35. BOOL b2 = [set isSubsetOfSet:set1];
  36. NSLog(@"set 是否是 set1 的子集 : %d", b2);
  37.  
  38. //判断 set 是否包含 Tom 字符串
  39. BOOL b3 = [set containsObject:@"Tom"];
  40. NSLog(@"set 是否包含 Tom : %d", b3);
  41.  
  42. //过滤
  43. NSSet * set2 = [set1 objectsPassingTest:^BOOL(id obj, BOOL *stop) {
  44. return (BOOL)([obj length] > 4);
  45. }];
  46. NSLog(@"过滤后的 set2 : \n%@", set2);
  47.  
  48. }
  49. return 0;
  50. }

-- 执行结果 :

  1. 2015-10-18 10:49:22.118 05.NSSet[11008:303] set 集合大小 : 3
  2. 2015-10-18 10:49:22.120 05.NSSet[11008:303] set set1 合并后的 NSSet :
  3. {(
  4. Hank,
  5. Jerry,
  6. Han,
  7. Devil,
  8. Angle,
  9. Tom
  10. )}
  11. 2015-10-18 10:49:22.120 05.NSSet[11008:303] set1 set 是否有交集 : 1
  12. 2015-10-18 10:49:22.121 05.NSSet[11008:303] set 是否是 set1 的子集 : 1
  13. 2015-10-18 10:49:22.121 05.NSSet[11008:303] set 是否包含 Tom : 1
  14. 2015-10-18 10:49:22.122 05.NSSet[11008:303] 过滤后的 set2 :
  15. {(
  16. Jerry,
  17. Devil,
  18. Angle
  19. )}
  20. Program ended with exit code: 0

2. NSSet 判重原理

(1) NSSet 重复判定原理

NSSet 添加元素判断 :

-- 位置判断 : 向 NSSet 中添加元素时, NSSet 会调用对象的 hash 方法 获取对象的 哈希值, 根据 HashCode 判断元素在 NSSet 哈希表中的存储位置;

-- 重复判断 : 两个元素 HashCode 相同, 通过 isEqual : 方法判断, 如果返回 NO 则将两个元素放在同一个位置, 如果返回 YES 添加失败;

NSSet 判断元素相同标准 :

-- 值相等 : 调用 isEqual : 方法返回 YES;

-- hashCode 相等 : 元素的 hash 方法返回值相同;

(2) hash 方法规则

注意 :

-- 重写对象 : 如果重写了对象的 isEqual 方法, 其 hash 方法也应该被重写, 尽量保持 isEqual 方法返回 YES 时, hash 方法也返回 YES;

-- 关于同一位置存储 : hashCode 相同, isEqual 不同的时候, 同一个位置存储多个元素, 需要用链表连接这些元素, 这会降低 NSSet 访问性能;

hash 方法规则 :

-- 同一对象返回的值相同;

-- isEqual 方法返回值相同时, hash 方法返回的值也应该相同;

-- isEqual 标准的实例变量应该用 hashCode 计算;

(3) 代码实例

代码实例 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14.  
  15. - (id) initWithName : (NSString *) _name age : (int) _age;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) initWithName:(NSString *)_name age:(int)_age
  16. {
  17. self = [super init];
  18. self.name = _name;
  19. self.age = _age;
  20. return self;
  21. }
  22.  
  23. - (BOOL) isEqual : (id) other
  24. {
  25. //如果地址相同, 那么比较结果肯定相同
  26. if(self == other)
  27. {
  28. return YES;
  29. }
  30.  
  31. //对象对比之前首先保证类型相同
  32. if([other class] == OCCat.class)
  33. {
  34. OCCat * cat = (OCCat *)other;
  35. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  36. }
  37. return NO;
  38. }
  39.  
  40. - (NSUInteger) hash
  41. {
  42. NSUInteger name_hash = [name hash];
  43.  
  44. return name_hash + age;
  45. }
  46.  
  47. //%@ 打印的时候输出的内容
  48. - (NSString *) description
  49. {
  50. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  51. }
  52.  
  53. @end

-- main.m :

  1. //
  2. // main.m
  3. // 05.NSSet
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSSet * set = [NSSet setWithObjects:
  17. [[OCCat alloc] initWithName:@"Tom" age:18],
  18. [[OCCat alloc] initWithName:@"Jerry" age:20],
  19. [[OCCat alloc] initWithName:@"Tom" age:18],
  20. nil];
  21. NSLog(@"set 集合个数 : %ld \n内容 : \n%@", [set count], set);
  22.  
  23. }
  24. return 0;
  25. }

-- 执行结果 :

  1. 2015-10-18 12:17:44.740 05.NSSet[11464:303] set 集合个数 : 2
  2. 内容 :
  3. {(
  4. <OCCat [name = Tom, age = 18]>,
  5. <OCCat [name = Jerry, age = 20]>
  6. )}
  7. Program ended with exit code: 0

3. NSMutableSet 功能与用法

(1) NSMutableSet 简介

NSMutableSet 简介 : NSMutableSet 可以动态添加, 删除, 修改 集合元素, 可计算集合的 交集 并集 差集;

-- "addObject : " 方法 : 向集合中添加单个元素;

-- "removeObject : " 方法 : 从集合中删除单个元素;

-- "removeAllObjects" 方法 : 删除集合中所有元素;

-- "addObjectsFromArray : " 方法 : 向 NSSet 集合中添加 NSArray 集合中的所有元素;

-- "unionSet : " 方法 : 计算两个 NSSet 并集;

-- "minusSet : " 方法 : 计算两个 NSSet 集合的差集;

-- "setSet : " 方法 : 用一个集合的元素 替换 已有集合的所有的元素;

(2) NSMutableSet 代码示例

NSMutableSet 示例代码 :

-- 示例代码 :

  1. //
  2. // main.m
  3. // 05.NSSet
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSMutableSet * set = [NSMutableSet setWithCapacity:5];
  17. //添加一个
  18. [set addObject:@"Tom"];
  19. //添加3个 元素 从 NSArray
  20. [set addObjectsFromArray:[NSArray arrayWithObjects:@"Jerry", @"Cat", @"Mouse", nil]];
  21. //去掉一个
  22. [set removeObject:@"Mouse"];
  23. NSLog(@"添加一个 添加 3个 去掉一个后的 set : \n%@", set);
  24.  
  25. NSSet * set2 = [NSSet setWithObjects:@"Tom", @"Hank", nil];
  26. [set unionSet:set2];
  27. NSLog(@"合集是 : \n%@", set);
  28.  
  29. }
  30. return 0;
  31. }

-- 执行结果 :

  1. 2015-10-18 17:57:14.948 05.NSSet[11656:303] 添加一个 添加 3 去掉一个后的 set :
  2. {(
  3. Jerry,
  4. Cat,
  5. Tom
  6. )}
  7. 2015-10-18 17:57:14.950 05.NSSet[11656:303] 合集是 :
  8. {(
  9. Jerry,
  10. Cat,
  11. Hank,
  12. Tom
  13. )}
  14. 2015-10-18 17:57:14.950 05.NSSet[11656:303] 差集是 :
  15. {(
  16. Jerry,
  17. Cat
  18. )}
  19. Program ended with exit code: 0

4. NSCountedSet 功能与用法

(1) NSCountedSet 简介

NSCountedSet 简介 :

-- 增加元素 : 该类是 NSMutableSet 的子类, 为每个元素维护了一个 计数 状态, 向该 NSCountedSet 中添加一个元素时, 如果添加成功, 则该元素的添加计数 标注为 1, 如果添加失败, 会再该元素的添加计数 +1;

-- 删除元素 : 从 NSCountedSet 集合中删除元素时, 计数会 -1, 某元素的添加计数减到 0 会删除这个元素;

-- "countForObject : " 方法 : 获取指定元素的添加次数;

(2) NSCountedSet 示例代码

示例代码 :

-- 代码 :

  1. //
  2. // main.m
  3. // 05.NSSet
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSCountedSet * set = [NSCountedSet setWithObjects:@"Tom", @"Jerry", @"Hank", nil];
  17. [set addObject:@"Tom"];
  18. [set addObject:@"Tom"];
  19.  
  20. NSLog(@"set : \n%@", set);
  21.  
  22. [set removeObject:@"Tom"];
  23. NSLog(@"set 删除一次后的效果 : \n%@", set);
  24.  
  25. [set removeObject:@"Tom"];
  26. [set removeObject:@"Tom"];
  27.  
  28. NSLog(@"set 删除三次后的效果 : \n%@", set);
  29.  
  30. }
  31. return 0;
  32. }

-- 执行结果 :

  1. 2015-10-18 18:10:56.253 05.NSSet[11746:303] set :
  2. <NSCountedSet: 0x100200330> (Tom [3], Jerry [1], Hank [1])
  3. 2015-10-18 18:10:56.255 05.NSSet[11746:303] set 删除一次后的效果 :
  4. <NSCountedSet: 0x100200330> (Tom [2], Jerry [1], Hank [1])
  5. 2015-10-18 18:10:56.255 05.NSSet[11746:303] set 删除三次后的效果 :
  6. <NSCountedSet: 0x100200330> (Jerry [1], Hank [1])
  7. Program ended with exit code: 0

5. NSOrderedSet 与 NSMutableOrderedSet 有序集合

(1) 有序集合简介

NSOrderedSet 简介 :

-- 特点 : 不允许重复, 可以保持元素添加顺序, 每个元素都有索引, 可以根据索引操作元素;

(2) 有序集合代码示例

有序集合代码示例 :

-- main.m 代码 :

  1. //
  2. // main.m
  3. // 05.NSSet
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSOrderedSet * set = [NSOrderedSet orderedSetWithObjects:@"Tom", @"Jerry", @"Hank", nil];
  17.  
  18. NSLog(@"第1个元素 : %@", [set firstObject]);
  19. NSLog(@"第2个元素 : %@", [set objectAtIndex : 1]);
  20. NSLog(@"最后1个元素 : %@", [set lastObject]);
  21. NSLog(@"Tom 的索引 : %ld", [set indexOfObject : @"Tom"]);
  22.  
  23. NSIndexSet * index_set = [set indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  24. return (BOOL) ([obj length] > 3);
  25. }];
  26. NSLog(@"%@", index_set);
  27.  
  28. }
  29. return 0;
  30. }

-- 执行结果 :

  1. 2015-10-18 18:31:30.562 05.NSSet[11925:303] 1个元素 : Tom
  2. 2015-10-18 18:31:30.564 05.NSSet[11925:303] 2个元素 : Jerry
  3. 2015-10-18 18:31:30.565 05.NSSet[11925:303] 最后1个元素 : Hank
  4. 2015-10-18 18:31:30.565 05.NSSet[11925:303] Tom 的索引 : 0
  5. 2015-10-18 18:31:30.566 05.NSSet[11925:303] <NSIndexSet: 0x100103760>[number of indexes: 2 (in 1 ranges), indexes: (1-2)]
  6. Program ended with exit code: 0

六. NSDictionary 与 NSMutableDictionary 字典集合

1. NSDictionary 功能与用法

(1) NSDictionary 简介

NSDictionary 简介 :

-- 作用 : NSDictionary 集合用于保存具有映射关系的数据, 其中保存两组数据, 一组是 key, 一组是 value, key 不允许重复;

-- key 与 value 对应关系 : key 与 value 是一一对应的, 每个 key 都能找到对应的 value;

(2) NSDictionary 方法简介

NSDictionary 创建方法简介 :

-- "dictionary : " 方法 : 创建不包含任何 key value 的 NSDictionary 集合;

-- "dictionaryWithContentsOfFile : / initWithContentsOfFile : " 方法 : 读取文件, 使用文件内容生成 NSDictionary 集合, 一般这种文件是从 NSDictionary 集合输出的, 需要有指定的格式;

-- "dictionaryWithDictionary : / initWithDictionary : " 方法 : 使用现有的 NSDictionary 生成新的 NSDictionary;

-- "dictionaryWithObject : forKey : " 方法 : 使用单个 key-value 创建 NSDictionary 集合;

-- "dictionaryWithObjects : forKeys : / initWithObjects : forKeys : " 方法 : 使用两个 NSArray 分别指定 key 和 value 集合;

-- "dictionaryWithObjectsAndKeys : / initWithObjectAndKeys : " 方法 : 调用该方法, 需要按照 key-value 格式传入多个 键值对集合;

(3) NSDictionary 常用方法

NSDictionary 常用方法 :

-- "count : " 方法 : 返回 NSDictionary 含有的 key-value 的数量;

-- "allKeys : " 方法 : 返回 NSDictionary 集合含有的 key 集合;

-- "allKeysForObject : " 方法 : 返回指定 Value 的全部 key 的集合;

-- "allValues : " 方法 : 该方法返回 NSDictionary 包含的全部 value 集合;

-- "objectForKeyedSubscript : " 方法 : 允许 NSDictionary 通过下标方法获取指定 key 的 value;

-- "valueForKey : " 方法 : 获取 NSDictionary 指定 key 的 value 值;

-- "keyEnumerator : " 方法 : 返回 key 集合的 NSEnumerator 对象;

-- "objectEnumerator : " 方法 : 返回 value 集合的 NSEnumerator 对象;

-- "enumerateKeysAndObjectsUsingBlock : " 方法 : 使用指定的代码块迭代集合中的键值对;

-- "enumerateKeysAndObjectsWithOptions : usingBlock : " 方法 : 使用指定代码块迭代集合中的键值对, 额外传入一个 NSEnumerationOptions 参数;

-- "writeToFile : atomically : " 方法 : 将 NSDictionary 对象数据写入文件中;

(4) NSDictionary 示例代码

NSDictionary 示例代码 :

-- NSDictionary+toString.h :

  1. //
  2. // NSDictionary+toString.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface NSDictionary (toString)
  12. - (void) toString;
  13. @end

-- NSDictionary+toString.m :

  1. //
  2. // NSDictionary+toString.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "NSDictionary+toString.h"
  10.  
  11. @implementation NSDictionary (toString)
  12. - (void) toString
  13. {
  14. NSMutableString * string = [NSMutableString stringWithString:@"{"];
  15.  
  16. for(id key in self)
  17. {
  18. [string appendString:[key description]];
  19. [string appendString:@" = "];
  20. [string appendString:[self[key] description]];
  21. [string appendString:@" , "];
  22. }
  23.  
  24. [string appendString:@"}"];
  25. NSLog(@"%@", string);
  26. }
  27. @end

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14.  
  15. - (id) initWithName : (NSString *) _name age : (int) _age;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) initWithName:(NSString *)_name age:(int)_age
  16. {
  17. self = [super init];
  18. self.name = _name;
  19. self.age = _age;
  20. return self;
  21. }
  22.  
  23. - (BOOL) isEqual : (id) other
  24. {
  25. //如果地址相同, 那么比较结果肯定相同
  26. if(self == other)
  27. {
  28. return YES;
  29. }
  30.  
  31. //对象对比之前首先保证类型相同
  32. if([other class] == OCCat.class)
  33. {
  34. OCCat * cat = (OCCat *)other;
  35. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  36. }
  37. return NO;
  38. }
  39.  
  40. - (NSUInteger) hash
  41. {
  42. NSUInteger name_hash = [name hash];
  43.  
  44. return name_hash + age;
  45. }
  46.  
  47. //%@ 打印的时候输出的内容
  48. - (NSString *) description
  49. {
  50. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  51. }
  52. @end

-- main.m :

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "NSDictionary+toString.h"
  11. #import "OCCat.h"
  12.  
  13. int main(int argc, const char * argv[])
  14. {
  15.  
  16. @autoreleasepool {
  17.  
  18. NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  19. [[OCCat alloc] initWithName:@"Tom" age:18], @"Tom",
  20. [[OCCat alloc] initWithName:@"Jerry" age:20], @"Jerry",
  21. [[OCCat alloc] initWithName:@"Hank" age:26], @"Hank",
  22. nil];
  23.  
  24. //自己实现的 NSDIctionary 打印方法
  25. [dictionary toString];
  26.  
  27. NSLog(@"dictionary 键值对个数 : %ld", [dictionary count]);
  28.  
  29. NSLog(@"打印所有 key : \n%@", [dictionary allKeys]);
  30.  
  31. NSLog(@"打印所有 value : \n%@", [dictionary allValues]);
  32.  
  33. //枚举器遍历
  34. NSEnumerator * enumerator = [dictionary objectEnumerator];
  35. NSObject * value;
  36. while(value = [enumerator nextObject])
  37. {
  38. NSLog(@"%@", value);
  39. }
  40.  
  41. //使用代码块遍历 NSDictionary 集合
  42. [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  43.  
  44. NSLog(@"key : %@ , value : %@", key, obj);
  45. }];
  46.  
  47. }
  48. return 0;
  49. }

-- 执行结果 :

  1. 2015-10-18 21:49:15.902 06.NSDictionary[12562:303] {Tom = <OCCat [name = Tom, age = 18]> , Jerry = <OCCat [name = Jerry, age = 20]> , Hank = <OCCat [name = Hank, age = 26]> , }
  2. 2015-10-18 21:49:15.904 06.NSDictionary[12562:303] dictionary 键值对个数 : 3
  3. 2015-10-18 21:49:15.904 06.NSDictionary[12562:303] 打印所有 key :
  4. (
  5. Tom,
  6. Jerry,
  7. Hank
  8. )
  9. 2015-10-18 21:49:15.905 06.NSDictionary[12562:303] 打印所有 value :
  10. (
  11. "<OCCat [name = Tom, age = 18]>",
  12. "<OCCat [name = Jerry, age = 20]>",
  13. "<OCCat [name = Hank, age = 26]>"
  14. )
  15. 2015-10-18 21:49:15.910 06.NSDictionary[12562:303] <OCCat [name = Tom, age = 18]>
  16. 2015-10-18 21:49:15.910 06.NSDictionary[12562:303] <OCCat [name = Jerry, age = 20]>
  17. 2015-10-18 21:49:15.910 06.NSDictionary[12562:303] <OCCat [name = Hank, age = 26]>
  18. 2015-10-18 21:49:15.911 06.NSDictionary[12562:303] key : Tom , value : <OCCat [name = Tom, age = 18]>
  19. 2015-10-18 21:49:15.911 06.NSDictionary[12562:303] key : Jerry , value : <OCCat [name = Jerry, age = 20]>
  20. 2015-10-18 21:49:15.912 06.NSDictionary[12562:303] key : Hank , value : <OCCat [name = Hank, age = 26]>
  21. Program ended with exit code: 0

2. NSDictionary key 排序

(1) NSDictionary Key 排序简介

NSDictionary 排序方法 :

-- "keysSortedByValueUsingSelector : " 方法 : 根据 value 指定方法 遍历 key-value 键值对, 对 key 排序, value 的指定方法必返回 NSOrderedSame, NSOrderedAscending 或 NSOrderedDescending 返回值;

-- "keysSortedByValueUsingComparator" 方法 : 根据 指定代码块 遍历 键值对, 对 key 进行排序, 代码块方法 需要返回 NSOrderedAscending, NSOrderedDescending 或 NSOrderedSame 返回值;

-- "keysSortedByValueWithOptions : usingComparator : " 方法 : 与前一个类似, 额外增加了一个 NSEnumerationOptions 参数;

(2) NSDictionary key排序示例代码

NSDictionary Key 排序示例代码 :

-- OCCat.h : 定义 compare : 比较方法;

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14.  
  15. - (id) initWithName : (NSString *) _name age : (int) _age;
  16. - (NSComparisonResult) compare : (id) other;
  17. @end

-- OCCat.m : 实现 compare : 比较方法;

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) initWithName:(NSString *)_name age:(int)_age
  16. {
  17. self = [super init];
  18. self.name = _name;
  19. self.age = _age;
  20. return self;
  21. }
  22.  
  23. - (BOOL) isEqual : (id) other
  24. {
  25. //如果地址相同, 那么比较结果肯定相同
  26. if(self == other)
  27. {
  28. return YES;
  29. }
  30.  
  31. //对象对比之前首先保证类型相同
  32. if([other class] == OCCat.class)
  33. {
  34. OCCat * cat = (OCCat *)other;
  35. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  36. }
  37. return NO;
  38. }
  39.  
  40. - (NSUInteger) hash
  41. {
  42. NSUInteger name_hash = [name hash];
  43.  
  44. return name_hash + age;
  45. }
  46.  
  47. //%@ 打印的时候输出的内容
  48. - (NSString *) description
  49. {
  50. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  51. }
  52.  
  53. - (NSComparisonResult) compare:(id)other
  54. {
  55. if([other class] == OCCat.class)
  56. {
  57. OCCat * other_cat = (OCCat *)other;
  58. if(self.age == other_cat.age)
  59. {
  60. return NSOrderedSame;
  61. }
  62. else if(self.age > other_cat.age)
  63. {
  64. return NSOrderedDescending;
  65. }
  66. else
  67. {
  68. return NSOrderedAscending;
  69. }
  70. }
  71. return NSOrderedSame;
  72. }
  73. @end

-- main.m :

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "NSDictionary+toString.h"
  11. #import "OCCat.h"
  12.  
  13. int main(int argc, const char * argv[])
  14. {
  15.  
  16. @autoreleasepool {
  17.  
  18. NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  19. [[OCCat alloc] initWithName:@"Tom" age:18], @"Tom",
  20. [[OCCat alloc] initWithName:@"Hank" age:26], @"Hank",
  21. [[OCCat alloc] initWithName:@"Jerry" age:20], @"Jerry",
  22. nil];
  23.  
  24. //自己实现的 NSDIctionary 打印方法
  25. [dictionary toString];
  26.  
  27. //使用 keysSortedByValueUsingSelector 方法排序
  28. NSArray * array = [dictionary keysSortedByValueUsingSelector:@selector(compare:)];
  29. NSLog(@"array : \n%@", array);
  30.  
  31. //使用代码块排序
  32. NSArray * array2 = [dictionary keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  33. OCCat * cat1 = (OCCat *)obj1;
  34. OCCat * cat2 = (OCCat *)obj2;
  35. if(cat1.age == cat2.age)
  36. {
  37. return NSOrderedSame;
  38. }
  39.  
  40. if(cat1.age > cat2.age)
  41. {
  42. return NSOrderedDescending;
  43. }
  44.  
  45. return NSOrderedAscending;
  46. }];
  47. NSLog(@"array2 : \n%@", array2);
  48.  
  49. }
  50. return 0;
  51. }

-- 执行结果 :

  1. 2015-10-19 12:57:17.276 06.NSDictionary[13537:303] {Tom = <OCCat [name = Tom, age = 18]> , Jerry = <OCCat [name = Jerry, age = 20]> , Hank = <OCCat [name = Hank, age = 26]> , }
  2. 2015-10-19 12:57:17.279 06.NSDictionary[13537:303] array2 :
  3. (
  4. Tom,
  5. Jerry,
  6. Hank
  7. )
  8. 2015-10-19 12:57:17.279 06.NSDictionary[13537:303] array :
  9. (
  10. Tom,
  11. Jerry,
  12. Hank
  13. )
  14. Program ended with exit code: 0

3. NSDictionary key 过滤

(1) NSDictionary Key 过滤方法简介

NSDictionary key 过滤方法简介 :

-- "keysOfEntriesPassingTest : " 方法 : 使用代码块 遍历 键值对, 代码块返回一个 BOOL 值, 参数一 是 key, 参数二 是 value, 第三个是循环标志位;

-- "keysOfEntriesWithOptions : passingTest : " 方法 : 与前一个方法功能基本相同, 增加了 NSEnumerationOptions 参数;

(2) NSDictionary Key 过滤代码示例

示例代码 : main.m 不同, 其它代码均相同;

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "NSDictionary+toString.h"
  11. #import "OCCat.h"
  12.  
  13. int main(int argc, const char * argv[])
  14. {
  15.  
  16. @autoreleasepool {
  17.  
  18. NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  19. [[OCCat alloc] initWithName:@"Tom" age:18], @"Tom",
  20. [[OCCat alloc] initWithName:@"Hank" age:26], @"Hank",
  21. [[OCCat alloc] initWithName:@"Jerry" age:20], @"Jerry",
  22. nil];
  23.  
  24. //自己实现的 NSDIctionary 打印方法
  25. [dictionary toString];
  26.  
  27. NSSet * set = [dictionary keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {
  28. OCCat * cat = (OCCat *)obj;
  29. if(cat.age >= 20)
  30. {
  31. return YES;
  32. }
  33. return NO;
  34. }];
  35.  
  36. NSLog(@"过滤后的key集合 : \n%@", set);
  37.  
  38. }
  39. return 0;
  40. }

-- 执行结果 :

  1. 2015-10-19 13:59:42.931 06.NSDictionary[13750:303] {Tom = <OCCat [name = Tom, age = 18]> , Jerry = <OCCat [name = Jerry, age = 20]> , Hank = <OCCat [name = Hank, age = 26]> , }
  2. 2015-10-19 13:59:42.934 06.NSDictionary[13750:303] 过滤后的key集合 :
  3. {(
  4. Jerry,
  5. Hank
  6. )}
  7. Program ended with exit code: 0

4. 自定义类作为 key

(1) 自定义类为 key 前提

自定义类为 key 基本要求 :

-- 重写isEqual 和 hash 方法 : isEqual : 和 hash : 方法必须正确重写, 即 isEqual 方法返回 YES 是, hash 方法返回的 hashCode 必须也相同;

-- 实现 copyWithZone 方法 : 尽量返回对象的不可变副本; 该策略是出于安全性考虑, 当多个 key-value 放入 NSDictionary 之后, 如果 key 可变导致 key 的hashCode 改变, 就会出现问题;

(2) 自定义类为 key 代码示例

自定义类为 key 代码 : 其它类代码相同, 只有 OCCat.m 与 main.m 不同;

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) initWithName:(NSString *)_name age:(int)_age
  16. {
  17. self = [super init];
  18. self.name = _name;
  19. self.age = _age;
  20. return self;
  21. }
  22.  
  23. - (BOOL) isEqual : (id) other
  24. {
  25. //如果地址相同, 那么比较结果肯定相同
  26. if(self == other)
  27. {
  28. return YES;
  29. }
  30.  
  31. //对象对比之前首先保证类型相同
  32. if([other class] == OCCat.class)
  33. {
  34. OCCat * cat = (OCCat *)other;
  35. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  36. }
  37. return NO;
  38. }
  39.  
  40. - (NSUInteger) hash
  41. {
  42. NSUInteger name_hash = [name hash];
  43.  
  44. return name_hash + age;
  45. }
  46.  
  47. //%@ 打印的时候输出的内容
  48. - (NSString *) description
  49. {
  50. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  51. }
  52.  
  53. - (id) copyWithZone : (NSZone *) zone
  54. {
  55. OCCat * cat = [[[self class] allocWithZone:zone] init];
  56. cat.name = self.name;
  57. cat.age = self.age;
  58. return cat;
  59. }
  60.  
  61. - (NSComparisonResult) compare:(id)other
  62. {
  63. if([other class] == OCCat.class)
  64. {
  65. OCCat * other_cat = (OCCat *)other;
  66. if(self.age == other_cat.age)
  67. {
  68. return NSOrderedSame;
  69. }
  70. else if(self.age > other_cat.age)
  71. {
  72. return NSOrderedDescending;
  73. }
  74. else
  75. {
  76. return NSOrderedAscending;
  77. }
  78. }
  79. return NSOrderedSame;
  80. }
  81. @end

-- main.m :

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "NSDictionary+toString.h"
  11. #import "OCCat.h"
  12.  
  13. int main(int argc, const char * argv[])
  14. {
  15.  
  16. @autoreleasepool {
  17.  
  18. NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  19. @"Tom", [[OCCat alloc] initWithName:@"Tom" age:18],
  20. @"Hank", [[OCCat alloc] initWithName:@"Hank" age:26],
  21. @"Jerry", [[OCCat alloc] initWithName:@"Jerry" age:20],
  22. nil];
  23.  
  24. //自己实现的 NSDIctionary 打印方法
  25. [dictionary toString];
  26.  
  27. }
  28. return 0;
  29. }

-- 执行结果 : 

  1. 2015-10-20 14:09:30.935 06.NSDictionary[15258:303] {<OCCat [name = Tom, age = 18]> = Tom , <OCCat [name = Hank, age = 26]> = Hank , <OCCat [name = Jerry, age = 20]> = Jerry , }
  2. Program ended with exit code: 0

5. NSMutableDictionary 功能与用法

(1) NSMutableDictionary 简介

NSMutableDictionary 独有方法简介 : 主要是比 NSDictionary 多个 增加 删除 键值对的方法;

-- "setObject : forKey : " 方法 : 设置 key-value 键值对, 如果 key 与之前的不重复, 直接插入, 如果重复, 直接将 value 替换;

-- "setObject : forKeyedSubscript : " 方法 : 通过下标方法设置键值对;

-- "addEntriesFromDictionary : " 方法 : 将另一个 NSDictionary 中的值复制到当前的 NSMutableDictionary 中;

-- "setDictionary : " 方法 : 用一个 NSDictionary 中的所有元素 替换另一个 NSDictionary 中的所有元素;

-- "removeObjectForKey : " 方法 : 根据 key 删除 键值对;

-- "removeAllObjects : " 方法 : 清空 NSDictionary;

-- "removeObjectForKeys : " 方法 : 删除一个 NSArray 中包含的 key 的键值对;

(2) NSMutableDictionary 方法示例

NSMutableDictionary 示例代码 :

-- 代码 :

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "NSDictionary+toString.h"
  11. #import "OCCat.h"
  12.  
  13. int main(int argc, const char * argv[])
  14. {
  15.  
  16. @autoreleasepool {
  17.  
  18. NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  19. @"Tom", [NSNumber numberWithInt:1],
  20. @"Jerry", [NSNumber numberWithInt:2],
  21. nil];
  22. [dictionary toString];
  23.  
  24. //替换
  25. NSLog(@"替换");
  26. dictionary[[NSNumber numberWithInt:1]] = @"Hank";
  27. [dictionary toString];
  28.  
  29. //增加
  30. NSLog(@"增加");
  31. dictionary[[NSNumber numberWithInt:3]] = @"Fuck";
  32. [dictionary toString];
  33.  
  34. //添加整个字典到另一个字典
  35. NSLog(@"添加整个字典到另一个字典");
  36. NSDictionary * dic1 = [NSDictionary dictionaryWithObjectsAndKeys:
  37. @"Obama", [NSNumber numberWithInt:4],
  38. @"Bush", [NSNumber numberWithInt:5],
  39. nil];
  40. [dictionary addEntriesFromDictionary:dic1];
  41. [dictionary toString];
  42.  
  43. //删除一部分
  44. NSLog(@"删除一部分");
  45. [dictionary removeObjectsForKeys:[NSArray arrayWithObjects:
  46. [NSNumber numberWithInt:4], [NSNumber numberWithInt:5], nil]];
  47. [dictionary toString];
  48.  
  49. }
  50. return 0;
  51. }

-- 执行结果 :

  1. 2015-10-20 15:04:01.100 06.NSDictionary[15426:303] {1 = Tom , 2 = Jerry , }
  2. 2015-10-20 15:04:01.102 06.NSDictionary[15426:303] 替换
  3. 2015-10-20 15:04:01.103 06.NSDictionary[15426:303] {1 = Hank , 2 = Jerry , }
  4. 2015-10-20 15:04:01.103 06.NSDictionary[15426:303] 增加
  5. 2015-10-20 15:04:01.104 06.NSDictionary[15426:303] {3 = Fuck , 1 = Hank , 2 = Jerry , }
  6. 2015-10-20 15:04:01.105 06.NSDictionary[15426:303] 添加整个字典到另一个字典
  7. 2015-10-20 15:04:01.106 06.NSDictionary[15426:303] {3 = Fuck , 2 = Jerry , 5 = Bush , 1 = Hank , 4 = Obama , }
  8. 2015-10-20 15:04:01.106 06.NSDictionary[15426:303] 删除一部分
  9. 2015-10-20 15:04:01.107 06.NSDictionary[15426:303] {3 = Fuck , 2 = Jerry , 1 = Hank , }
  10. Program ended with exit code: 0

六. 谓词 NSPredicate

1. 谓词简介

(1) 谓词简介

谓词简介 : 个人感觉 谓词比较像 Java 中的正则表达式;

-- 作用 : 谓词用于定义 逻辑条件, 用于 搜索 或 过滤内存中的数据, 尤其是 搜索过滤集合中的数据;

-- NSPredicate 子类 : NSPredicate 有 3 个子类, NSComparisonPredicate , NSCompoundPredicate, NSExpression;

-- 创建方法 : 使用 NSPredicate 的 "predicateWithFormat :" 方法 创建 NSPredicate 对象;

-- 没有占位符的谓词结果计算 : 直接使用 NSPredicate 的 evalueWithObject 方法计算谓词结果, 返回值是一个 BOOL 值;

-- 有占位符的谓词结果计算 : 调用 "predicateWithSubstitutionVariables" 方法为占位符参数设置参数值, 然后执行 evalueWithObject 方法计算结果;

-- 同时完成两步的方法 : NSPredicate 提供了一个 "evalueWithObject : substitutionVariables : " 方法可以同时替换占位符 和 计算结果;

(2) 谓词 代码简单示例

谓词代码示例 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14.  
  15. - (id) initWithName : (NSString *) _name age : (int) _age;
  16. - (NSComparisonResult) compare : (id) other;
  17. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) initWithName:(NSString *)_name age:(int)_age
  16. {
  17. self = [super init];
  18. self.name = _name;
  19. self.age = _age;
  20. return self;
  21. }
  22.  
  23. - (BOOL) isEqual : (id) other
  24. {
  25. //如果地址相同, 那么比较结果肯定相同
  26. if(self == other)
  27. {
  28. return YES;
  29. }
  30.  
  31. //对象对比之前首先保证类型相同
  32. if([other class] == OCCat.class)
  33. {
  34. OCCat * cat = (OCCat *)other;
  35. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  36. }
  37. return NO;
  38. }
  39.  
  40. - (NSUInteger) hash
  41. {
  42. NSUInteger name_hash = [name hash];
  43.  
  44. return name_hash + age;
  45. }
  46.  
  47. //%@ 打印的时候输出的内容
  48. - (NSString *) description
  49. {
  50. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  51. }
  52.  
  53. - (id) copyWithZone : (NSZone *) zone
  54. {
  55. OCCat * cat = [[[self class] allocWithZone:zone] init];
  56. cat.name = self.name;
  57. cat.age = self.age;
  58. return cat;
  59. }
  60.  
  61. - (NSComparisonResult) compare:(id)other
  62. {
  63. if([other class] == OCCat.class)
  64. {
  65. OCCat * other_cat = (OCCat *)other;
  66. if(self.age == other_cat.age)
  67. {
  68. return NSOrderedSame;
  69. }
  70. else if(self.age > other_cat.age)
  71. {
  72. return NSOrderedDescending;
  73. }
  74. else
  75. {
  76. return NSOrderedAscending;
  77. }
  78. }
  79. return NSOrderedSame;
  80. }
  81. @end

-- main.m : 其它类 与 NSDictionary 中的示例相同;

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16.  
  17. NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name like 'T*'"];
  18. OCCat * cat = [[OCCat alloc] initWithName:@"Tom" age:18];
  19. OCCat * cat2 = [[OCCat alloc] initWithName:@"Jerry" age:20];
  20.  
  21. NSLog(@"cat : %d, cat2 : %d", [predicate evaluateWithObject:cat], [predicate evaluateWithObject:cat2]);
  22.  
  23. }
  24. return 0;
  25. }

-- 执行结果 :

  1. 2015-10-20 16:21:32.998 06.NSDictionary[15545:303] cat : 1, cat2 : 0
  2. Program ended with exit code: 0

2. 谓词过滤集合

(1) 集合过滤方法简介

谓词方法简介 : 谓词遍历集合时, 使用谓词对集合中的元素进行过滤, 元素计算谓词返回 YES 才会被保留下来, 返回 NO, 该元素就会被删除;

-- "- (NSArray * ) filteredArrayUsingPredicate : (NSPredicate *) predicate :" 方法 : 使用谓词过滤 NSArray 集合, 返回过滤后的新集合;

-- "- (void) filterUsingPredicate : (NSPredicate *) predicate :" 方法 : 使用谓词过滤 NSMutableArray 集合, 直接删除集合中的不合格的元素;

-- "- (NSSet * ) filteredSetUsingPredicate : (NSPredicate *) Predicate :" 方法 : 使用谓词过滤 NSSet 集合, 返回一个新的集合;

-- "- (void) filterUsingPredicate : (NSPredicate * ) Predicate :" 方法 : 过滤 NSMutableSet 集合, 直接删除不合格的元素;

(2) 集合过滤方法代码

示例代码 :

-- main.c :

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16.  
  17. NSMutableArray * array = [NSMutableArray arrayWithObjects:
  18. [NSNumber numberWithInt:10],
  19. [NSNumber numberWithInt:20],
  20. [NSNumber numberWithInt:30],
  21. nil];
  22. //过滤大于 15 的数字
  23. NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF > 15"];
  24. [array filterUsingPredicate:predicate];
  25. NSLog(@"过滤后的 array : \n%@", array);
  26.  
  27. NSSet * set = [NSSet setWithObjects:
  28. [[OCCat alloc] initWithName:@"Tom" age:18],
  29. [[OCCat alloc] initWithName:@"Jerry" age:20],
  30. nil];
  31. //过滤 name 包含 om 字符串的 NSSet
  32. NSPredicate * predicate1 = [NSPredicate predicateWithFormat:@"name CONTAINS 'om'"];
  33. NSSet * filterSet = [set filteredSetUsingPredicate:predicate1];
  34. NSLog(@"过滤后的 set : \n%@", filterSet);
  35.  
  36. }
  37. return 0;
  38. }

-- 执行结果 :

  1. 2015-10-20 21:17:41.615 06.NSDictionary[15761:303] 过滤后的 array :
  2. (
  3. 20,
  4. 30
  5. )
  6. 2015-10-20 21:17:41.617 06.NSDictionary[15761:303] 过滤后的 set :
  7. {(
  8. <OCCat [name = Tom, age = 18]>
  9. )}
  10. Program ended with exit code: 0

3. 谓词 占位符

(1) 谓词占位符参数

谓词支持的占位符 :

-- "%K" 占位符 : 动态传入属性名;

-- "%@" 占位符 : 动态设置属性值;

动态改变的属性值 :

-- 示例 : [NSPredicate PredicateWithFormat : @"name CATAINS $JAVA"], $JAVA 是动态改变值, 程序可以动态改变 $JAVA 值;

-- 设置参数值 : 调用 NSPredicate 的 predicateWithSubstitutionVariables : 设置参数值, 该方法返回 NSPredicate 对象;

-- 计算结果 : 调用 NSPredicate 的 evaluateWithObject : substitutionVariables : 完成参数计算结果;

(2) 谓词占位符参数 代码示例

代码示例 :

-- main.m :

-- 执行结果 :

.

.

.

.

.

.

.

.

.

CocoaChina

Code4App

懒人 IOS 源码库

.


二. 日期 时间 API

1. 日期 时间 示例

四. NSArray NSMutableArray 数组集合

Objective-C 集合概述 : 

-- NSArray : 有序, 可重复集合;

-- NSSet : 无序, 不可重复集合;

-- NSDictionary : 有映射关系的集合;

1. NSArray 基本功能用法

(1) NSArray 创建

NSDictionary 简介 :

-- 作用 : NSDictionary 集合用于保存具有映射关系的数据, 其中保存两组数据, 一组是 key, 一组是 value, key 不允许重复;

-- key 与 value 对应关系 : key 与 value 是一一对应的, 每个 key 都能找到对应的 value;

一. 字符串 API

1. NSString 用法简介

(1) NSString API 介绍

NSString 功能 :

-- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 String 开头的方法;

  1. // init 开头方法创建字符串
  2. unichar data[5] = {97, 98, 99, 100, 101};
  3. NSString * str = [[NSString alloc] initWithCharacters : data length : 5];

  1. // string 开头方法创建字符串
  2. char * con_str = "Hello World";
  3. NSString * str1 = [NSString stringWithUTF8String : con_str];

-- 字符串获取 : 读取文件 或 网络 URL 初始化字符串;

-- 字符串写出 : 字符串内容 写入 文件 或 URL;

-- 长度获取 : 获取字符串长度, 既可获取字符串内包含的字符个数, 又可获取字符串包含的字节个数;

  1. // 字符串个数 字节数统计
  2. NSLog(@"str char count is : %lu", [str length]);
  3. NSLog(@"str utf8 byte count is : %lu", [str lengthOfBytesUsingEncoding : NSUTF8StringEncoding]);

-- 获取子字符串 : 既可以获取指定位置的字符, 又可以获取指定范围的字符串;

  1. // 获取 前 5 个字符组成的字符串
  2. NSString * str5 = [str3 substringToIndex : 5];
  3. // 获取 从 第五个字符开始的字符串
  4. NSString * str6 = [str3 substringFromIndex : 5];
  5. // 获取 从 第五个 到 第九个 字符串
  6. NSString * str7 = [str3 substringWithRange : NSMakeRange(5, 9)];

-- 获取 C 字符串 : 获取 NSString * 对应的 char * 字符串;

-- 连接字符串 : 将 2 个字符串连接;

  1. //字符串拼接
  2. NSString * str3 = [str2 stringByAppendingString : @", append this"];
  3. NSString * str4 = [str2 stringByAppendingFormat : @", append %@", @"format"];

-- 分隔字符串 : 将 一个 字符串分成两个;

-- 查找 字符 或 子字符串 : 查找字符串内指定的字符 和 子字符串;

  1. // 获取 "append" 出现的位置
  2. NSRange pos = [str3 rangeOfString : @"append"];
  3. NSLog(@"append position from %ld, length %ld", pos.location, pos.length);

-- 替换字符串 :

-- 比较字符串 :

-- 字符串大小比较 :

-- 对字符串中得字符进行大小写转换 :

  1. // 大小写转换
  2. NSString * str8 = [str2 uppercaseString];
  3. NSLog(@"str8 : %@", str8);

(2) NSString 示例代码

示例代码 :

  1. /*************************************************************************
  2. > File Name: OCNSStringDemo.m
  3. > Author: octopus
  4. > Mail: octopus_truth.163.com
  5. > Created Time: 二 10/ 6 17:04:29 2015
  6. ************************************************************************/
  7. #import <Foundation/Foundation.h>
  8.  
  9. int main(int argc, char * argv[])
  10. {
  11. @autoreleasepool {
  12. // init 开头方法创建字符串
  13. unichar data[5] = {97, 98, 99, 100, 101};
  14. NSString * str = [[NSString alloc] initWithCharacters : data length : 5];
  15.  
  16. // string 开头方法创建字符串
  17. char * con_str = "Hello World";
  18. NSString * str1 = [NSString stringWithUTF8String : con_str];
  19.  
  20. NSLog(@"str : %@, str1 : %@", str, str1);
  21.  
  22. // 将字符串写入文件
  23. [str1 writeToFile : @"octopus.txt"
  24. atomically : YES
  25. encoding : NSUTF8StringEncoding
  26. error : nil];
  27.  
  28. // 从文件读取字符串
  29. NSString * str2 = [NSString stringWithContentsOfFile : @"octopus.txt"
  30. encoding : NSUTF8StringEncoding
  31. error : nil];
  32.  
  33. NSLog(@"str2 : %@", str2);
  34.  
  35. //字符串拼接
  36. NSString * str3 = [str2 stringByAppendingString : @", append this"];
  37. NSString * str4 = [str2 stringByAppendingFormat : @", append %@", @"format"];
  38.  
  39. NSLog(@"str3 : %@, str4 : %@", str3, str4);
  40.  
  41. // 字符串个数 字节数统计
  42. NSLog(@"str char count is : %lu", [str length]);
  43. NSLog(@"str utf8 byte count is : %lu", [str lengthOfBytesUsingEncoding : NSUTF8StringEncoding]);
  44.  
  45. // 获取 前 5 个字符组成的字符串
  46. NSString * str5 = [str3 substringToIndex : 5];
  47. // 获取 从 第五个字符开始的字符串
  48. NSString * str6 = [str3 substringFromIndex : 5];
  49. // 获取 从 第五个 到 第九个 字符串
  50. NSString * str7 = [str3 substringWithRange : NSMakeRange(5, 9)];
  51.  
  52. NSLog(@"str5 : %@, str6 : %@, str7 : %@", str5, str6, str7);
  53.  
  54. // 获取 "append" 出现的位置
  55. NSRange pos = [str3 rangeOfString : @"append"];
  56. NSLog(@"append position from %ld, length %ld", pos.location, pos.length);
  57.  
  58. // 大小写转换
  59. NSString * str8 = [str2 uppercaseString];
  60. NSLog(@"str8 : %@", str8);
  61.  
  62. }
  63. }

-- 执行结果 :

  1. bogon:6.6 octopus$ clang -fobjc-arc -framework Foundation OCNSStringDemo.m
  2. bogon:6.6 octopus$ ./a.out
  3. 2015-10-06 17:52:40.007 a.out[1095:507] str : abcde, str1 : Hello World
  4. 2015-10-06 17:52:40.018 a.out[1095:507] str2 : Hello World
  5. 2015-10-06 17:52:40.018 a.out[1095:507] str3 : Hello World, append this, str4 : Hello World, append format
  6. 2015-10-06 17:52:40.019 a.out[1095:507] str char count is : 5
  7. 2015-10-06 17:52:40.020 a.out[1095:507] str utf8 byte count is : 5
  8. 2015-10-06 17:52:40.020 a.out[1095:507] str5 : Hello, str6 : World, append this, str7 : World, a
  9. 2015-10-06 17:52:40.021 a.out[1095:507] append position from 13, length 6
  10. 2015-10-06 17:52:40.021 a.out[1095:507] str8 : HELLO WORLD

2. NSMutableString 可变字符串

(1) NSMutableString 简介

NSString 与 NSMutableString 对比 :

-- NSString 缺陷 : NSString 字符串一旦被创建, 字符串序列是不可改变的;

-- NSMutableString 优点 : NSString 中所有的方法, NSMutableString 都可以调用, NSMutableString 对象可以当做 NSString 对象使用;

(2) NSMutableString 源码示例

源码示例 :

  1. /*************************************************************************
  2. > File Name: OCNSMutableStringTest.m
  3. > Author: octopus
  4. > Mail: octopus_truth.163.com
  5. > Created Time: 二 10/ 6 18:03:28 2015
  6. ************************************************************************/
  7.  
  8. #import <Foundation/Foundation.h>
  9.  
  10. int main(int argc, char * argv[])
  11. {
  12. @autoreleasepool {
  13. NSMutableString * str = [NSMutableString stringWithString : @"Hello World"];
  14.  
  15. NSLog(@"str : %@", str);
  16.  
  17. // 字符串追加
  18. [str appendString : @" Octopus"];
  19. NSLog(@"str : %@", str);
  20.  
  21. [str insertString : @"Insert " atIndex : 6];
  22. NSLog(@"str : %@", str);
  23.  
  24. [str deleteCharactersInRange : NSMakeRange (6, 2)];
  25. NSLog(@"str : %@", str);
  26.  
  27. [str replaceCharactersInRange : NSMakeRange(6, 9)
  28. withString : @" Replace "];
  29. NSLog(@"str : %@", str);
  30. }
  31. }

-- 运行结果 :

  1. bogon:6.6 octopus$ clang -fobjc-arc -framework Foundation OCNSMutableStringTest.m
  2. bogon:6.6 octopus$ ./a.out
  3. 2015-10-06 18:44:04.498 a.out[1180:507] str : Hello World
  4. 2015-10-06 18:44:04.500 a.out[1180:507] str : Hello World Octopus
  5. 2015-10-06 18:44:04.500 a.out[1180:507] str : Hello Insert World Octopus
  6. 2015-10-06 18:44:04.501 a.out[1180:507] str : Hello sert World Octopus
  7. 2015-10-06 18:44:04.501 a.out[1180:507] str : Hello Replace d Octopus

二. 日期 时间 API

1. 日期 时间 示例

示例源码 :

  1. /*************************************************************************
  2. > File Name: OCNSDateTest.m
  3. > Author: octopus
  4. > Mail: octopus_truth.163.com
  5. > Created Time: 二 10/ 6 21:12:10 2015
  6. ************************************************************************/
  7.  
  8. #import <Foundation/Foundation.h>
  9.  
  10. int main(int argc, char * argv[])
  11. {
  12. @autoreleasepool {
  13. //当前时间
  14. NSDate * date = [NSDate date];
  15. NSLog(@"date : %@", date);
  16.  
  17. //一天后的时间
  18. NSDate * date1 = [[NSDate alloc] initWithTimeIntervalSinceNow : 3600 * 24];
  19. NSLog(@"date1 : %@", date1);
  20.  
  21. //3天前时间
  22. NSDate * date2 = [[NSDate alloc] initWithTimeIntervalSinceNow : -3 * 3600 * 24];
  23. NSLog(@"date2 : %@", date2);
  24.  
  25. //获取从 1970年1月1日 开始 20年后的日期
  26. NSDate * date3 = [NSDate dateWithTimeIntervalSince1970 : 3600 * 24 * 366 * 20];
  27. NSLog(@"date3 : %@", date3);
  28.  
  29. //获取当前 Local 下对应的字符串
  30. NSLocale * cn = [NSLocale currentLocale];
  31. NSLog(@"date1 : %@", [date1 descriptionWithLocale : cn]);
  32.  
  33. //日期比较
  34. NSDate * date_earlier = [date earlierDate : date1];
  35. NSDate * date_later = [date laterDate : date1];
  36. NSLog(@"date_earlier : %@, date_later : %@", date_earlier, date_later);
  37.  
  38. //比较结果 枚举值 之前 NSOrderAscending 相同 NSOrderdSame 之后 NSOrderdDescending
  39. switch ([date compare : date1])
  40. {
  41. case NSOrderedAscending :
  42. NSLog(@"NSOrderedAscending");
  43. break;
  44. case NSOrderedSame :
  45. NSLog(@"NSOrderedSame");
  46. break;
  47. case NSOrderedDescending :
  48. NSLog(@"NSOrderedDescending");
  49. break;
  50. }
  51.  
  52. //比较时间差
  53. NSLog(@"date - date1 : %g second", [date timeIntervalSinceDate : date1]);
  54. NSLog(@"date - now : %g second", [date timeIntervalSinceNow]);
  55. }
  56. }

-- 执行结果 :

  1. bogon:~ octopus$ clang -fobjc-arc -framework Foundation OCNSDateTest.m
  2. bogon:~ octopus$ ./a.out
  3. 2015-10-06 21:45:23.441 a.out[1400:507] date : 2015-10-06 13:45:23 +0000
  4. 2015-10-06 21:45:23.442 a.out[1400:507] date1 : 2015-10-07 13:45:23 +0000
  5. 2015-10-06 21:45:23.442 a.out[1400:507] date2 : 2015-10-03 13:45:23 +0000
  6. 2015-10-06 21:45:23.442 a.out[1400:507] date3 : 1990-01-16 00:00:00 +0000
  7. 2015-10-06 21:45:23.444 a.out[1400:507] date1 : 2015107 星期三 中国标准时间下午9:45:23
  8. 2015-10-06 21:45:23.444 a.out[1400:507] date_earlier : 2015-10-06 13:45:23 +0000, date_later : 2015-10-07 13:45:23 +0000
  9. 2015-10-06 21:45:23.445 a.out[1400:507] NSOrderedAscending
  10. 2015-10-06 21:45:23.445 a.out[1400:507] date - date1 : -86400 second
  11. 2015-10-06 21:45:23.445 a.out[1400:507] date - now : -0.011359 second

2. 日期格式器

(1) NSDateFormatter 作用

NSDateFormatter 作用 : 使 NSDate 与 NSString 对象之间相互转化;

NSString 与 NSDate 转换步骤 :

-- 1.创建 NSDateFormatter 对象 :

-- 2.设置格式 : 调用 NSDateFormatter 的 "setDateStyle :", "setTimeStyle :" 方法设置时间日期格式;

-- 3. NSDate -> NSString : 调用 NSDateFormatter 的 "stringFromDate :" 方法;

-- 4. NSString -> NSDate : 调用 NSDateFormatter 的 "dateFromString :" 方法;

(2) NSDateFormatter 时间日期格式

时间 日期格式 枚举 :

-- NSDateFormatterNoStyle : 不显示日期, 时间;

-- NSDateFormatterShortStyle : 显示短日期;

-- NSDateFormatterMediumStyle : 显示中等日期;

-- NSDateFormatterLongStyle : 显示长日期;

-- NSDateFormatterFullStyle : 显示完整日期;

-- 自定义模板 : 调用 NSDateFormatter 的 "setDateFormat :" 方法 设置 时间 日期格式;

(3) NSDateFormatter 代码示例

示例代码 :

  1. /*************************************************************************
  2. > File Name: OCNSDateFormatterTest.m
  3. > Author: octopus
  4. > Mail: octopus_truth.163.com
  5. > Created Time: 三 10/ 7 14:10:32 2015
  6. ************************************************************************/
  7.  
  8. #import <Foundation/Foundation.h>
  9.  
  10. int main(int argc, char * argv[])
  11. {
  12. @autoreleasepool {
  13. //创建日期
  14. NSDate * date = [NSDate date];
  15.  
  16. //初始化 Locale 信息
  17. NSLocale * cn = [[NSLocale alloc] initWithLocaleIdentifier : @"zh_CN"];
  18. NSLocale * en = [[NSLocale alloc] initWithLocaleIdentifier : @"en_US"];
  19.  
  20. //创建并设置 NSDateFormatter
  21. NSDateFormatter * style_short = [[NSDateFormatter alloc] init];
  22. [style_short setDateStyle : NSDateFormatterShortStyle];
  23.  
  24. NSDateFormatter * style_medium = [[NSDateFormatter alloc] init];
  25. [style_medium setDateStyle : NSDateFormatterMediumStyle];
  26.  
  27. NSDateFormatter * style_long = [[NSDateFormatter alloc] init];
  28. [style_long setDateStyle : NSDateFormatterLongStyle];
  29.  
  30. NSDateFormatter * style_full = [[NSDateFormatter alloc] init];
  31. [style_full setDateStyle : NSDateFormatterFullStyle];
  32.  
  33. //打印中国格式的日期信息
  34. NSLog(@"cn short date : %@", [style_short stringFromDate : date]);
  35. NSLog(@"cn medium date : %@", [style_medium stringFromDate : date]);
  36. NSLog(@"cn long date : %@", [style_long stringFromDate : date]);
  37. NSLog(@"cn full date : %@", [style_full stringFromDate : date]);
  38.  
  39. //将四个 NSDateFormatter 设置成美国格式
  40. [style_short setLocale : en];
  41. [style_medium setLocale : en];
  42. [style_long setLocale : en];
  43. [style_full setLocale : en];
  44.  
  45. //打印美国格式的日期字符串
  46. NSLog(@"en short date : %@", [style_short stringFromDate : date]);
  47. NSLog(@"en medium date : %@", [style_medium stringFromDate : date]);
  48. NSLog(@"en long date : %@", [style_long stringFromDate : date]);
  49. NSLog(@"en full date : %@", [style_full stringFromDate : date]);
  50. }
  51. }

-- 执行结果 :

  1. localhost:oc_object octopus$ clang -fobjc-arc -framework Foundation OCNSDateFormatterTest.m
  2. localhost:oc_object octopus$ ./a.out
  3. 2015-10-07 18:54:08.207 a.out[825:507] cn short date : 15-10-7
  4. 2015-10-07 18:54:08.209 a.out[825:507] cn medium date : 2015107
  5. 2015-10-07 18:54:08.209 a.out[825:507] cn long date : 2015107
  6. 2015-10-07 18:54:08.210 a.out[825:507] cn full date : 2015107 星期三
  7. 2015-10-07 18:54:08.211 a.out[825:507] en short date : 10/7/15
  8. 2015-10-07 18:54:08.212 a.out[825:507] en medium date : Oct 7, 2015
  9. 2015-10-07 18:54:08.212 a.out[825:507] en long date : October 7, 2015
  10. 2015-10-07 18:54:08.213 a.out[825:507] en full date : Wednesday, October 7, 2015

3. 日历 (NSCalendar) 日期 (NSDateComponents) 组件

(1) 两个类简介

NSCalendar 与 NSDateComponents 简介 :

-- 主要作用 : 月, 日, 年 等数值转化为 NSDate, 从 NSDate 对象中提取 月, 日, 年 数值;

-- NSCalendar 作用 : NSDate 与 NSDateComponents 转化媒介;

-- NSDateComponents 作用 : 专门封装 年月日时分秒 个字段信息, 包含了 year, month, day, hour, minute, second, week, weekday 等 字段的 getter 和 setter 方法;

(2) NSCalendar 常用方法

NSCalendar 常用方法 :

-- NSDate -> 数据 : "(NSDateComponents *) components : FromDate :", 从 NSDate 中提取 年月日时分秒 各个字段数据;

-- 数据 -> NSDate : "dateFromComponents : (NSDateComponents *) comps", 使用 年月日时分秒 数据创建 NSDate;

(3) 获取 NSDate 中数据

NSDate 获取 日期数值数据 :

-- 1.创建 NSCalendar 对象 :

-- 2.获取 NSDateComponents 对象 : 调用 NSCalendar 的 "components : fromDate :" 方法获取 NSDateComponents 对象;

-- 3.获取具体数值 : 调用 NSDateComponents 对象的 getter 方法获取各字段具体数值;

(4) 获取 NSDate 中的数值数据

根据 日志数值数据 创建 NSDate :

-- 1.创建 NSCalendar 对象 :

-- 2.创建 NSDateComponents 对象 : 调用对象的 setter 方法, 将具体的数值设置到字段中去;

-- 3.创建 NSDate 对象 : 调用 NSCalendar 的 "dateFromComponents : (NSDateComponents *)" 初始化 NSDate 对象;

(5) NSCalendar 和 NSDateComponents 示例代码

示例代码 :

  1. //
  2. // main.m
  3. // 01.Calendar
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. //创建 Calendar 对象
  17. NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier : NSGregorianCalendar];
  18. //获取当前日期
  19. NSDate * date = [NSDate date];
  20. //指定 NSDateComponents 中含有的字段信息
  21. unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
  22. NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit;
  23. //获取 NSDateComponents 对象
  24. NSDateComponents * components = [calendar components:unitFlags fromDate:date];
  25. //将 NSDateComponents 对象中得字段打印出来
  26. NSLog(@"%ld 年 %ld 月 %ld 日, %ld 时, %ld 分, %ld 秒, 星期 %ld", components.year, components.month, components.day, components.hour, components.minute, components.second, components.weekday);
  27.  
  28. //根据 NSDateComponents 创建 NSDate 对象
  29. components.year = 3000;
  30. NSDate * date1 = [calendar dateFromComponents:components];
  31. NSLog(@"date1 : %@", date1);
  32. }
  33.  
  34. }

-- 执行结果 :

  1. 2015-10-08 09:56:28.695 01.Calendar[1255:303] 2015 10 8 日, 9 时, 56 分, 28 秒, 星期 9223372036854775807
  2. 2015-10-08 09:56:28.701 01.Calendar[1255:303] date1 : 3000-10-08 01:56:28 +0000
  3. Program ended with exit code: 0

4. 定时器

(1) 定时器创建

创建定时器 :

-- 两个创建方法 : 调用 NSTimer 的 "scheduledTimeWithTimeInterval : repeats : " 方法, 或者 scheduledTimerWithTimeInterval : target : selector : userInfo : repeats : " 方法;

-- timeInterval 参数 : 指定执行周期, 每隔多少时间执行一次;

-- target 与 selector 参数 : 指定重复执行任务, 如果指定 target 或者 selector 参数, 则指定使用 target 的 selector 方法为执行的任务;

-- Invocation 参数 : 传入一个 NSInvocation 对象, 该 NSInvocation 对象也是封装了一个 target 和 selector;

-- userInfo 参数 : 传入额外的附加信息;

-- repeats 参数 : 指定一个 BOOL 值, 指定是否需要循环执行任务;

(2) 定时器流程

定时器使用流程 :

-- 创建定时器 :

  1. [NSTimer scheduledTimerWithTimeInterval:0.5
  2. target:self
  3. selector:NSSelectorFromString(@"info:")
  4. userInfo:nil
  5. repeats: YES];

-- 编写任务方法 :

-- 销毁定时器 :

  1. [timer invalidate];

三. 对象拷贝

1. 拷贝方法 copy 与 mutableCopy

(1) 方法简介

拷贝方法简介  :

-- copy 方法 : 复制对象的副本, 一般返回对象不可修改的副本; 假如被复制的对象是可修改的 NSMutableString, 复制后成为 NSString 不可修改;

-- mutableCopy 方法 : 复制对象的可变副本, 返回对象的可变副本; 假如被复制对象不可修改 如 NSString, 使用该方法复制后为 NSMutableString 可以修改;

(2) 示例代码

示例代码 :

  1. //
  2. // main.m
  3. // 03.Copy
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSString * str = @"octopus";
  17. NSMutableString * str_mutable = [NSMutableString stringWithString:@"octopus"];
  18.  
  19. //复制可变字符串
  20. NSMutableString * str_mutable1 = [str mutableCopy];
  21. NSMutableString * str_mutable2 = [str_mutable mutableCopy];
  22.  
  23. //验证可变字符串
  24. [str_mutable1 appendString:@" mutable"];
  25. [str_mutable2 appendString:@" mutable"];
  26. NSLog(@"str_mutable1 : %@, str_mutable2 : %@", str_mutable1, str_mutable2);
  27.  
  28. //复制不可变字符串
  29. NSString * str1 = [str copy];
  30. NSMutableString * str2 = [str_mutable copy];
  31. [str2 appendString:@" mutable"];
  32. NSLog(@"str_mutable2 : %@", str_mutable2);
  33. }
  34. return 0;
  35. }

-- 执行结果 : 后面的复制 不能被修改, 所以报错;

  1. 2015-10-08 20:14:57.690 03.Copy[760:303] str_mutable1 : octopus mutable, str_mutable2 : octopus mutable
  2. 2015-10-08 20:14:57.692 03.Copy[760:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'
  3. *** First throw call stack:
  4. (
  5. 0 CoreFoundation 0x00007fff8fda225c __exceptionPreprocess + 172
  6. 1 libobjc.A.dylib 0x00007fff8e6a0e75 objc_exception_throw + 43
  7. 2 CoreFoundation 0x00007fff8fda210c +[NSException raise:format:] + 204
  8. 3 CoreFoundation 0x00007fff8fd71dbe mutateError + 110
  9. 4 03.Copy 0x0000000100000dfc main + 348
  10. 5 libdyld.dylib 0x00007fff84bb85fd start + 1
  11. )
  12. libc++abi.dylib: terminating with uncaught exception of type NSException
  13. (lldb)

2. 自定义 拷贝方法

(1) 自定义类拷贝方法

调用 copy 方法前提 :

-- 1.该类实现了 NSCopying 协议;

-- 2.该类实现了 copyWithZone: 方法;

调用 mutableCopy 方法前提 :

-- 1.该类实现了 NSMutableCopying 协议;

-- 2.该类实现了 mutableCopyWithZone 方法;

(2) 代码示例

自定义类赋值代码示例 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, strong) NSString * name;
  13. @property (assign, nonatomic) int age;
  14. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) copyWithZone : (NSZone *) zone
  16. {
  17. OCCat * cat = [[[self class] allocWithZone:zone]init];
  18. cat.name = self.name;
  19. cat.age = self.age;
  20. return cat;
  21. }
  22.  
  23. @end

-- main.m :

  1. //
  2. // main.m
  3. // 03.Copy
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16. OCCat * cat1 = [[OCCat alloc] init];
  17. cat1.name = [NSMutableString stringWithString:@"Cat One"];
  18. cat1.age = 18;
  19.  
  20. OCCat * cat2 = [cat1 copy];
  21. cat2.name = [NSMutableString stringWithString:@"Cat Two"];
  22. cat2.age = 20;
  23.  
  24. NSLog(@"cat1.name : %@, cat1.age : %d, cat2.name : %@, cat2.age : %d", cat1.name, cat1.age, cat2.name, cat2.age);
  25. }
  26. return 0;
  27. }

-- 执行结果 :

  1. 2015-10-08 20:41:08.584 03.Copy[941:303] cat1.name : Cat One, cat1.age : 18, cat2.name : Cat Two, cat2.age : 20
  2. Program ended with exit code: 0

3. 浅复制 深复制

(1) 浅拷贝示例

浅拷贝示例 : 复制 cat2 对象是从 cat1 复制而来, 但是 cat1 中得 name 是一个 NSString * 引用对象, 这两个 cat1 cat2 都指向同一个 name 对象;

-- 修改下之前的 main 函数 :

  1. //
  2. // main.m
  3. // 03.Copy
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16. OCCat * cat1 = [[OCCat alloc] init];
  17. cat1.name = [NSMutableString stringWithString:@"Cat One"];
  18. cat1.age = 18;
  19.  
  20. OCCat * cat2 = [cat1 copy];
  21. [(NSMutableString *)cat2.name replaceCharactersInRange:NSMakeRange(0, 7) withString:@"Cat Two"];
  22. cat2.age = 20;
  23.  
  24. NSLog(@"cat1.name : %@, cat1.age : %d, cat2.name : %@, cat2.age : %d", cat1.name, cat1.age, cat2.name, cat2.age);
  25. }
  26. return 0;
  27. }

-- 执行结果 : 

  1. 2015-10-09 17:13:56.329 03.Copy[1744:303] cat1.name : Cat Two, cat1.age : 18, cat2.name : Cat Two, cat2.age : 20
  2. Program ended with exit code: 0

(2) 深拷贝示例

深拷贝完整示例 :

-- OCCat.h : 与之前的相同;

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, strong) NSString * name;
  13. @property (assign, nonatomic) int age;
  14. @end

-- OCCat.m : 只是在 copyWithZone 方法中, 将 name 赋值 改为 name copy 后赋值;

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) copyWithZone : (NSZone *) zone
  16. {
  17. OCCat * cat = [[[self class] allocWithZone:zone]init];
  18. cat.name = [self.name mutableCopy];
  19. cat.age = self.age;
  20. return cat;
  21. }
  22.  
  23. @end

-- main.m : 与之前相同;

  1. //
  2. // main.m
  3. // 03.Copy
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16. OCCat * cat1 = [[OCCat alloc] init];
  17. cat1.name = [NSMutableString stringWithString:@"Cat One"];
  18. cat1.age = 18;
  19.  
  20. OCCat * cat2 = [cat1 copy];
  21. [(NSMutableString *)cat2.name replaceCharactersInRange:NSMakeRange(0, 7) withString:@"Cat Two"];
  22. cat2.age = 20;
  23.  
  24. NSLog(@"cat1.name : %@, cat1.age : %d, cat2.name : %@, cat2.age : %d", cat1.name, cat1.age, cat2.name, cat2.age);
  25. }
  26. return 0;
  27. }

-- 执行结果 :

  1. 2015-10-09 17:27:22.302 03.Copy[1809:303] cat1.name : Cat One, cat1.age : 18, cat2.name : Cat Two, cat2.age : 20
  2. Program ended with exit code: 0

4. setter 的 copy 参数

(1) copy 参数

copy 参数用处 :

-- 示例 :

  1. @property (nonatomic, copy) NSMutableString * name;

-- 作用 : 为 name 属性 赋值时, 将赋值对象 copy 一份, 将副本赋值给 name 属性;

-- 使用 copy 指令, 相当于将 setter 方法设置成如下状态 :

  1. - (void) setName:(NSMutableString *)name1
  2. {
  3. this.name = [name1 copy];
  4. }

(2) copy 参数示例

copy 代码错误使用示例 : copy 的属性, 赋值后 是不可变的, 如 NSMutableString 接收一个 NSString 赋值, 如果在对赋值后的属性执行 NSMutableString 的操作, 就会报错;

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSMutableString * name;
  13. @property (assign, nonatomic) int age;
  14. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. @end

-- main.h :

  1. //
  2. // main.m
  3. // 03.Copy
  4. //
  5. // Created by octopus on 15-10-8.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16. OCCat * cat1 = [[OCCat alloc] init];
  17. cat1.name = [NSMutableString stringWithString:@"Cat One"];
  18. cat1.age = 18;
  19.  
  20. [cat1.name appendString:@" append"];
  21. }
  22. return 0;
  23. }

-- 执行结果 : 

  1. 2015-10-09 23:17:01.469 03.Copy[2095:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'
  2. *** First throw call stack:
  3. (
  4. 0 CoreFoundation 0x00007fff8fda225c __exceptionPreprocess + 172
  5. 1 libobjc.A.dylib 0x00007fff8e6a0e75 objc_exception_throw + 43
  6. 2 CoreFoundation 0x00007fff8fda210c +[NSException raise:format:] + 204
  7. 3 CoreFoundation 0x00007fff8fd71dbe mutateError + 110
  8. 4 03.Copy 0x0000000100000d86 main + 246
  9. 5 libdyld.dylib 0x00007fff84bb85fd start + 1
  10. )
  11. libc++abi.dylib: terminating with uncaught exception of type NSException
  12. (lldb)

四. NSArray NSMutableArray 数组集合

Objective-C 集合概述 : 

-- NSArray : 有序, 可重复集合;

-- NSSet : 无序, 不可重复集合;

-- NSDictionary : 有映射关系的集合;

1. NSArray 基本功能用法

(1) NSArray 创建

NSArray 简介 : 有序, 可重复集合;

-- 特点 : 每个元素都有对应的顺序索引, 允许重复元素, 通过索引来访问指定位置元素, 索引从 0 开始;

NSArray 对象创建 :

-- "array" 方法 : 创建空得 NSArray 对象;

-- "arrayWithContentsOfFile : " 方法 : 读取文件内容创建 NSArray 对象;

-- "initWithContentsOfFile : " 方法 :  读取文件内容创建 NSArray 对象;

-- "arrayWithObject : " 方法 : 创建只包含单个元素的 NSArray 对象;

-- "initWithObject : " 方法 : 创建只包含单个元素的 NSArray 对象;

-- "arrayWithObjects : " 方法 : 创建包含 N 个元素的 NSArray 对象, 最后一个 nil 元素, 表示 NSArray 集合结束;

  1. NSArray * array = [NSArray arrayWithObjects:@"Smoke", @"Drink", @"Perm", nil];

-- "initWithObjects : " 方法 : 创建包含 N 个元素的 NSArray 对象;

(2) NSArray 基本方法

NSArray 集合主要方法 :

-- 查索引 : 查询集合元素在 NSArray 中索引;

  1. //获取元素在集合中得位置
  2. NSLog(@"Index Of Drink at subArray is : %ld", [subArray indexOfObject:@"Drink"]);
  3. NSLog(@"Index Of Drink at array is : %ld", [array indexOfObject:@"Drink" inRange:NSMakeRange(1, 2)]);

-- 查元素 : 根据索引值 取出 NSArray 集合中得元素;

  1. // 获取 第 0 1 最后一个元素 并打印
  2. NSLog(@"First : %@", [array objectAtIndex:0]);
  3. NSLog(@"Second : %@", [array objectAtIndex:1]);
  4. NSLog(@"Last : %@", [array lastObject]);

-- 整体调用 : 对集合元素整体调用的方法;

-- 排序 : 对 NSArray 集合进行排序;

-- 截取元素 : 取出 NSArray 部分元素组成新集合;

  1. //截取 1 2 个元素组成新 NSArray 集合对象
  2. NSArray * subArray = [array objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)]];

-- 追加元单个素 : "arrayByAddingObject : " 追加单个元素;

  1. array = [array arrayByAddingObject:@"Run"];

-- 追加数组 : "arrayWithObjects : " 追加一个数组;

  1. array = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"Jump", @"Fuck", nil]];

(3) NSArray 基本方法 示例代码

示例代码 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14. - (id) initWithName : (NSString *) _name age : (int) _age;
  15. - (void) purr : (NSString *) content;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14. - (id) initWithName:(NSString *)_name age:(int)_age
  15. {
  16. self = [super init];
  17. self.name = _name;
  18. self.age = _age;
  19. return self;
  20. }
  21.  
  22. - (void) purr:(NSString *)content
  23. {
  24. NSLog(@"cat name : %@, age %d is purr", self.name, self.age);
  25. }
  26.  
  27. - (BOOL) isEqual : (id) other
  28. {
  29. //如果地址相同, 那么比较结果肯定相同
  30. if(self == other)
  31. {
  32. return YES;
  33. }
  34.  
  35. //对象对比之前首先保证类型相同
  36. if([other class] == OCCat.class)
  37. {
  38. OCCat * cat = (OCCat *)other;
  39. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  40. }
  41. return NO;
  42. }
  43.  
  44. //%@ 打印的时候输出的内容
  45. - (NSString *) description
  46. {
  47. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  48. }
  49.  
  50. @end

-- main.m :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. //创建 NSArray 对象
  17. NSArray * array = [NSArray arrayWithObjects:@"Smoke", @"Drink", @"Perm", nil];
  18.  
  19. // 获取 第 0 1 最后一个元素 并打印
  20. NSLog(@"First : %@", [array objectAtIndex:0]);
  21. NSLog(@"Second : %@", [array objectAtIndex:1]);
  22. NSLog(@"Third: %@", array[2]);
  23. NSLog(@"Last : %@", [array lastObject]);
  24.  
  25. // 打印整个 array
  26. NSLog(@"array : \n%@", array);
  27.  
  28. //截取 1 2 个元素组成新 NSArray 集合对象, NSIndexSet 集合用于保存索引值
  29. NSArray * subArray = [array objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)]];
  30.  
  31. //获取元素在集合中得位置
  32. NSLog(@"Index Of Drink at subArray is : %ld", [subArray indexOfObject:@"Drink"]);
  33. NSLog(@"Index Of Drink at array is : %ld", [array indexOfObject:@"Drink" inRange:NSMakeRange(1, 2)]);
  34.  
  35. //添加单个元素
  36. array = [array arrayByAddingObject:@"Run"];
  37. //追加一个 NSArray 集合, nil 结尾表示 NSArray 结束
  38. array = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"Jump", @"Fuck", nil]];
  39. //遍历 array
  40. for (int i = 0; i < array.count; i ++) {
  41. NSLog(@"array %d : %@", i, [array objectAtIndex:i]);
  42. }
  43.  
  44. //将字符串写出到文件中
  45. [array writeToFile:@"array.txt" atomically:YES];
  46.  
  47. //创建 OCCat 的 array
  48. NSArray * array2 = [NSArray arrayWithObjects:
  49. [[OCCat alloc] initWithName : @"Tom" age : 18],
  50. [[OCCat alloc] initWithName : @"Jerry" age : 20],
  51. nil];
  52. //打印 array, 验证 重写的 description 方法
  53. NSLog(@"array2 is : \n%@", array2);
  54. OCCat* cat = [[OCCat alloc] initWithName : @"Tom" age : 18];
  55. //获取 元素 在 array 中的位置, 验证 isEqual 方法
  56. NSUInteger index = [array2 indexOfObject:cat];
  57. NSLog(@"index Of Tom is : %ld", index);
  58.  
  59. }
  60. return 0;
  61. }

-- 执行结果 :

  1. 2015-10-14 17:16:12.748 04.NSArray[7155:303] First : Smoke
  2. 2015-10-14 17:16:12.750 04.NSArray[7155:303] Second : Drink
  3. 2015-10-14 17:16:12.751 04.NSArray[7155:303] Third: Perm
  4. 2015-10-14 17:16:12.751 04.NSArray[7155:303] Last : Perm
  5. 2015-10-14 17:16:12.751 04.NSArray[7155:303] array :
  6. (
  7. Smoke,
  8. Drink,
  9. Perm
  10. )
  11. 2015-10-14 17:16:12.752 04.NSArray[7155:303] Index Of Drink at subArray is : 0
  12. 2015-10-14 17:16:12.752 04.NSArray[7155:303] Index Of Drink at array is : 1
  13. 2015-10-14 17:16:12.753 04.NSArray[7155:303] array 0 : Smoke
  14. 2015-10-14 17:16:12.753 04.NSArray[7155:303] array 1 : Drink
  15. 2015-10-14 17:16:12.753 04.NSArray[7155:303] array 2 : Perm
  16. 2015-10-14 17:16:12.754 04.NSArray[7155:303] array 3 : Run
  17. 2015-10-14 17:16:12.754 04.NSArray[7155:303] array 4 : Jump
  18. 2015-10-14 17:16:12.755 04.NSArray[7155:303] array 5 : Fuck
  19. 2015-10-14 17:16:12.769 04.NSArray[7155:303] array2 is :
  20. (
  21. "<OCCat [name = Tom, age = 18]>",
  22. "<OCCat [name = Jerry, age = 20]>"
  23. )
  24. 2015-10-14 17:16:12.769 04.NSArray[7155:303] index Of Tom is : 0
  25. Program ended with exit code: 0

2. NSArray 集合整体调用

(1) NSArray 整体元素调用简介

调用所有元素方法 :

-- "makeObjectPerformSelector : " 方法 : 按顺序 依次 调用 NSArray 中集合的每个元素, 需要传入 SEL 参数, 该参数指定需要调用的方法;

-- "makeObjectPerformSelector : withObject : " 方法 : 参数一 SEL 代表的方法, 参数二 方法的参数, 参数三 控制是否终止迭代;

-- "enumerateObjectsUsingBlock : " 方法 : 遍历集合中元素, 使用 集合中的元素 执行指定代码块;

-- "enumerateObjectsWithOptions : usingBlock : " 方法 : 遍历元素, 依次使用元素执行代码块, 可以额外传入一个参数控制遍历选项;

-- "enumerateObjectsAtIndexs : options : usingBlock : " 方法 : 遍历指定范围元素, 依次使用元素执行代码块, 传入最后的参数用于控制遍历;

(2) NSArray 整体元素调用代码示例

代码示例 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14. - (id) initWithName : (NSString *) _name age : (int) _age;
  15. - (void) purr : (NSString *) content;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14. - (id) initWithName:(NSString *)_name age:(int)_age
  15. {
  16. self = [super init];
  17. self.name = _name;
  18. self.age = _age;
  19. return self;
  20. }
  21.  
  22. - (void) purr:(NSString *)content
  23. {
  24. NSLog(@"cat name : %@, age %d is purr", self.name, self.age);
  25. }
  26.  
  27. - (BOOL) isEqual : (id) other
  28. {
  29. //如果地址相同, 那么比较结果肯定相同
  30. if(self == other)
  31. {
  32. return YES;
  33. }
  34.  
  35. //对象对比之前首先保证类型相同
  36. if([other class] == OCCat.class)
  37. {
  38. OCCat * cat = (OCCat *)other;
  39. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  40. }
  41. return NO;
  42. }
  43.  
  44. //%@ 打印的时候输出的内容
  45. - (NSString *) description
  46. {
  47. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  48. }
  49.  
  50. @end

-- main.m :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. //创建 OCCat 的 array
  17. NSArray * array = [NSArray arrayWithObjects:
  18. [[OCCat alloc] initWithName : @"Tom" age : 18],
  19. [[OCCat alloc] initWithName : @"Jerry" age : 20],
  20. nil];
  21.  
  22. NSLog(@"array is : \n%@", array);
  23.  
  24. [array makeObjectsPerformSelector:@selector(purr:) withObject:@" FUCK YOU "];
  25.  
  26. }
  27. return 0;
  28. }

-- 执行结果 :

  1. 2015-10-15 10:50:49.902 04.NSArray[8086:303] array is :
  2. (
  3. "<OCCat [name = Tom, age = 18]>",
  4. "<OCCat [name = Jerry, age = 20]>"
  5. )
  6. 2015-10-15 10:50:49.905 04.NSArray[8086:303] cat name : Tom, age 18 is purr
  7. 2015-10-15 10:50:49.905 04.NSArray[8086:303] cat name : Jerry, age 20 is purr
  8. Program ended with exit code: 0

3. NSArray 排序

(1) NSArray 排序简介

NSArray 排序 :

-- "sortArrayUsingFunction : context : " 方法 : 使用排序函数对集合进行排序, 函数会返回 NSOrderedDescending, NSOrderedAscending, NSOrderedSame 枚举值, 同时会返回一个 排序号的 NSArray 集合;

-- "sortArrayUsingSelector : " 方法 : 使用集合元素自身的方法对集合进行排序, 该方法返回 NSOrderedDescending, NSOrderedAscending, NSOrderedSame 枚举值, 同时还会返回一个 排序号的 NSArray 集合;

-- "sortArrayUsingComparator : " 方法 : 使用代码块对集合进行排序, 代码块 返回 NSOrderedDescending, NSOrderedAscending, NSOrderedSame 枚举值, 同时返回排序号的 NSArray 集合;

(2) NSArray 排序代码示例

NSArray 排序示例代码 :

-- main.c :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. NSInteger intSort(id num1, id num2, void * context)
  12. {
  13. int v1 = [num1 intValue];
  14. int v2 = [num2 intValue];
  15.  
  16. if(v1 < v2)
  17. {
  18. return NSOrderedAscending;
  19. }
  20.  
  21. if(v1 > v2)
  22. {
  23. return NSOrderedDescending;
  24. }
  25.  
  26. return NSOrderedSame;
  27. }
  28.  
  29. int main(int argc, const char * argv[])
  30. {
  31.  
  32. @autoreleasepool {
  33.  
  34. //创建一个 NSarray 集合, 其中放置 NSString 元素
  35. NSArray * array = [NSArray arrayWithObjects:@"Tom", @"Jerry", @"Fuck", nil];
  36. NSLog(@"array : \n%@\n", array);
  37.  
  38. //使用 compare 方法进行排序
  39. array = [array sortedArrayUsingSelector:@selector(compare:)];
  40. NSLog(@"array sort : \n%@\n", array);
  41.  
  42. //创建一个 NSNumber 元素的 NSarray 集合
  43. NSArray * array_num = [NSArray arrayWithObjects:
  44. [NSNumber numberWithInt:18],
  45. [NSNumber numberWithInt:16],
  46. [NSNumber numberWithInt:20],
  47. [NSNumber numberWithInt:-1],
  48. nil];
  49. NSLog(@"array_num : \n%@\n", array_num);
  50.  
  51. //使用 intSort 方法进行排序
  52. array_num = [array_num sortedArrayUsingFunction:intSort context:nil];
  53. NSLog(@"array_num sort : \n%@\n", array_num);
  54.  
  55. //使用代码块进行排序
  56. array_num = [array_num sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
  57. {
  58. if([obj2 intValue] < [obj1 intValue])
  59. {
  60. return NSOrderedAscending;
  61. }
  62.  
  63. if([obj2 intValue] > [obj1 intValue])
  64. {
  65. return NSOrderedDescending;
  66. }
  67.  
  68. return NSOrderedSame;
  69. }];
  70. NSLog(@"array_num : \n%@\n", array_num);
  71.  
  72. }
  73. return 0;
  74. }

-- 执行结果 :

  1. 2015-10-15 11:52:51.335 04.NSArray[8465:303] array :
  2. (
  3. Tom,
  4. Jerry,
  5. Fuck
  6. )
  7. 2015-10-15 11:52:51.336 04.NSArray[8465:303] array sort :
  8. (
  9. Fuck,
  10. Jerry,
  11. Tom
  12. )
  13. 2015-10-15 11:52:51.337 04.NSArray[8465:303] array_num :
  14. (
  15. 18,
  16. 16,
  17. 20,
  18. "-1"
  19. )
  20. 2015-10-15 11:52:51.337 04.NSArray[8465:303] array_num sort :
  21. (
  22. "-1",
  23. 16,
  24. 18,
  25. 20
  26. )
  27. 2015-10-15 11:52:51.338 04.NSArray[8465:303] array_num :
  28. (
  29. 20,
  30. 18,
  31. 16,
  32. "-1"
  33. )
  34. Program ended with exit code: 0

4. NSArray 枚举器遍历

(1) NSArray 获取枚举器

NSArray 枚举器 NSEnumerator 简介 :

-- "objectEnumerator : " 方法 : 返回 NSArray 集合顺序枚举器;

-- "reverseObjectEnumerator : "  方法 : 返回 NSArray 集合的逆序枚举器;

(2) NSEnumerator 枚举器

NSEnumerator 方法简介 :

-- "allObjects : " 方法 : 获取集合中的所有元素;

-- "nextObjects : " 方法 : 获取集合中的下一个元素;

(3) NSEnumerator 枚举器示例代码

NSEnumerator 示例代码 :

-- main.m 代码 :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. //创建一个 NSarray 集合, 其中放置 NSString 元素
  17. NSArray * array = [NSArray arrayWithObjects:@"Tom", @"Jerry", @"Fuck", nil];
  18. NSLog(@"array : \n%@\n", array);
  19.  
  20. //顺序枚举
  21. NSEnumerator * enumerator = [array objectEnumerator];
  22. id object;
  23. while (object = [enumerator nextObject]) {
  24. NSLog(@"%@", object);
  25. }
  26.  
  27. NSLog(@"\n");
  28.  
  29. //逆序枚举
  30. NSEnumerator * reverseEnumerator = [array reverseObjectEnumerator];
  31. while (object = [reverseEnumerator nextObject]) {
  32. NSLog(@"%@", object);
  33. }
  34.  
  35. }
  36. return 0;
  37. }

-- 执行结果 :

  1. 2015-10-15 12:09:59.170 04.NSArray[8590:303] array :
  2. (
  3. Tom,
  4. Jerry,
  5. Fuck
  6. )
  7. 2015-10-15 12:09:59.176 04.NSArray[8590:303] Tom
  8. 2015-10-15 12:09:59.177 04.NSArray[8590:303] Jerry
  9. 2015-10-15 12:09:59.177 04.NSArray[8590:303] Fuck
  10. 2015-10-15 12:09:59.178 04.NSArray[8590:303]
  11. 2015-10-15 12:09:59.178 04.NSArray[8590:303] Fuck
  12. 2015-10-15 12:09:59.178 04.NSArray[8590:303] Jerry
  13. 2015-10-15 12:09:59.179 04.NSArray[8590:303] Tom
  14. Program ended with exit code: 0

5. NSArray 快速枚举

(1) NSArray 快速枚举

快速枚举简介 : 快速枚举无需获取集合长度, 也不用根据索引访问集合元素;

-- 语法 :

  1. for (id object in array)
  2. {
  3. //对 object 进行操作
  4. }

(2) NSArray 快速枚举示例

NSArray 快速枚举示例 :

-- main.m :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. //创建一个 NSarray 集合, 其中放置 NSString 元素
  17. NSArray * array = [NSArray arrayWithObjects:@"Tom", @"Jerry", @"Fuck", nil];
  18. NSLog(@"array : \n%@\n", array);
  19.  
  20. //快速枚举
  21. for(id object in array)
  22. {
  23. NSLog(@"%@", object);
  24. }
  25.  
  26. }
  27. return 0;
  28. }

-- 执行结果 :

  1. 2015-10-15 12:17:36.096 04.NSArray[8642:303] array :
  2. (
  3. Tom,
  4. Jerry,
  5. Fuck
  6. )
  7. 2015-10-15 12:17:36.098 04.NSArray[8642:303] Tom
  8. 2015-10-15 12:17:36.098 04.NSArray[8642:303] Jerry
  9. 2015-10-15 12:17:36.099 04.NSArray[8642:303] Fuck
  10. Program ended with exit code: 0

6. NSMutableArray 可变数组

(1) NSMutableArray 简介

NSMutableArray 简介 :

-- NSMutableArray 与 NSArray 对比 : NSArray 是不可变数组, 一旦创建不能增删替换元素, NSMutableArray 可以随时增加 删除 替换 元素;

-- "addXX" 方法 : 添加集合元素的方法;

-- "removeXX" 方法 : 删除集合元素的方法;

-- "replaceXX" 方法 : 替换集合元素的方法;

-- "sortXX" 方法 : 对集合本身排序的方法;

(2) NSMutableArray 代码示例

NSMutableArray 代码示例 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14. - (id) initWithName : (NSString *) _name age : (int) _age;
  15. - (void) purr : (NSString *) content;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14. - (id) initWithName:(NSString *)_name age:(int)_age
  15. {
  16. self = [super init];
  17. self.name = _name;
  18. self.age = _age;
  19. return self;
  20. }
  21.  
  22. - (void) purr:(NSString *)content
  23. {
  24. NSLog(@"cat name : %@, age %d is purr", self.name, self.age);
  25. }
  26.  
  27. - (BOOL) isEqual : (id) other
  28. {
  29. //如果地址相同, 那么比较结果肯定相同
  30. if(self == other)
  31. {
  32. return YES;
  33. }
  34.  
  35. //对象对比之前首先保证类型相同
  36. if([other class] == OCCat.class)
  37. {
  38. OCCat * cat = (OCCat *)other;
  39. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  40. }
  41. return NO;
  42. }
  43.  
  44. //%@ 打印的时候输出的内容
  45. - (NSString *) description
  46. {
  47. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  48. }
  49.  
  50. @end

-- main.m :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSMutableArray * array = [NSMutableArray arrayWithObjects:
  17. [[OCCat alloc] initWithName : @"Tom" age : 18],
  18. [[OCCat alloc] initWithName : @"Jerry" age: 20],
  19. [[OCCat alloc] initWithName : @"Jay" age : 180],
  20. nil];
  21. NSLog(@"array 原始数组 : \n%@\n", array);
  22.  
  23. //添加单个元素
  24. [array addObject:[[OCCat alloc] initWithName:@"octopus" age:26]];
  25. NSLog(@"array 追加单个元素 : \n%@\n", array);
  26.  
  27. //添加数组
  28. [array addObjectsFromArray:[NSArray arrayWithObjects:
  29. [[OCCat alloc] initWithName:@"HanShuliang" age:26],
  30. [[OCCat alloc] initWithName:@"Bill" age:60],
  31. nil]];
  32. NSLog(@"array 追加集合 : \n%@\n", array);
  33.  
  34. //插入单个元素
  35. [array insertObject : [[OCCat alloc] initWithName:@"XiJinping" age:26] atIndex:0];
  36. NSLog(@"array 插入单个元素 : \n%@\n", array);
  37.  
  38. //插入集合
  39. [array insertObjects: [NSArray arrayWithObjects:
  40. [[OCCat alloc] initWithName:@"LiKeqiang" age:26],
  41. [[OCCat alloc] initWithName:@"Son" age:60],
  42. nil]
  43. atIndexes: [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 2)]];
  44. NSLog(@"array 插入集合 : \n%@\n", array);
  45.  
  46. //删除最后一个元素
  47. [array removeLastObject];
  48. NSLog(@"array 删除最后一个元素 : \n%@\n", array);
  49.  
  50. //删除第3个元素
  51. [array removeObjectAtIndex:3];
  52. NSLog(@"array 删除第三个元素 : \n%@\n", array);
  53.  
  54. //删除第3,4个元素
  55. [array removeObjectsInRange:NSMakeRange(3, 2)];
  56. NSLog(@"array 删除第3,4个元素 : \n%@\n", array);
  57.  
  58. //替换第0个元素
  59. [array replaceObjectAtIndex:0 withObject:[[OCCat alloc] initWithName:@"HanShuliang" age:28]];
  60. NSLog(@"array 替换第0个元素 : \n%@\n", array);
  61.  
  62. }
  63. return 0;
  64. }

-- 执行结果 :

  1. 2015-10-15 12:51:43.749 04.NSArray[9033:303] array 原始数组 :
  2. (
  3. "<OCCat [name = Tom, age = 18]>",
  4. "<OCCat [name = Jerry, age = 20]>",
  5. "<OCCat [name = Jay, age = 180]>"
  6. )
  7. 2015-10-15 12:51:43.752 04.NSArray[9033:303] array 追加单个元素 :
  8. (
  9. "<OCCat [name = Tom, age = 18]>",
  10. "<OCCat [name = Jerry, age = 20]>",
  11. "<OCCat [name = Jay, age = 180]>",
  12. "<OCCat [name = octopus, age = 26]>"
  13. )
  14. 2015-10-15 12:51:43.752 04.NSArray[9033:303] array 追加集合 :
  15. (
  16. "<OCCat [name = Tom, age = 18]>",
  17. "<OCCat [name = Jerry, age = 20]>",
  18. "<OCCat [name = Jay, age = 180]>",
  19. "<OCCat [name = octopus, age = 26]>",
  20. "<OCCat [name = HanShuliang, age = 26]>",
  21. "<OCCat [name = Bill, age = 60]>"
  22. )
  23. 2015-10-15 12:51:43.753 04.NSArray[9033:303] array 插入单个元素 :
  24. (
  25. "<OCCat [name = XiJinping, age = 26]>",
  26. "<OCCat [name = Tom, age = 18]>",
  27. "<OCCat [name = Jerry, age = 20]>",
  28. "<OCCat [name = Jay, age = 180]>",
  29. "<OCCat [name = octopus, age = 26]>",
  30. "<OCCat [name = HanShuliang, age = 26]>",
  31. "<OCCat [name = Bill, age = 60]>"
  32. )
  33. 2015-10-15 12:51:43.753 04.NSArray[9033:303] array 插入集合 :
  34. (
  35. "<OCCat [name = XiJinping, age = 26]>",
  36. "<OCCat [name = Tom, age = 18]>",
  37. "<OCCat [name = LiKeqiang, age = 26]>",
  38. "<OCCat [name = Son, age = 60]>",
  39. "<OCCat [name = Jerry, age = 20]>",
  40. "<OCCat [name = Jay, age = 180]>",
  41. "<OCCat [name = octopus, age = 26]>",
  42. "<OCCat [name = HanShuliang, age = 26]>",
  43. "<OCCat [name = Bill, age = 60]>"
  44. )
  45. 2015-10-15 12:51:43.755 04.NSArray[9033:303] array 删除最后一个元素 :
  46. (
  47. "<OCCat [name = XiJinping, age = 26]>",
  48. "<OCCat [name = Tom, age = 18]>",
  49. "<OCCat [name = LiKeqiang, age = 26]>",
  50. "<OCCat [name = Son, age = 60]>",
  51. "<OCCat [name = Jerry, age = 20]>",
  52. "<OCCat [name = Jay, age = 180]>",
  53. "<OCCat [name = octopus, age = 26]>",
  54. "<OCCat [name = HanShuliang, age = 26]>"
  55. )
  56. 2015-10-15 12:51:43.756 04.NSArray[9033:303] array 删除第三个元素 :
  57. (
  58. "<OCCat [name = XiJinping, age = 26]>",
  59. "<OCCat [name = Tom, age = 18]>",
  60. "<OCCat [name = LiKeqiang, age = 26]>",
  61. "<OCCat [name = Jerry, age = 20]>",
  62. "<OCCat [name = Jay, age = 180]>",
  63. "<OCCat [name = octopus, age = 26]>",
  64. "<OCCat [name = HanShuliang, age = 26]>"
  65. )
  66. 2015-10-15 12:51:43.757 04.NSArray[9033:303] array 删除第3,4个元素 :
  67. (
  68. "<OCCat [name = XiJinping, age = 26]>",
  69. "<OCCat [name = Tom, age = 18]>",
  70. "<OCCat [name = LiKeqiang, age = 26]>",
  71. "<OCCat [name = octopus, age = 26]>",
  72. "<OCCat [name = HanShuliang, age = 26]>"
  73. )
  74. 2015-10-15 12:51:43.757 04.NSArray[9033:303] array 替换第0个元素 :
  75. (
  76. "<OCCat [name = HanShuliang, age = 28]>",
  77. "<OCCat [name = Tom, age = 18]>",
  78. "<OCCat [name = LiKeqiang, age = 26]>",
  79. "<OCCat [name = octopus, age = 26]>",
  80. "<OCCat [name = HanShuliang, age = 26]>"
  81. )
  82. Program ended with exit code: 0

7. NSArray 的 KVO KVC 用法

(1) NSArray KVC 简介

NSArray KVC 简介 : NSArray 可以对元素进行整体 KVC 编码;

-- "setValue : forKey : " 方法 : 将所有元素的制定 key 变量设置为 某个值;

-- "valueForKey : " 方法 : 返回 所有元素指定变量值组成的 NSArray 集合;

(2) NSArray KVO 简介

NSArray KVO 简介 : NSArray 中可以对所有元素进行 KVO 编程;

-- "addObserver : forKeyPath : options : context : " 方法 : 为集合中所有元素添加 KVO 监听器;

-- "removeObserver : forKeyPath : " 方法 : 为集合中所有元素删除 KVO 监听器;

-- "addObserver : toObjectAtIndexes : forKeyPath : options : context : " 方法 : 为集合中指定元素添加 KVO 监听;

-- "removeObserver : fromObjectsAtIndexes : forKeyPath : " 方法 : 为集合中指定元素删除 KVO 监听;

(3) NSArray KVC KVO 示例代码

NSArray KVC KVO 示例代码 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14. - (id) initWithName : (NSString *) _name age : (int) _age;
  15. - (void) purr : (NSString *) content;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14. - (id) initWithName:(NSString *)_name age:(int)_age
  15. {
  16. self = [super init];
  17. self.name = _name;
  18. self.age = _age;
  19. return self;
  20. }
  21.  
  22. - (void) purr:(NSString *)content
  23. {
  24. NSLog(@"cat name : %@, age %d is purr", self.name, self.age);
  25. }
  26.  
  27. - (BOOL) isEqual : (id) other
  28. {
  29. //如果地址相同, 那么比较结果肯定相同
  30. if(self == other)
  31. {
  32. return YES;
  33. }
  34.  
  35. //对象对比之前首先保证类型相同
  36. if([other class] == OCCat.class)
  37. {
  38. OCCat * cat = (OCCat *)other;
  39. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  40. }
  41. return NO;
  42. }
  43.  
  44. //%@ 打印的时候输出的内容
  45. - (NSString *) description
  46. {
  47. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  48. }
  49.  
  50. @end

-- main.m :

  1. //
  2. // main.m
  3. // 04.NSArray
  4. //
  5. // Created by octopus on 15-10-13.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSMutableArray * array = [NSMutableArray arrayWithObjects:
  17. [[OCCat alloc] initWithName : @"Tom" age : 18],
  18. [[OCCat alloc] initWithName : @"Jerry" age: 20],
  19. [[OCCat alloc] initWithName : @"Jay" age : 180],
  20. nil];
  21. NSLog(@"array 原始数组 : \n%@\n", array);
  22.  
  23. NSArray * str_array = [array valueForKeyPath:@"name"];
  24. NSLog(@"str_array : \n%@\n", str_array);
  25.  
  26. [array setValue:@"octopus" forKeyPath:@"name"];
  27. NSLog(@"array 设置 KVC 后的数组 : \n%@\n", array);
  28.  
  29. }
  30. return 0;
  31. }

-- 执行结果 :

  1. 2015-10-15 13:07:04.116 04.NSArray[9135:303] array 原始数组 :
  2. (
  3. "<OCCat [name = Tom, age = 18]>",
  4. "<OCCat [name = Jerry, age = 20]>",
  5. "<OCCat [name = Jay, age = 180]>"
  6. )
  7. 2015-10-15 13:07:04.118 04.NSArray[9135:303] str_array :
  8. (
  9. Tom,
  10. Jerry,
  11. Jay
  12. )
  13. 2015-10-15 13:07:04.118 04.NSArray[9135:303] array 设置 KVC 后的数组 :
  14. (
  15. "<OCCat [name = octopus, age = 18]>",
  16. "<OCCat [name = octopus, age = 20]>",
  17. "<OCCat [name = octopus, age = 180]>"
  18. )
  19. Program ended with exit code: 0

五. NSSet 与 NSmutableSet 集合

1. NSSet 功能与用法

(1) NSSet 简介

NSSet 功能简介 :

-- 基本属性 : 无序, 不可重复; 如果将两个相同的元素放在同一个 NSSet 中, 只会保留一个;

-- 性能分析 : NSSet 使用 hash 方法存储集合中的元素, 存取 和 查找性能很好;

(2) NSSet 与 NSArray 的相同之处

NSSet 与 NSArray 相同之处 :

-- 获取元素数量 : 调用 count 方法获取集合元素数量;

-- 快速枚举 : 都可以通过 for (id object in collection) 快速枚举来遍历元素;

-- 枚举器 : 都可以通过 objectEnumerator 方法获取 NSEnumerator 枚举器对集合元素进行遍历;

-- 方法遍历 : 都可以通过 "makeObjectPerformSelector : " 和 "makeObjectPerformSelector : withObject : " 方法对集合元素整体调用某个方法;

-- 代码块遍历 : 都可以通过 "enumerateObjectUsingBlock : " 和 "enumerateObjectWithOptions : usingBlock : " 使用代码块遍历执行集合元素;

-- KVC 编程 : 都可以通过 "valueForKey : " 和 "setValue : forKey : " 进行所有元素的 KVC 编程;

-- KVO 编程 : 都可以通过 "addObserver : forKeyPath : options : context : " 等方法进行 KVO 编程;

(3) NSSet 常用方法

NSSet 常用方法 :

-- "setByAddingObject : " 方法 : 向集合中添加一个新元素, 返回新集合;

-- "setByAddingObjectFromSet : " 方法 : 向 NSSet 集合中添加一个 NSSet 集合, 返回新集合;

-- "setByAddingObjectFromArray : " 方法 : 向 NSSet 集合中添加一个 NSArray 集合, 返回新集合;

-- "allObjects : " 方法 : 将 NSSet 集合中所有元素组成 NSArray , 并返回 NSArray 集合;

-- "anyObject : " 方法 : 返回 NSSet 集合中的某个元素;

-- "containsObject : " 方法 : 判断是否包含 某元素;

-- "member : "方法 : 判断是否包含指定元素, 如果包含返回该元素, 否则返回 nil;

-- "objectPassingTest : " 方法 : 使用代码块过滤集合元素, 通过验证的元素组成新的 NSSet 集合并返回该集合;

-- "objectWithOptions : passingTest : " 方法 : 在 "objectPassingTest : " 基础上额外传入一个 NSEnumerationOptions 参数遍历元素;

-- "isSubsetOfSet : " 方法 : 判断当前 NSSet 集合是否是另外一个集合的子集合;

-- "intersectsSet : " 方法 : 计算两个集合是否有交集;

-- "isEqualToSet : " 方法 : 判断两个集合元素是否相等;

(4) NSSet 代码示例

NSSet 代码示例 :

-- 代码 :

  1. //
  2. // main.m
  3. // 05.NSSet
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSSet * set = [NSSet setWithObjects:@"Tom", @"Jerry", @"Hank", nil];
  17.  
  18. //输出集合大小
  19. NSLog(@"set 集合大小 : %ld", [set count]);
  20.  
  21. NSSet * set1 = [NSSet setWithObjects:@"Angle", @"Devil", nil];
  22. //添加单个元素
  23. set1 = [set1 setByAddingObject:@"Han"];
  24. //添加整个集合
  25. set1 = [set1 setByAddingObjectsFromSet:set];
  26.  
  27. //输出合并后的 NSSet
  28. NSLog(@"set 与 set1 合并后的 NSSet : \n%@", set1);
  29.  
  30. //判断交集
  31. BOOL b1 = [set intersectsSet:set1];
  32. NSLog(@"set1 与 set 是否有交集 : %d", b1);
  33.  
  34. //判断 set 是否是 set1 的子集
  35. BOOL b2 = [set isSubsetOfSet:set1];
  36. NSLog(@"set 是否是 set1 的子集 : %d", b2);
  37.  
  38. //判断 set 是否包含 Tom 字符串
  39. BOOL b3 = [set containsObject:@"Tom"];
  40. NSLog(@"set 是否包含 Tom : %d", b3);
  41.  
  42. //过滤
  43. NSSet * set2 = [set1 objectsPassingTest:^BOOL(id obj, BOOL *stop) {
  44. return (BOOL)([obj length] > 4);
  45. }];
  46. NSLog(@"过滤后的 set2 : \n%@", set2);
  47.  
  48. }
  49. return 0;
  50. }

-- 执行结果 :

  1. 2015-10-18 10:49:22.118 05.NSSet[11008:303] set 集合大小 : 3
  2. 2015-10-18 10:49:22.120 05.NSSet[11008:303] set set1 合并后的 NSSet :
  3. {(
  4. Hank,
  5. Jerry,
  6. Han,
  7. Devil,
  8. Angle,
  9. Tom
  10. )}
  11. 2015-10-18 10:49:22.120 05.NSSet[11008:303] set1 set 是否有交集 : 1
  12. 2015-10-18 10:49:22.121 05.NSSet[11008:303] set 是否是 set1 的子集 : 1
  13. 2015-10-18 10:49:22.121 05.NSSet[11008:303] set 是否包含 Tom : 1
  14. 2015-10-18 10:49:22.122 05.NSSet[11008:303] 过滤后的 set2 :
  15. {(
  16. Jerry,
  17. Devil,
  18. Angle
  19. )}
  20. Program ended with exit code: 0

2. NSSet 判重原理

(1) NSSet 重复判定原理

NSSet 添加元素判断 :

-- 位置判断 : 向 NSSet 中添加元素时, NSSet 会调用对象的 hash 方法 获取对象的 哈希值, 根据 HashCode 判断元素在 NSSet 哈希表中的存储位置;

-- 重复判断 : 两个元素 HashCode 相同, 通过 isEqual : 方法判断, 如果返回 NO 则将两个元素放在同一个位置, 如果返回 YES 添加失败;

NSSet 判断元素相同标准 :

-- 值相等 : 调用 isEqual : 方法返回 YES;

-- hashCode 相等 : 元素的 hash 方法返回值相同;

(2) hash 方法规则

注意 :

-- 重写对象 : 如果重写了对象的 isEqual 方法, 其 hash 方法也应该被重写, 尽量保持 isEqual 方法返回 YES 时, hash 方法也返回 YES;

-- 关于同一位置存储 : hashCode 相同, isEqual 不同的时候, 同一个位置存储多个元素, 需要用链表连接这些元素, 这会降低 NSSet 访问性能;

hash 方法规则 :

-- 同一对象返回的值相同;

-- isEqual 方法返回值相同时, hash 方法返回的值也应该相同;

-- isEqual 标准的实例变量应该用 hashCode 计算;

(3) 代码实例

代码实例 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14.  
  15. - (id) initWithName : (NSString *) _name age : (int) _age;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) initWithName:(NSString *)_name age:(int)_age
  16. {
  17. self = [super init];
  18. self.name = _name;
  19. self.age = _age;
  20. return self;
  21. }
  22.  
  23. - (BOOL) isEqual : (id) other
  24. {
  25. //如果地址相同, 那么比较结果肯定相同
  26. if(self == other)
  27. {
  28. return YES;
  29. }
  30.  
  31. //对象对比之前首先保证类型相同
  32. if([other class] == OCCat.class)
  33. {
  34. OCCat * cat = (OCCat *)other;
  35. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  36. }
  37. return NO;
  38. }
  39.  
  40. - (NSUInteger) hash
  41. {
  42. NSUInteger name_hash = [name hash];
  43.  
  44. return name_hash + age;
  45. }
  46.  
  47. //%@ 打印的时候输出的内容
  48. - (NSString *) description
  49. {
  50. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  51. }
  52.  
  53. @end

-- main.m :

  1. //
  2. // main.m
  3. // 05.NSSet
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSSet * set = [NSSet setWithObjects:
  17. [[OCCat alloc] initWithName:@"Tom" age:18],
  18. [[OCCat alloc] initWithName:@"Jerry" age:20],
  19. [[OCCat alloc] initWithName:@"Tom" age:18],
  20. nil];
  21. NSLog(@"set 集合个数 : %ld \n内容 : \n%@", [set count], set);
  22.  
  23. }
  24. return 0;
  25. }

-- 执行结果 :

  1. 2015-10-18 12:17:44.740 05.NSSet[11464:303] set 集合个数 : 2
  2. 内容 :
  3. {(
  4. <OCCat [name = Tom, age = 18]>,
  5. <OCCat [name = Jerry, age = 20]>
  6. )}
  7. Program ended with exit code: 0

3. NSMutableSet 功能与用法

(1) NSMutableSet 简介

NSMutableSet 简介 : NSMutableSet 可以动态添加, 删除, 修改 集合元素, 可计算集合的 交集 并集 差集;

-- "addObject : " 方法 : 向集合中添加单个元素;

-- "removeObject : " 方法 : 从集合中删除单个元素;

-- "removeAllObjects" 方法 : 删除集合中所有元素;

-- "addObjectsFromArray : " 方法 : 向 NSSet 集合中添加 NSArray 集合中的所有元素;

-- "unionSet : " 方法 : 计算两个 NSSet 并集;

-- "minusSet : " 方法 : 计算两个 NSSet 集合的差集;

-- "setSet : " 方法 : 用一个集合的元素 替换 已有集合的所有的元素;

(2) NSMutableSet 代码示例

NSMutableSet 示例代码 :

-- 示例代码 :

  1. //
  2. // main.m
  3. // 05.NSSet
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSMutableSet * set = [NSMutableSet setWithCapacity:5];
  17. //添加一个
  18. [set addObject:@"Tom"];
  19. //添加3个 元素 从 NSArray
  20. [set addObjectsFromArray:[NSArray arrayWithObjects:@"Jerry", @"Cat", @"Mouse", nil]];
  21. //去掉一个
  22. [set removeObject:@"Mouse"];
  23. NSLog(@"添加一个 添加 3个 去掉一个后的 set : \n%@", set);
  24.  
  25. NSSet * set2 = [NSSet setWithObjects:@"Tom", @"Hank", nil];
  26. [set unionSet:set2];
  27. NSLog(@"合集是 : \n%@", set);
  28.  
  29. }
  30. return 0;
  31. }

-- 执行结果 :

  1. 2015-10-18 17:57:14.948 05.NSSet[11656:303] 添加一个 添加 3 去掉一个后的 set :
  2. {(
  3. Jerry,
  4. Cat,
  5. Tom
  6. )}
  7. 2015-10-18 17:57:14.950 05.NSSet[11656:303] 合集是 :
  8. {(
  9. Jerry,
  10. Cat,
  11. Hank,
  12. Tom
  13. )}
  14. 2015-10-18 17:57:14.950 05.NSSet[11656:303] 差集是 :
  15. {(
  16. Jerry,
  17. Cat
  18. )}
  19. Program ended with exit code: 0

4. NSCountedSet 功能与用法

(1) NSCountedSet 简介

NSCountedSet 简介 :

-- 增加元素 : 该类是 NSMutableSet 的子类, 为每个元素维护了一个 计数 状态, 向该 NSCountedSet 中添加一个元素时, 如果添加成功, 则该元素的添加计数 标注为 1, 如果添加失败, 会再该元素的添加计数 +1;

-- 删除元素 : 从 NSCountedSet 集合中删除元素时, 计数会 -1, 某元素的添加计数减到 0 会删除这个元素;

-- "countForObject : " 方法 : 获取指定元素的添加次数;

(2) NSCountedSet 示例代码

示例代码 :

-- 代码 :

  1. //
  2. // main.m
  3. // 05.NSSet
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSCountedSet * set = [NSCountedSet setWithObjects:@"Tom", @"Jerry", @"Hank", nil];
  17. [set addObject:@"Tom"];
  18. [set addObject:@"Tom"];
  19.  
  20. NSLog(@"set : \n%@", set);
  21.  
  22. [set removeObject:@"Tom"];
  23. NSLog(@"set 删除一次后的效果 : \n%@", set);
  24.  
  25. [set removeObject:@"Tom"];
  26. [set removeObject:@"Tom"];
  27.  
  28. NSLog(@"set 删除三次后的效果 : \n%@", set);
  29.  
  30. }
  31. return 0;
  32. }

-- 执行结果 :

  1. 2015-10-18 18:10:56.253 05.NSSet[11746:303] set :
  2. <NSCountedSet: 0x100200330> (Tom [3], Jerry [1], Hank [1])
  3. 2015-10-18 18:10:56.255 05.NSSet[11746:303] set 删除一次后的效果 :
  4. <NSCountedSet: 0x100200330> (Tom [2], Jerry [1], Hank [1])
  5. 2015-10-18 18:10:56.255 05.NSSet[11746:303] set 删除三次后的效果 :
  6. <NSCountedSet: 0x100200330> (Jerry [1], Hank [1])
  7. Program ended with exit code: 0

5. NSOrderedSet 与 NSMutableOrderedSet 有序集合

(1) 有序集合简介

NSOrderedSet 简介 :

-- 特点 : 不允许重复, 可以保持元素添加顺序, 每个元素都有索引, 可以根据索引操作元素;

(2) 有序集合代码示例

有序集合代码示例 :

-- main.m 代码 :

  1. //
  2. // main.m
  3. // 05.NSSet
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. int main(int argc, const char * argv[])
  12. {
  13.  
  14. @autoreleasepool {
  15.  
  16. NSOrderedSet * set = [NSOrderedSet orderedSetWithObjects:@"Tom", @"Jerry", @"Hank", nil];
  17.  
  18. NSLog(@"第1个元素 : %@", [set firstObject]);
  19. NSLog(@"第2个元素 : %@", [set objectAtIndex : 1]);
  20. NSLog(@"最后1个元素 : %@", [set lastObject]);
  21. NSLog(@"Tom 的索引 : %ld", [set indexOfObject : @"Tom"]);
  22.  
  23. NSIndexSet * index_set = [set indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
  24. return (BOOL) ([obj length] > 3);
  25. }];
  26. NSLog(@"%@", index_set);
  27.  
  28. }
  29. return 0;
  30. }

-- 执行结果 :

  1. 2015-10-18 18:31:30.562 05.NSSet[11925:303] 1个元素 : Tom
  2. 2015-10-18 18:31:30.564 05.NSSet[11925:303] 2个元素 : Jerry
  3. 2015-10-18 18:31:30.565 05.NSSet[11925:303] 最后1个元素 : Hank
  4. 2015-10-18 18:31:30.565 05.NSSet[11925:303] Tom 的索引 : 0
  5. 2015-10-18 18:31:30.566 05.NSSet[11925:303] <NSIndexSet: 0x100103760>[number of indexes: 2 (in 1 ranges), indexes: (1-2)]
  6. Program ended with exit code: 0

六. NSDictionary 与 NSMutableDictionary 字典集合

1. NSDictionary 功能与用法

(1) NSDictionary 简介

NSDictionary 简介 :

-- 作用 : NSDictionary 集合用于保存具有映射关系的数据, 其中保存两组数据, 一组是 key, 一组是 value, key 不允许重复;

-- key 与 value 对应关系 : key 与 value 是一一对应的, 每个 key 都能找到对应的 value;

(2) NSDictionary 方法简介

NSDictionary 创建方法简介 :

-- "dictionary : " 方法 : 创建不包含任何 key value 的 NSDictionary 集合;

-- "dictionaryWithContentsOfFile : / initWithContentsOfFile : " 方法 : 读取文件, 使用文件内容生成 NSDictionary 集合, 一般这种文件是从 NSDictionary 集合输出的, 需要有指定的格式;

-- "dictionaryWithDictionary : / initWithDictionary : " 方法 : 使用现有的 NSDictionary 生成新的 NSDictionary;

-- "dictionaryWithObject : forKey : " 方法 : 使用单个 key-value 创建 NSDictionary 集合;

-- "dictionaryWithObjects : forKeys : / initWithObjects : forKeys : " 方法 : 使用两个 NSArray 分别指定 key 和 value 集合;

-- "dictionaryWithObjectsAndKeys : / initWithObjectAndKeys : " 方法 : 调用该方法, 需要按照 key-value 格式传入多个 键值对集合;

(3) NSDictionary 常用方法

NSDictionary 常用方法 :

-- "count : " 方法 : 返回 NSDictionary 含有的 key-value 的数量;

-- "allKeys : " 方法 : 返回 NSDictionary 集合含有的 key 集合;

-- "allKeysForObject : " 方法 : 返回指定 Value 的全部 key 的集合;

-- "allValues : " 方法 : 该方法返回 NSDictionary 包含的全部 value 集合;

-- "objectForKeyedSubscript : " 方法 : 允许 NSDictionary 通过下标方法获取指定 key 的 value;

-- "valueForKey : " 方法 : 获取 NSDictionary 指定 key 的 value 值;

-- "keyEnumerator : " 方法 : 返回 key 集合的 NSEnumerator 对象;

-- "objectEnumerator : " 方法 : 返回 value 集合的 NSEnumerator 对象;

-- "enumerateKeysAndObjectsUsingBlock : " 方法 : 使用指定的代码块迭代集合中的键值对;

-- "enumerateKeysAndObjectsWithOptions : usingBlock : " 方法 : 使用指定代码块迭代集合中的键值对, 额外传入一个 NSEnumerationOptions 参数;

-- "writeToFile : atomically : " 方法 : 将 NSDictionary 对象数据写入文件中;

(4) NSDictionary 示例代码

NSDictionary 示例代码 :

-- NSDictionary+toString.h :

  1. //
  2. // NSDictionary+toString.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface NSDictionary (toString)
  12. - (void) toString;
  13. @end

-- NSDictionary+toString.m :

  1. //
  2. // NSDictionary+toString.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "NSDictionary+toString.h"
  10.  
  11. @implementation NSDictionary (toString)
  12. - (void) toString
  13. {
  14. NSMutableString * string = [NSMutableString stringWithString:@"{"];
  15.  
  16. for(id key in self)
  17. {
  18. [string appendString:[key description]];
  19. [string appendString:@" = "];
  20. [string appendString:[self[key] description]];
  21. [string appendString:@" , "];
  22. }
  23.  
  24. [string appendString:@"}"];
  25. NSLog(@"%@", string);
  26. }
  27. @end

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14.  
  15. - (id) initWithName : (NSString *) _name age : (int) _age;
  16. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) initWithName:(NSString *)_name age:(int)_age
  16. {
  17. self = [super init];
  18. self.name = _name;
  19. self.age = _age;
  20. return self;
  21. }
  22.  
  23. - (BOOL) isEqual : (id) other
  24. {
  25. //如果地址相同, 那么比较结果肯定相同
  26. if(self == other)
  27. {
  28. return YES;
  29. }
  30.  
  31. //对象对比之前首先保证类型相同
  32. if([other class] == OCCat.class)
  33. {
  34. OCCat * cat = (OCCat *)other;
  35. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  36. }
  37. return NO;
  38. }
  39.  
  40. - (NSUInteger) hash
  41. {
  42. NSUInteger name_hash = [name hash];
  43.  
  44. return name_hash + age;
  45. }
  46.  
  47. //%@ 打印的时候输出的内容
  48. - (NSString *) description
  49. {
  50. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  51. }
  52. @end

-- main.m :

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "NSDictionary+toString.h"
  11. #import "OCCat.h"
  12.  
  13. int main(int argc, const char * argv[])
  14. {
  15.  
  16. @autoreleasepool {
  17.  
  18. NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  19. [[OCCat alloc] initWithName:@"Tom" age:18], @"Tom",
  20. [[OCCat alloc] initWithName:@"Jerry" age:20], @"Jerry",
  21. [[OCCat alloc] initWithName:@"Hank" age:26], @"Hank",
  22. nil];
  23.  
  24. //自己实现的 NSDIctionary 打印方法
  25. [dictionary toString];
  26.  
  27. NSLog(@"dictionary 键值对个数 : %ld", [dictionary count]);
  28.  
  29. NSLog(@"打印所有 key : \n%@", [dictionary allKeys]);
  30.  
  31. NSLog(@"打印所有 value : \n%@", [dictionary allValues]);
  32.  
  33. //枚举器遍历
  34. NSEnumerator * enumerator = [dictionary objectEnumerator];
  35. NSObject * value;
  36. while(value = [enumerator nextObject])
  37. {
  38. NSLog(@"%@", value);
  39. }
  40.  
  41. //使用代码块遍历 NSDictionary 集合
  42. [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  43.  
  44. NSLog(@"key : %@ , value : %@", key, obj);
  45. }];
  46.  
  47. }
  48. return 0;
  49. }

-- 执行结果 :

  1. 2015-10-18 21:49:15.902 06.NSDictionary[12562:303] {Tom = <OCCat [name = Tom, age = 18]> , Jerry = <OCCat [name = Jerry, age = 20]> , Hank = <OCCat [name = Hank, age = 26]> , }
  2. 2015-10-18 21:49:15.904 06.NSDictionary[12562:303] dictionary 键值对个数 : 3
  3. 2015-10-18 21:49:15.904 06.NSDictionary[12562:303] 打印所有 key :
  4. (
  5. Tom,
  6. Jerry,
  7. Hank
  8. )
  9. 2015-10-18 21:49:15.905 06.NSDictionary[12562:303] 打印所有 value :
  10. (
  11. "<OCCat [name = Tom, age = 18]>",
  12. "<OCCat [name = Jerry, age = 20]>",
  13. "<OCCat [name = Hank, age = 26]>"
  14. )
  15. 2015-10-18 21:49:15.910 06.NSDictionary[12562:303] <OCCat [name = Tom, age = 18]>
  16. 2015-10-18 21:49:15.910 06.NSDictionary[12562:303] <OCCat [name = Jerry, age = 20]>
  17. 2015-10-18 21:49:15.910 06.NSDictionary[12562:303] <OCCat [name = Hank, age = 26]>
  18. 2015-10-18 21:49:15.911 06.NSDictionary[12562:303] key : Tom , value : <OCCat [name = Tom, age = 18]>
  19. 2015-10-18 21:49:15.911 06.NSDictionary[12562:303] key : Jerry , value : <OCCat [name = Jerry, age = 20]>
  20. 2015-10-18 21:49:15.912 06.NSDictionary[12562:303] key : Hank , value : <OCCat [name = Hank, age = 26]>
  21. Program ended with exit code: 0

2. NSDictionary key 排序

(1) NSDictionary Key 排序简介

NSDictionary 排序方法 :

-- "keysSortedByValueUsingSelector : " 方法 : 根据 value 指定方法 遍历 key-value 键值对, 对 key 排序, value 的指定方法必返回 NSOrderedSame, NSOrderedAscending 或 NSOrderedDescending 返回值;

-- "keysSortedByValueUsingComparator" 方法 : 根据 指定代码块 遍历 键值对, 对 key 进行排序, 代码块方法 需要返回 NSOrderedAscending, NSOrderedDescending 或 NSOrderedSame 返回值;

-- "keysSortedByValueWithOptions : usingComparator : " 方法 : 与前一个类似, 额外增加了一个 NSEnumerationOptions 参数;

(2) NSDictionary key排序示例代码

NSDictionary Key 排序示例代码 :

-- OCCat.h : 定义 compare : 比较方法;

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14.  
  15. - (id) initWithName : (NSString *) _name age : (int) _age;
  16. - (NSComparisonResult) compare : (id) other;
  17. @end

-- OCCat.m : 实现 compare : 比较方法;

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) initWithName:(NSString *)_name age:(int)_age
  16. {
  17. self = [super init];
  18. self.name = _name;
  19. self.age = _age;
  20. return self;
  21. }
  22.  
  23. - (BOOL) isEqual : (id) other
  24. {
  25. //如果地址相同, 那么比较结果肯定相同
  26. if(self == other)
  27. {
  28. return YES;
  29. }
  30.  
  31. //对象对比之前首先保证类型相同
  32. if([other class] == OCCat.class)
  33. {
  34. OCCat * cat = (OCCat *)other;
  35. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  36. }
  37. return NO;
  38. }
  39.  
  40. - (NSUInteger) hash
  41. {
  42. NSUInteger name_hash = [name hash];
  43.  
  44. return name_hash + age;
  45. }
  46.  
  47. //%@ 打印的时候输出的内容
  48. - (NSString *) description
  49. {
  50. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  51. }
  52.  
  53. - (NSComparisonResult) compare:(id)other
  54. {
  55. if([other class] == OCCat.class)
  56. {
  57. OCCat * other_cat = (OCCat *)other;
  58. if(self.age == other_cat.age)
  59. {
  60. return NSOrderedSame;
  61. }
  62. else if(self.age > other_cat.age)
  63. {
  64. return NSOrderedDescending;
  65. }
  66. else
  67. {
  68. return NSOrderedAscending;
  69. }
  70. }
  71. return NSOrderedSame;
  72. }
  73. @end

-- main.m :

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "NSDictionary+toString.h"
  11. #import "OCCat.h"
  12.  
  13. int main(int argc, const char * argv[])
  14. {
  15.  
  16. @autoreleasepool {
  17.  
  18. NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  19. [[OCCat alloc] initWithName:@"Tom" age:18], @"Tom",
  20. [[OCCat alloc] initWithName:@"Hank" age:26], @"Hank",
  21. [[OCCat alloc] initWithName:@"Jerry" age:20], @"Jerry",
  22. nil];
  23.  
  24. //自己实现的 NSDIctionary 打印方法
  25. [dictionary toString];
  26.  
  27. //使用 keysSortedByValueUsingSelector 方法排序
  28. NSArray * array = [dictionary keysSortedByValueUsingSelector:@selector(compare:)];
  29. NSLog(@"array : \n%@", array);
  30.  
  31. //使用代码块排序
  32. NSArray * array2 = [dictionary keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  33. OCCat * cat1 = (OCCat *)obj1;
  34. OCCat * cat2 = (OCCat *)obj2;
  35. if(cat1.age == cat2.age)
  36. {
  37. return NSOrderedSame;
  38. }
  39.  
  40. if(cat1.age > cat2.age)
  41. {
  42. return NSOrderedDescending;
  43. }
  44.  
  45. return NSOrderedAscending;
  46. }];
  47. NSLog(@"array2 : \n%@", array2);
  48.  
  49. }
  50. return 0;
  51. }

-- 执行结果 :

  1. 2015-10-19 12:57:17.276 06.NSDictionary[13537:303] {Tom = <OCCat [name = Tom, age = 18]> , Jerry = <OCCat [name = Jerry, age = 20]> , Hank = <OCCat [name = Hank, age = 26]> , }
  2. 2015-10-19 12:57:17.279 06.NSDictionary[13537:303] array2 :
  3. (
  4. Tom,
  5. Jerry,
  6. Hank
  7. )
  8. 2015-10-19 12:57:17.279 06.NSDictionary[13537:303] array :
  9. (
  10. Tom,
  11. Jerry,
  12. Hank
  13. )
  14. Program ended with exit code: 0

3. NSDictionary key 过滤

(1) NSDictionary Key 过滤方法简介

NSDictionary key 过滤方法简介 :

-- "keysOfEntriesPassingTest : " 方法 : 使用代码块 遍历 键值对, 代码块返回一个 BOOL 值, 参数一 是 key, 参数二 是 value, 第三个是循环标志位;

-- "keysOfEntriesWithOptions : passingTest : " 方法 : 与前一个方法功能基本相同, 增加了 NSEnumerationOptions 参数;

(2) NSDictionary Key 过滤代码示例

示例代码 : main.m 不同, 其它代码均相同;

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "NSDictionary+toString.h"
  11. #import "OCCat.h"
  12.  
  13. int main(int argc, const char * argv[])
  14. {
  15.  
  16. @autoreleasepool {
  17.  
  18. NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  19. [[OCCat alloc] initWithName:@"Tom" age:18], @"Tom",
  20. [[OCCat alloc] initWithName:@"Hank" age:26], @"Hank",
  21. [[OCCat alloc] initWithName:@"Jerry" age:20], @"Jerry",
  22. nil];
  23.  
  24. //自己实现的 NSDIctionary 打印方法
  25. [dictionary toString];
  26.  
  27. NSSet * set = [dictionary keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {
  28. OCCat * cat = (OCCat *)obj;
  29. if(cat.age >= 20)
  30. {
  31. return YES;
  32. }
  33. return NO;
  34. }];
  35.  
  36. NSLog(@"过滤后的key集合 : \n%@", set);
  37.  
  38. }
  39. return 0;
  40. }

-- 执行结果 :

  1. 2015-10-19 13:59:42.931 06.NSDictionary[13750:303] {Tom = <OCCat [name = Tom, age = 18]> , Jerry = <OCCat [name = Jerry, age = 20]> , Hank = <OCCat [name = Hank, age = 26]> , }
  2. 2015-10-19 13:59:42.934 06.NSDictionary[13750:303] 过滤后的key集合 :
  3. {(
  4. Jerry,
  5. Hank
  6. )}
  7. Program ended with exit code: 0

4. 自定义类作为 key

(1) 自定义类为 key 前提

自定义类为 key 基本要求 :

-- 重写isEqual 和 hash 方法 : isEqual : 和 hash : 方法必须正确重写, 即 isEqual 方法返回 YES 是, hash 方法返回的 hashCode 必须也相同;

-- 实现 copyWithZone 方法 : 尽量返回对象的不可变副本; 该策略是出于安全性考虑, 当多个 key-value 放入 NSDictionary 之后, 如果 key 可变导致 key 的hashCode 改变, 就会出现问题;

(2) 自定义类为 key 代码示例

自定义类为 key 代码 : 其它类代码相同, 只有 OCCat.m 与 main.m 不同;

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) initWithName:(NSString *)_name age:(int)_age
  16. {
  17. self = [super init];
  18. self.name = _name;
  19. self.age = _age;
  20. return self;
  21. }
  22.  
  23. - (BOOL) isEqual : (id) other
  24. {
  25. //如果地址相同, 那么比较结果肯定相同
  26. if(self == other)
  27. {
  28. return YES;
  29. }
  30.  
  31. //对象对比之前首先保证类型相同
  32. if([other class] == OCCat.class)
  33. {
  34. OCCat * cat = (OCCat *)other;
  35. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  36. }
  37. return NO;
  38. }
  39.  
  40. - (NSUInteger) hash
  41. {
  42. NSUInteger name_hash = [name hash];
  43.  
  44. return name_hash + age;
  45. }
  46.  
  47. //%@ 打印的时候输出的内容
  48. - (NSString *) description
  49. {
  50. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  51. }
  52.  
  53. - (id) copyWithZone : (NSZone *) zone
  54. {
  55. OCCat * cat = [[[self class] allocWithZone:zone] init];
  56. cat.name = self.name;
  57. cat.age = self.age;
  58. return cat;
  59. }
  60.  
  61. - (NSComparisonResult) compare:(id)other
  62. {
  63. if([other class] == OCCat.class)
  64. {
  65. OCCat * other_cat = (OCCat *)other;
  66. if(self.age == other_cat.age)
  67. {
  68. return NSOrderedSame;
  69. }
  70. else if(self.age > other_cat.age)
  71. {
  72. return NSOrderedDescending;
  73. }
  74. else
  75. {
  76. return NSOrderedAscending;
  77. }
  78. }
  79. return NSOrderedSame;
  80. }
  81. @end

-- main.m :

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "NSDictionary+toString.h"
  11. #import "OCCat.h"
  12.  
  13. int main(int argc, const char * argv[])
  14. {
  15.  
  16. @autoreleasepool {
  17.  
  18. NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  19. @"Tom", [[OCCat alloc] initWithName:@"Tom" age:18],
  20. @"Hank", [[OCCat alloc] initWithName:@"Hank" age:26],
  21. @"Jerry", [[OCCat alloc] initWithName:@"Jerry" age:20],
  22. nil];
  23.  
  24. //自己实现的 NSDIctionary 打印方法
  25. [dictionary toString];
  26.  
  27. }
  28. return 0;
  29. }

-- 执行结果 : 

  1. 2015-10-20 14:09:30.935 06.NSDictionary[15258:303] {<OCCat [name = Tom, age = 18]> = Tom , <OCCat [name = Hank, age = 26]> = Hank , <OCCat [name = Jerry, age = 20]> = Jerry , }
  2. Program ended with exit code: 0

5. NSMutableDictionary 功能与用法

(1) NSMutableDictionary 简介

NSMutableDictionary 独有方法简介 : 主要是比 NSDictionary 多个 增加 删除 键值对的方法;

-- "setObject : forKey : " 方法 : 设置 key-value 键值对, 如果 key 与之前的不重复, 直接插入, 如果重复, 直接将 value 替换;

-- "setObject : forKeyedSubscript : " 方法 : 通过下标方法设置键值对;

-- "addEntriesFromDictionary : " 方法 : 将另一个 NSDictionary 中的值复制到当前的 NSMutableDictionary 中;

-- "setDictionary : " 方法 : 用一个 NSDictionary 中的所有元素 替换另一个 NSDictionary 中的所有元素;

-- "removeObjectForKey : " 方法 : 根据 key 删除 键值对;

-- "removeAllObjects : " 方法 : 清空 NSDictionary;

-- "removeObjectForKeys : " 方法 : 删除一个 NSArray 中包含的 key 的键值对;

(2) NSMutableDictionary 方法示例

NSMutableDictionary 示例代码 :

-- 代码 :

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "NSDictionary+toString.h"
  11. #import "OCCat.h"
  12.  
  13. int main(int argc, const char * argv[])
  14. {
  15.  
  16. @autoreleasepool {
  17.  
  18. NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  19. @"Tom", [NSNumber numberWithInt:1],
  20. @"Jerry", [NSNumber numberWithInt:2],
  21. nil];
  22. [dictionary toString];
  23.  
  24. //替换
  25. NSLog(@"替换");
  26. dictionary[[NSNumber numberWithInt:1]] = @"Hank";
  27. [dictionary toString];
  28.  
  29. //增加
  30. NSLog(@"增加");
  31. dictionary[[NSNumber numberWithInt:3]] = @"Fuck";
  32. [dictionary toString];
  33.  
  34. //添加整个字典到另一个字典
  35. NSLog(@"添加整个字典到另一个字典");
  36. NSDictionary * dic1 = [NSDictionary dictionaryWithObjectsAndKeys:
  37. @"Obama", [NSNumber numberWithInt:4],
  38. @"Bush", [NSNumber numberWithInt:5],
  39. nil];
  40. [dictionary addEntriesFromDictionary:dic1];
  41. [dictionary toString];
  42.  
  43. //删除一部分
  44. NSLog(@"删除一部分");
  45. [dictionary removeObjectsForKeys:[NSArray arrayWithObjects:
  46. [NSNumber numberWithInt:4], [NSNumber numberWithInt:5], nil]];
  47. [dictionary toString];
  48.  
  49. }
  50. return 0;
  51. }

-- 执行结果 :

  1. 2015-10-20 15:04:01.100 06.NSDictionary[15426:303] {1 = Tom , 2 = Jerry , }
  2. 2015-10-20 15:04:01.102 06.NSDictionary[15426:303] 替换
  3. 2015-10-20 15:04:01.103 06.NSDictionary[15426:303] {1 = Hank , 2 = Jerry , }
  4. 2015-10-20 15:04:01.103 06.NSDictionary[15426:303] 增加
  5. 2015-10-20 15:04:01.104 06.NSDictionary[15426:303] {3 = Fuck , 1 = Hank , 2 = Jerry , }
  6. 2015-10-20 15:04:01.105 06.NSDictionary[15426:303] 添加整个字典到另一个字典
  7. 2015-10-20 15:04:01.106 06.NSDictionary[15426:303] {3 = Fuck , 2 = Jerry , 5 = Bush , 1 = Hank , 4 = Obama , }
  8. 2015-10-20 15:04:01.106 06.NSDictionary[15426:303] 删除一部分
  9. 2015-10-20 15:04:01.107 06.NSDictionary[15426:303] {3 = Fuck , 2 = Jerry , 1 = Hank , }
  10. Program ended with exit code: 0

六. 谓词 NSPredicate

1. 谓词简介

(1) 谓词简介

谓词简介 : 个人感觉 谓词比较像 Java 中的正则表达式;

-- 作用 : 谓词用于定义 逻辑条件, 用于 搜索 或 过滤内存中的数据, 尤其是 搜索过滤集合中的数据;

-- NSPredicate 子类 : NSPredicate 有 3 个子类, NSComparisonPredicate , NSCompoundPredicate, NSExpression;

-- 创建方法 : 使用 NSPredicate 的 "predicateWithFormat :" 方法 创建 NSPredicate 对象;

-- 没有占位符的谓词结果计算 : 直接使用 NSPredicate 的 evalueWithObject 方法计算谓词结果, 返回值是一个 BOOL 值;

-- 有占位符的谓词结果计算 : 调用 "predicateWithSubstitutionVariables" 方法为占位符参数设置参数值, 然后执行 evalueWithObject 方法计算结果;

-- 同时完成两步的方法 : NSPredicate 提供了一个 "evalueWithObject : substitutionVariables : " 方法可以同时替换占位符 和 计算结果;

(2) 谓词 代码简单示例

谓词代码示例 :

-- OCCat.h :

  1. //
  2. // OCCat.h
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface OCCat : NSObject
  12. @property (nonatomic, copy) NSString * name;
  13. @property (nonatomic, assign) int age;
  14.  
  15. - (id) initWithName : (NSString *) _name age : (int) _age;
  16. - (NSComparisonResult) compare : (id) other;
  17. @end

-- OCCat.m :

  1. //
  2. // OCCat.m
  3. // octopus
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import "OCCat.h"
  10.  
  11. @implementation OCCat
  12. @synthesize name;
  13. @synthesize age;
  14.  
  15. - (id) initWithName:(NSString *)_name age:(int)_age
  16. {
  17. self = [super init];
  18. self.name = _name;
  19. self.age = _age;
  20. return self;
  21. }
  22.  
  23. - (BOOL) isEqual : (id) other
  24. {
  25. //如果地址相同, 那么比较结果肯定相同
  26. if(self == other)
  27. {
  28. return YES;
  29. }
  30.  
  31. //对象对比之前首先保证类型相同
  32. if([other class] == OCCat.class)
  33. {
  34. OCCat * cat = (OCCat *)other;
  35. return [self.name isEqualToString:cat.name] && (self.age == cat.age);
  36. }
  37. return NO;
  38. }
  39.  
  40. - (NSUInteger) hash
  41. {
  42. NSUInteger name_hash = [name hash];
  43.  
  44. return name_hash + age;
  45. }
  46.  
  47. //%@ 打印的时候输出的内容
  48. - (NSString *) description
  49. {
  50. return [NSString stringWithFormat:@"<OCCat [name = %@, age = %d]>", self.name, self.age];
  51. }
  52.  
  53. - (id) copyWithZone : (NSZone *) zone
  54. {
  55. OCCat * cat = [[[self class] allocWithZone:zone] init];
  56. cat.name = self.name;
  57. cat.age = self.age;
  58. return cat;
  59. }
  60.  
  61. - (NSComparisonResult) compare:(id)other
  62. {
  63. if([other class] == OCCat.class)
  64. {
  65. OCCat * other_cat = (OCCat *)other;
  66. if(self.age == other_cat.age)
  67. {
  68. return NSOrderedSame;
  69. }
  70. else if(self.age > other_cat.age)
  71. {
  72. return NSOrderedDescending;
  73. }
  74. else
  75. {
  76. return NSOrderedAscending;
  77. }
  78. }
  79. return NSOrderedSame;
  80. }
  81. @end

-- main.m : 其它类 与 NSDictionary 中的示例相同;

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16.  
  17. NSPredicate * predicate = [NSPredicate predicateWithFormat:@"name like 'T*'"];
  18. OCCat * cat = [[OCCat alloc] initWithName:@"Tom" age:18];
  19. OCCat * cat2 = [[OCCat alloc] initWithName:@"Jerry" age:20];
  20.  
  21. NSLog(@"cat : %d, cat2 : %d", [predicate evaluateWithObject:cat], [predicate evaluateWithObject:cat2]);
  22.  
  23. }
  24. return 0;
  25. }

-- 执行结果 :

  1. 2015-10-20 16:21:32.998 06.NSDictionary[15545:303] cat : 1, cat2 : 0
  2. Program ended with exit code: 0

2. 谓词过滤集合

(1) 集合过滤方法简介

谓词方法简介 : 谓词遍历集合时, 使用谓词对集合中的元素进行过滤, 元素计算谓词返回 YES 才会被保留下来, 返回 NO, 该元素就会被删除;

-- "- (NSArray * ) filteredArrayUsingPredicate : (NSPredicate *) predicate :" 方法 : 使用谓词过滤 NSArray 集合, 返回过滤后的新集合;

-- "- (void) filterUsingPredicate : (NSPredicate *) predicate :" 方法 : 使用谓词过滤 NSMutableArray 集合, 直接删除集合中的不合格的元素;

-- "- (NSSet * ) filteredSetUsingPredicate : (NSPredicate *) Predicate :" 方法 : 使用谓词过滤 NSSet 集合, 返回一个新的集合;

-- "- (void) filterUsingPredicate : (NSPredicate * ) Predicate :" 方法 : 过滤 NSMutableSet 集合, 直接删除不合格的元素;

(2) 集合过滤方法代码

示例代码 :

-- main.c :

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16.  
  17. NSMutableArray * array = [NSMutableArray arrayWithObjects:
  18. [NSNumber numberWithInt:10],
  19. [NSNumber numberWithInt:20],
  20. [NSNumber numberWithInt:30],
  21. nil];
  22. //过滤大于 15 的数字
  23. NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF > 15"];
  24. [array filterUsingPredicate:predicate];
  25. NSLog(@"过滤后的 array : \n%@", array);
  26.  
  27. NSSet * set = [NSSet setWithObjects:
  28. [[OCCat alloc] initWithName:@"Tom" age:18],
  29. [[OCCat alloc] initWithName:@"Jerry" age:20],
  30. nil];
  31. //过滤 name 包含 om 字符串的 NSSet
  32. NSPredicate * predicate1 = [NSPredicate predicateWithFormat:@"name CONTAINS 'om'"];
  33. NSSet * filterSet = [set filteredSetUsingPredicate:predicate1];
  34. NSLog(@"过滤后的 set : \n%@", filterSet);
  35.  
  36. }
  37. return 0;
  38. }

-- 执行结果 :

  1. 2015-10-20 21:17:41.615 06.NSDictionary[15761:303] 过滤后的 array :
  2. (
  3. 20,
  4. 30
  5. )
  6. 2015-10-20 21:17:41.617 06.NSDictionary[15761:303] 过滤后的 set :
  7. {(
  8. <OCCat [name = Tom, age = 18]>
  9. )}
  10. Program ended with exit code: 0

3. 谓词 占位符

(1) 谓词占位符参数

谓词支持的占位符 :

-- "%K" 占位符 : 动态传入属性名;

-- "%@" 占位符 : 动态设置属性值;

动态改变的属性值 :

-- 示例 : [NSPredicate PredicateWithFormat : @"name CATAINS $JAVA"], $JAVA 是动态改变值, 程序可以动态改变 $JAVA 值;

-- 设置参数值 : 调用 NSPredicate 的 predicateWithSubstitutionVariables : 设置参数值, 该方法返回 NSPredicate 对象;

-- 计算结果 : 调用 NSPredicate 的 evaluateWithObject : substitutionVariables : 完成参数计算结果;

(2) 谓词占位符参数 代码示例

代码示例 :

-- main.m :

  1. //
  2. // main.m
  3. // 06.NSDictionary
  4. //
  5. // Created by octopus on 15-10-18.
  6. // Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "OCCat.h"
  11.  
  12. int main(int argc, const char * argv[])
  13. {
  14.  
  15. @autoreleasepool {
  16.  
  17. NSArray * array = [NSArray arrayWithObjects:
  18. [[OCCat alloc] initWithName:@"Tom" age:18],
  19. [[OCCat alloc] initWithName:@"Jerry" age:19],
  20. [[OCCat alloc] initWithName:@"John" age:20],
  21. [[OCCat alloc] initWithName:@"Fuck" age:13],
  22. nil];
  23. //谓词过滤, name 属性 包含 "o" 字符串的元素
  24. NSPredicate * predicate = [NSPredicate predicateWithFormat:@"%K CONTAINS %@", @"name", @"o"];
  25. NSArray * array1 = [array filteredArrayUsingPredicate:predicate];
  26. NSLog(@"包含有 o 字符串集合 : \n%@", array1);
  27.  
  28. NSPredicate * predicateTemp = [NSPredicate predicateWithFormat:@"%K CONTAINS $JAVA", @"name"];
  29. NSPredicate * predicate2 = [predicateTemp predicateWithSubstitutionVariables:
  30. [NSDictionary dictionaryWithObjectsAndKeys:
  31. @"o", @"JAVA", nil]];
  32. NSArray * array2 = [array filteredArrayUsingPredicate:predicate2];
  33. NSLog(@"array2 : \n%@", array2);
  34.  
  35. }
  36. return 0;
  37. }

-- 执行结果 :

  1. 2015-10-21 09:40:18.593 06.NSDictionary[16254:303] 包含有 o 字符串集合 :
  2. (
  3. "<OCCat [name = Tom, age = 18]>",
  4. "<OCCat [name = John, age = 20]>"
  5. )
  6. 2015-10-21 09:40:18.595 06.NSDictionary[16254:303] array2 :
  7. (
  8. "<OCCat [name = Tom, age = 18]>",
  9. "<OCCat [name = John, age = 20]>"
  10. )
  11. Program ended with exit code: 0

4. 谓词 语法

谓词表达式 : 使用谓词, 需要有谓词表达式, 该表达式会返回 BOOL 值;

(1) 比较运算符

比较运算符 :

-- "==" "=" 运算符 : 判断左右两边表达式是否相等;

-- ">=" "=>" 运算符 : 左边 是否大于等于 右边 运算符;

-- "<=" "=<" 运算符 : 左边 是否小于等于 右边运算符;

-- ">" 运算符 : 左边 是否 大于 右边;

-- "<" 运算符 : 左边 是否 小于 右边;

-- "!=" "<>" 运算符 : 两个表达式是否不相等;

-- "BETWEEN" 运算符 : 表达式格式 "BETWEEN {下限, 上限}", 表达式 大于等于 下限, 表达式 小于等于 上限;

(2) 逻辑运算符

逻辑运算符 :

-- "AND" "&&" 运算符 : 逻辑与;

-- "OR" "||" 运算符 : 逻辑或;

-- "NOT" "!" 运算符 : 逻辑非;

(3) 逻辑运算符

逻辑运算符 :

-- "BEGINSWITH" 运算符 : 字符串是否以指定字符串开头;

-- "ENDSSWITH" 运算符 : 字符串是否以指定字符串结尾;

-- "CONTAINS" 运算符 : 字符串是否包含指定字符串;

-- "LIKE" 运算符 : 匹配指定字符串, 允许 "*" 和 "?" 通配符, * 代表多个字符, ? 代表一个字符;

-- "MATCHES" 运算符 : 匹配正则表达式;

(4) 集合操作运算符

集合操作运算符 :

-- "ANY" "SOME" 运算符 : 集合中任意一个元素满足条件, 返回 YES;

-- "ALL" 运算符 : 集合中所有的元素满足条件 才 返回 YES;

-- "NONE" 运算符 : 没有任何元素满足条件 返回 YES;

-- "IN" 运算符 : 左边的表达式出现在右边的集合中 返回 YES;

-- "array[index]" 运算符 : 返回 array 中 index 索引元素;

-- "array[FIRST]" 运算符 : 返回 array 中的第一个元素;

-- "array[LAST]" 运算符 : 返回 array 中最后一个元素;

-- "array[SIZE]" 运算符 : 返回 array 中元素个数;

(5) 直接量

直接量 :

-- "FALSE" "NO" : 假逻辑;

-- "TRUE" "YES" : 真逻辑;

-- "NULL" "NIL" : 空值;

-- "SELF" : 对象本身;

-- "text" 'text' :  字符串;

【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词的更多相关文章

  1. 李洪强iOS之Foundation框架—字符串

    Foundation框架—字符串 一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:O ...

  2. iOS开发-常用第三方开源框架介绍

    iOS开发-常用第三方开源框架介绍 图像: 1.图片浏览控件MWPhotoBrowser        实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网 ...

  3. iOS开发之常用第三方框架(下载地址,使用方法,总结)

    iOS开发之常用第三方框架(下载地址,使用方法,总结) 说句实话,自学了这么久iOS,如果说我不知道的但是又基本上都摸遍了iOS相关知识,但是每次做项目的时候,遇到难一点的地方或者没试过的东西就闷了. ...

  4. Android &Swift iOS开发:语言与框架对比

    转载自:http://www.infoq.com/cn/articles/from-android-to-swift-ios?utm_campaign=rightbar_v2&utm_sour ...

  5. Foundation框架—字符串

    Foundation框架—字符串 一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:O ...

  6. Objective - c Foundation 框架详解2

    Objective - c  Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...

  7. iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角)--(转)

    图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作. 下 ...

  8. 浅谈iOS开发中多语言的字符串排序

    一.前言 在iOS开发中,一个经常的场景是利用tableview展示一组数据,以很多首歌曲为例子.为了便于查找,一般会把这些歌曲按照一定的顺序排列,还会加上索引条以便于快速定位. 由于歌曲名可能有数字 ...

  9. IOS开发学习笔记016-Foundation框架

     Foundation 框架的学习 一.Foundation 常用结构体 1.NSRange(location,length)  typedef struct _NSRange { NSUIntege ...

随机推荐

  1. POJ1222熄灯问题

    千年老题,以前用枚举做,现在用高斯消元做 自由元直接做成0即可 #include<cstdio> #include<cstdlib> #include<algorithm ...

  2. hdu 5510 Bazinga(字符串kmp)

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  3. 求n个数的最小公倍数

    解决的问题: 对于一个长度为n序列ai,求ai的最小公倍数 解析: 我们知道,如果求两个数a,b的LCM=a*b/gcd(a,b),多个数我们可以两两求LCM,再合并,这样会爆long long 所以 ...

  4. WPF 实现换肤功能

    将所有控件的基本样式汇集到一个资源字典中,构成界面的基本样式文件,然后进行不同颜色皮肤的定制. 即在新的皮肤资源字典文件中引入基本样式文件,然后使用资源继承,并且只设置控件的颜色属性等,形成一个皮肤文 ...

  5. 关于 printf scanf getchar

    float默认小数6位 右对齐.-m 左对齐 在调用printf函数输出数据时,当数据的实际位宽大于printf函数中的指定位宽时,将按照数据的实际位宽输出数据. .n表精度 输出%符号 注意点 #i ...

  6. redis分布式锁-SETNX实现

    Redis有一系列的命令,特点是以NX结尾,NX是Not eXists的缩写,如SETNX命令就应该理解为:SET if Not eXists.这系列的命令非常有用,这里讲使用SETNX来实现分布式锁 ...

  7. 聊聊并发(一)深入分析Volatile的实现原理

    本文属于作者原创,原文发表于InfoQ:http://www.infoq.com/cn/articles/ftf-java-volatile 引言 在多线程并发编程中synchronized和Vola ...

  8. Hibernate更新数据(不用update也可以)

    在介绍hibernate的更新之前,我们先来看看session的两个方法.load和get方法:这两个方法是获取数据的根据对象的id值: 先看两段代码.load和get的方法都含有两个参数,前者是得到 ...

  9. numpy.squeeze()是干啥的

    例子: a = 3 print np.squeeze(a) # 输出3 a = [3] print np.squeeze(a) # 输出3 a = [[3]] print np.squeeze(a) ...

  10. 3.5 find() 判断是否存在某元素

    vector 判断是否存在某元素: if(find(A.begin(), A.end(), A[i]) != A.end()){ // 若存在 A[i] // find() 返回一个指针 }