iOS开发 —— Objective C语言知识点总结

来源:互联网 发布:禁止应用使用移动数据 编辑:程序博客网 时间:2024/05/16 13:46

C和OC对比

  1. OC中主要开发在什么平台上的应用程序? 
    答:可以使用OC开发Mac OS X平台和iOS平台的应用程序

  2. OC中新增关键字大部分是以什么开头? 
    答:OC中新增关键字大部分是以@开头

  3. OC中新增加了那些数据类型? 
    答:Block类型 
    指针类型(Class, id类型) 
    空类型 
    特殊类型(SEL, nil)

  4. 面向对象特性是什么? 
    答:继承性,封装性,多态性

  5. import和#include有什么区别?

    答:import 的功能和 include一样, 是将右边的文件拷贝到当前import的位置.为了降低程序员的负担, 防止重复导入, 避免程序员去书写 头文件卫士, 那么OC给出来一个新的预处理指令import 
    import优点: 会自动防止重复拷贝

  6. printf和NSLog有什么区别? 
    答: NSLog会自动换行 
    NSLog在输出内容时会附加一些系统信息 
    NSLog和printf接收的参数不一样

  7. Foundation有什么作用? 
    答:Foundation.h我们称之为主头文件, 主头文件中又拷贝了该工具箱中所有工具的头文件, 我们只需要导入主头文件就可以使用该工具箱中所有的工具, 避免了每次使用都要导入一个对应的头文件

面向对象基本概念

  1. 什么是面向对象? 
    答:对象是人们要进行研究的任何事物,从最简单的整数到复杂的飞机等均可看作对象,它不仅能表示具体的事物,还能表示抽象的规则、计划或事件。

  2. 面向对象和面向过程的区别? 
    答: 
    1>面向对象是相对面向过程而言 
    2>面向对象和面向过程都是一种思想 
    面向过程 
    强调的是功能行为 
    关注的是解决问题需要哪些步骤 
    面向对象 
    将功能封装进对象,强调具备了功能的对象 
    关注的是解决问题需要哪些对象

类与对象

  1. 什么是对象? 
    答:对象是人们要进行研究的任何事物,从最简单的整数到复杂的飞机等均可看作对象,它不仅能表示具体的事物,还能表示抽象的规则、计划或事件。

  2. 什么是类? 
    答:具有相同特性(数据元素)和行为(功能)的对象的抽象就是类。因此,对象的抽象是类,类的具体化就是对象,也可以说类的实例是对象,类实际上就是一种数据类型。

  3. 类由几部分组成? 
    答: 类(Class) 一个类由3个部分构成 
    类的名称:类名 
    类的属性:一组包含数据的属性 
    类的方法:允许对属性中包含的数据进行操作的方法

  4. 怎么来定义一个类?书写类的格式 
    答:定义类要分为两部分: 声明和实现

类的声明格式 
@interface 类名 : 父类名(NSObject) 

定义实例变量(成员变量,属性) 

方法的声明

@end

类的实现格式 
@implementation 类名 
方法的具体实现 
@end;

5.OC中属性写在类中哪个位置? 
答:OC类声明中属性只能在写@interface和@end之间的{}中

6.如何通过类创建对象?书写创建对象格式 
答: 创建对象格式: 
类名 *指针名 = [类名 new];

7.实例化对象调用哪个方法?实例化对象做了哪3件事? 
答:实例化对象调用类方法new 
1.给对象分配存储空间 
2.初始化类中的实力变量 
3.返回对象内存地址

8.如何访问对象中的属性? 
答:使用指针访问 
格式: 指针名->_属性名;

OC方法

  1. 方法和函数的区别? 
    答: 
    1)OC中的行为和C语言中的函数一样, 都是用来保存一段特定功能的代码 
    C语言中定义一个函数, 分为声明和实现, 声明写在.h中, 实现写在.c中 
    OC中定义一个方法, 也分为声明和实现, 声明写在@interface中, 实现写在@implementation

  2. C语言的函数分为两种: 内部函数和外部函数 
    OC中的方法也分为两种; 类方法和对象方法

  3. 怎么声明和实现无参无返回值方法的?调用方法的格式?(书写格式) 
    答: 
    方法的声明格式:

  4. (返回值类型)方法名; 
    方法的实现格式:
  5. (返回值类型)方法名{ 

    调用方法格式: 
    [对象名 方法名]; 
    或者 [类名 方法名];

  6. 小括号在OC方法中有什么用法? 
    答:OC方法中的()有特殊的用途, OC方法中的()是用来扩住数据类型的

  7. 有参数方法格式是怎样? 
    带一个参数 
    方法声明 
    ·- (返回值类型)方法名:(参数类型)参数名;

方法实现 
- (返回值类型)方法名:(参数类型)参数名{

}

带多个参数 
方法声明 
- (返回值类型)方法名1:(参数类型)参数名1 方法名2:(参数类型)参数名2; 
- (返回值类型)方法名1:(参数类型)参数名1 and方法名2:(参数类型)参数名 
- (返回值类型)方法名1:(参数类型)参数名1 :(参数类型)参数名2; 
方法实现 
- (返回值类型)方法名1:(参数类型)参数名1 方法名2:(参数类型)参数名2{ 
}

OC类方法

  1. 类方法和对象方法的区别? 
    答: 
    0). 对象方法以-开头 
    类方法以+开头 
    1). 对象方法必须用对象调用 
    类方法必须用类来调用 
    2).对象方法中可以直接访问属性(成员变量) 
    类方法中不可以直接访问属性(成员变量) 
    3). 类方法和对象方法可以进行相互调用(展示代码)

  2. 类方法的应用场景? 
    答: 
    类方法一般用于定义工具方法 
    字符串查找 
    文件操作 
    数据库操作

  3. isa是什么数据类型? 
    答: 类的第0个属性并不是我们编写的_age, 而是一个叫做isa的属性 
    isa是对象中的隐藏指针,指向创建这个对象的类,占8个字节

方法和函数的区别

  1. 函数和方法的区别? 
    答: 
    1).函数属于整个文件, 方法属于某一个类 
    方法如果离开类就不行 
    2).函数可以直接调用, 方法必须用对象或者类来调用 
    注意: 虽然函数属于整个文件, 但是如果把函数写在类的声明中会不识别 
    3).不能把函数当做方法来调用, 也不能把方法当做函数来调用

  2. 方法有哪些的注意点? 
    答:

    方法可以没有声明只有实现 
    方法可以只有声明没有实现, 编译不会报错, 但是运行会报错 
    如果方法只有声明没有实现, 那么运行时会报: 
    reason: ‘+[Person demo]: unrecognized selector sent to class 0x100001140’ 
    发送了一个不能识别的消息, 在Person类中没有+开头的demo方法 
    reason: ‘-[Person test]: unrecognized selector sent to instance 0x100400000’

    常见的错误有哪些? 
    1.只有类的声明,没有类的实现 
    2.漏了@end

  3. @interface和@implementation嵌套 
    4.成员变量没有写在括号里面 
    5.方法的声明写在了大括号里面 
    6.成员变量不能在{}中进行初始化、不能被直接拿出去访问 
    7.方法不能当做函数一样调用 
    8.OC方法只能声明在@interface和@end之间,只能实现在@implementation和@end之间。也就是说OC方法不能独立于类存在 
    9.C函数不属于类,跟类没有联系,C函数只归定义函数的文件所有 
    10.C函数不能访问OC对象的成员 
    11.低级错误:方法有声明,但是实现的时候写成了函数 
    12.OC可以没有@interface同样可以定义一个类

多文件开发

  1. 为什么要使用多文件? 
    答: 
    一个iOS项目可能会有多个人开发,如果多个人同时修改一个文件,那么就很可能会产生冲突,比如这个增加一个方法,那个人把这方法删掉了。另外就是当把多个人写功能合并起来的时候,也非常困难,写到一个文件中,无法顺畅的进行团队合作

  2. OC中如何进行多文件开发? 
    答: 
    在工作中,通常把不同的类放到不同的文件中,每个类的声明和实现分开 
    声明写在.h头文件中, 
    实现写在相应的.m文件中去, 
    类名是什么,文件名就是什么

  3. 使用多文件开发有什么好处? 
    答: 
    显著提高团队协作的效率 
    提高程序的开发速度 
    提高程序的可维护性 
    提高代码的可读性

面向对象特性

  1. :面向对象三大特性有哪些? 
    答:继承性,封装性,多态性

  2. 什么是封装?封装的好处?封装的规范? 
    答: 
    封装: 屏蔽内部实现的细节, 仅仅对外提供共有的方法/接口 
    好处: 保证数据的安全性,将变化隔离 
    规范: 一般情况下不会对外直接暴露成员变量, 都会提供一些共有的方法进行赋值 
    成员变量都需要封装起来

  3. 为什么要进行封装? 
    答:一个类把自己的成员变量暴露给外部的时候,那么该类就失去对该成员变量的管理权,别人可以任意的修改你的成员变量。

geeter-setter方法

  1. 什么是setter方法?sett方法的书写格式? 
    答:setter方法就是给成员变量赋值的方法 
    格式: 
    (1) setter方法一定是对象方法 
    (2) 一定没有返回值 
    (3) 一定以set开头, 并且set后面跟上需要设置的成员变量的名称去掉下划线, 并且首字母大写 
    (4) 一定有参数, 参数类型一定和需要设置的成员变量的类型一致, 并且参数名称就是成员变量的名称去掉下划线

  2. getter方法就是获取成员变量值的方法 
    格式: 
    (1) getter方法一定是对象方法 
    (2)一定有返回值, 而且返回值一定和获取的成员变量的类型一致 
    (3)方法名称就是获取的成员变量的名称去掉下划线 
    (4) 一定没有参数

  3. 成员变量以下划线开头有什么好处? 
    答: 
    1.用于区分局部变量/全局变量/形参 
    2.方便程序编码, 提高编码效率

  4. 解释一下什么是只读属性?什么是只写属性?可读可写的属性?私有属性? 
    答: 
    一个属性可以只有getter方法, 没有setter方法, 这种属性我们称之为只读属性 
    一个属性也可以只有setter方法, 没有getter方法, 这种属性我们称之为只写属性 
    如果既有setter方法又有getter方法, 那么这种属性我们称之为可读可写的属性 
    一个属性也可以没有getter和setter, 这种属性我们称之为私有属性

点语法和self关键字

  1. 什么是点语法?点语法的本质? 
    答:如果给属性提供了getter和setter方法, 那么访问属性就又多了一种访问方式 , 点语法. 
    点语法的本质是调用了一个类的setter和getter方法

  2. 如何使用点语法? 
    答: 
    点语法是一个编译器的特性, 会在程序翻译成二进制的时候将.语法自动转换为setter和getter方法 
    如果点语法在=号的左边, 那么编译器会自动转换为setter方法 
    如果点语法在=号的右边, 或者没有等号, 那么编译器就会自动转换为getter方法

  3. 点语法注意事项? 
    答:点语法一般用于给成员变量赋值, 如果不是给成员变量赋值一般情况下不建议使用, 但是也可以使用

  4. 什么是成员变量?什么是对象方法?什么是类方法? 
    答: 
    成员变量: 
    成员变量是一个实例对象的具体状态特征,并且这些状态特征是可以改变的,如张三的年龄,身高,体重等

对象方法: 
一个实例对象的行为,比如张三具有吃的行为,张三做出这样行为的时候,有可能会影响,自身的某些状态特征,比如张三吃可能会增加张三体重和身高。

类方法: 
类方法是某个类的行为,可以直接通过类名调用;如果在类方法中需要使用某些数据,必须通过参数传入;类方法不能访问成员变量。

  1. 如何使用self? 
    如果self在对象方法中, 那么self就代表调用当前对象方法的那个对象 
    如果self在类方法中, 那么self就代表调用当前类方法的那个类 
    总结: 
    我们只用关注self在哪一个方法中 , 如果在类方法那么就代表当前类, 如果在对象方法那么就代表”当前调用该方法的对象”

  2. self有哪些注意事项? 
    答: 
    (1)self会自动区分类方法和对象方法, 如果在类方法中使用self调用对象方法, 那么会直接报错 
    (2)不能在对象方法或者类方法中利用self调用当前self所在的方法

  3. self的有哪些使用场景? 
    答: 
    可以用于在对象方法之间进行相互调用 
    可以用于在类方法之间进行相互调用 
    可以用于区分成员变量和局部变量同名的情况

继承基本概念

  1. 什么是继承?什么是父类?什么是子类?如何实现继承? 
    答: 
    1)子类获得父类的特性就是继承 
    2)被继承的这个类我们称之为父类/ 超类 
    3)继承了某个类的类我们称之为子类 
    4)在声明子类的时候,在子类名称后面通过:父类名称方式来实现继承 
    @interface 子类名称 : 父类名称

@end类

当B类继承A类, 那么B类就拥有A类所有的属性和方法(类方法/对象方法)

2.什么叫方法重写?重写后是以什么顺序调用方法的? 
答: 
(1)如果子类中有和父类中同名的方法, 那么我们称之为方法重写 
注意: 继承中的方法调用顺序, 如果自己有就调用自己的, 如果自己没有就调用父类的 
“方法的调用顺序, 先自己再父类, 如果父类中没有再爷爷类, 如果爷爷类再没有就找爷爷的爸爸类 
如果一直找到NSObject类都没有找到, 那么就会报错 
reason: ‘-[Iphone signalWithNumber:]: unrecognized selector sent to instance 0x1003043c0’ 
注意:在继承中方法可以重写, 但是属性(成员变量)不能重写

3.方法重写的使用场景? 
答:使用场景:当从父类继承的某个方法不适合子类,可以在子类中重写父类的这个方法。

4.继承的条件是什么? 
答: 
不要以为继承可以提高代码的复用性, 以后但凡发现多个类当中有重复代码就抽取一个父类 
只要满足一定的条件我们才能使用继承 
条件: XXXX 是 XXX / 某某某 is a 某某某

5.继承的优点是什么? 
答: 
提高代码的复用性 
可以让类与类之间产生关系, 正是因为继承让类与类之间产生了关系所以才有了多态

6.继承的缺点是什么? 
答: 
耦合性太强(依赖性太强)

super关键字

  1. 什么是super? 
    super是个编译器的指令符号,只是告诉编译器在执行的时候,去调谁的方法.

  2. 怎么使用super? 
    答: 
    super在类方法中, 一定会调用父类的类方法 
    super在对象方法中, 一定会调用父类的对象方法 
    可以利用super在任意方法中调用父类中的方法

  3. super使用场景? 
    答: 
    子类重写父类的方法时想保留父类的一些行为

多态

  1. 什么是多态?程序中是怎么体现多态的? 
    答: 
    多态就是某一类事物的多种形态 
    在程序中如何表现: 
    父类指针指向子类对象

  2. 多态的条件是什么? 
    答: 
    1)有继承关系 
    2)子类重写父类方法 
    3)父类指针指向子类对象

  3. 多态的优点是什么? 
    答:提高了代码的扩展性,复用性

  4. 多态的注意点? 
    答:如果父类指针指向子类对象, 需要调用子类特有的方法, 必须先强制类型转换为子类才能调用

description方法

  1. 使用%@了打印一个对象,输出的是什么内容?%@的原理是什么? 
    答:%@是用来打印对象的, description方法默认返回对象的描述信息(默认实现是返回类名和对象的内存地址). 
    其实%@的本质是用于打印字符串. 
    只要利用%@打印某个对象, 系统内部默认就会调用父类的description方法 
    调用该方法, 该方法会返回一个字符串, 字符串的默认格式 <类的名称: 对象的地址>

  2. 重写description方法注意点? 
    答:如果在description方法中利用%@输出self会造成死循环 
    建议: 在description方法中尽量不要使用self来获取成员变量 
    因为如果你经常在description方法中使用self, 可能已不小心就写成了 %@, self

私有变量和私有方法

  1. 什么是私有变量?什么是私有方法? 
    答: 
    实例变量(成员变量)既可以在@interface中定义, 也可以在@implementation中定义 
    私有变量: 
    写在@implementation中的成员变量, 默认就是私有的成员变量, 并且和利用@private修饰的不太一样, 在@implementation中定义的成员变量在其它类中无法查看, 也无法访问

私有方法: 
在@implementation中定义的私有变量只能在本类中访问

property和synthesize基本使用

  1. @porperty是一个编译器指令 
    在Xocde4.4之前, 可以使用@porperty来代替getter/setter方法的声明 
    也就是说我们只需要写上@porperty就不用写getter/setter方法的声明

  2. 编译器只要看到@property,就知道我们要生成某一个属性的 
    getter/setter方法的声明

  3. @propertyde格式? 
    答:@property 数据类型 变量名;

  4. @synthesize是什么指令?作用是什么? 
    答: 
    synthesize是一个编译器指令, 它可以简化我们getter/setter方法的实现

  5. @synthesize age = _age; 在给age赋值时,编译器做了哪些事? 
    @synthesize age = _age; 
    (1)在@synthesize后面的age,告诉编译器, 需要实现哪个@property生成的声明 
    (2)告诉@synthesize, 需要将传入的值赋值给谁和返回谁的值给调用者 
    如果在@synthesize后面没有告诉系统将传入的值赋值给谁, 系统默认会赋值给和@synthesize后面写得名称相同的成员变量 
    @synthesize age;

  6. property增强做了哪些事? 
    答: 
    (1)从Xcode4.4以后,对@property进行了增强, 以后只要利用一个@property就可以同时生成setter/getter方法的声明和实现

(2)如果没有告诉@property要将传入的参数赋值给谁, 默认@property会将传入的属性赋值给_开头的成员变量

7.@property的使用场景? 
答: 
如果不想对传入的数据进行过滤, 仅仅是提供方法给外界操作成员变量, 那么就可以使用@property,并且系统会自动给我们生成一个_开头的成员变量

8.使用property增强后,什么时候要重写getter/setter方法? 
答:使用property增强,只会生成最简单的getter/setter方法的声明和实现, 并不会对传入的数据进行过滤 
如果想对传入的数据进行过滤, 那么我们就必须重写getter/setter方法

9.重写getter/setter方法有哪些注意点? 
答: 
如果重写了setter方法, 那么property就只会生成getter方法 
如果重写了getter方法, 那么property就只会生成setter方法 
如果同时重写了getter/setter方法, 那么property就不会自动帮我们生成私有的成员变量

property修饰符

  1. 增强@property使用修饰符后的的格式是什么? 
    答: 
    格式: 
    @property(属性修饰符) 数据类型 变量名称;

  2. @property 有哪些修饰符?各有什么作用? 
    答:readwrite: 代表既生成getter方法 , 也生成setter方法 
    默认情况下 @property就是readwrite的 
    @property(readwrite) int age; 
    ‘readonly: 代表只生成getter方法不生成setter方法’ 
    可以给setter方法起别名@property(setter=tiZhong:) double weight; 
    可以给getter方法起别名@property(getter=isMarried) BOOL married;

静态数据类型和动态数据类型

  1. 静态数据类型的特点: 
    在编译时就知道变量的类型, 
    知道变量中有哪些属性和方法 
    在编译的时候就可以访问这些属性和方法, 
    并且如果是通过静态数据类型定义变量, 如果访问了不属于静态数据类型的属性和方法, 那么编译器就会报错

  2. 动态数据类型的特点: 
    在编译的时候编译器并不知道变量的真实类型, 只有在运行的时候才知道它的真实类型 
    并且如果通过动态数据类型定义变量, 如果访问了不属于动态数据类型的属性和方法, 编译器不会报错

  3. id和NSObject * 的区别? 
    答: 
    NSObject *是一个静态数据类型 
    id 是一个动态数据类型

  4. 动态数据类型的应用场景? 
    答:动态类型主要用在多态, 可以减少代码量, 避免调用子类特有的方法需要强制类型转换

  5. 动态数据类型的弊端是什么? 
    答:由于动态数据类型可以调用任意方法, 所以有可能调用到不属于自己的方法, 而编译时又不会报错, 所以可能导致运行时的错误

  6. 判断数据类型的有哪些方法?(变量 修改为 对象) 
    答:为了避免动态数据类型引发的运行时的错误, 一般情况下如果使用动态数据类型保存一个对象, 在调用这个变量的方法之前会进行一次判断, 判断当前对象是否能够调用这个方法

构造方法

  1. 什么是构造方法? 
    答: 
    在OC中init开头的方法, 我们称之为构造方法

  2. 构造方法的用途? 
    答: 
    构造方法的用途: 用于初始化一个对象, 让某个对象一创建出来就拥有某些属性和值

  3. 如何实现构造方法? 
    答: 
    重写init方法, 在init方法中初始化成员变量

  4. 如何重写init方法? 
    答:重写init方法必须按照苹果规定的格式重写, 如果不按照规定会引发一些未知的错误 
    (1)必须先初始化父类, 再初始化子类 
    (2)必须判断父类是否初始化成功, 只有父类初始化成功才能继续初始化子类 
    (3)返回当前对象的地址

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">- (instancetype)init{    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化父类</span>    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 只要父类初始化成功 , 就会返回对应的地址, 如果初始化失败, 就会返回nil</span>    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// nil == 0 == 假 == 没有初始化成功</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> init];    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 判断父类是否初始化成功</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> != <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>) {      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 初始化子类</span>      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置属性的值</span>        _age = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>;    }    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 返回地址</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>;}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>

自定义构造方法

  1. 什么是自定义构造方法?为什么要自定义构造方法? 
    (1)自定义构造方法就是自定义一个init方法 
    (2)有时候我们需要在创建某个对象的时候,让对象的某些属性就具有值,这时候就需要传入一些参数给对象的属性,为了满足这个需求,就需要自定义构造方法

  2. 自定义构造方法的格式? 
    答: 
    (1)一定是对象方法 
    (2)一定返回id/instancetype 
    (3)方法名称一定以init开头 
    -(instancetype)initWithAge:(int)age;

  3. 自定义构造方法在继承中有一个原则? 
    答:自己的事情自己做,属于谁的属性就由谁来进行操作 
    父类的属性交给父类的方法来处理,子类的方法处理子类自己独有的属性

  4. 自定义构造方法在子类,如何调用的父类构造方法的? 
    答:子类在重写自定构造方法时,一般使用super 调用父类的构造方法,先让父类将父类的属性进行初始化

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">- (instancetype)initWithAge:(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>)age andName:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)name andNo:(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>)no{    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> initWithAge:age andName:name]) {        _no = no;    }    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>;}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

instancetype和id区别

  1. instancetype和id区别? 
    答: 
    (1)id在编译的时候不能判断对象的真实类型 
    instancetype在编译的时候可以判断对象的真实类型

    (2)如果init方法的返回值是instancetype, 那么将返回值赋值给一个其它的对象会报一个警告 
    如果是在以前, init的返回值是id, 那么将init返回的对象地址赋值给其它对象是不会报错的

    (3)id可以用来定义变量, 可以作为返回值, 可以作为形参 
    instancetype只能用于作为返回值

  2. instancetype 应用场景? 
    答:以后但凡自定义构造方法, 返回值尽量使用instancetype, 不要使用id

类工厂方法

  1. 什么是类工厂方法? 
    答:用于快速创建对象的类方法, 我们称之为类工厂方法

  2. 类工厂方法应用场景? 
    答:类工厂方法中主要用于 给对象分配存储空间和初始化这块存储空间

  3. 类工厂方法使用规范? 
    答:规范: 
    1.一定是类方法 + 
    2.方法名称以类的名称开头, 首字母小写 
    3.一定有返回值, 返回值是id/instancetype 
    4.在类工厂方法实现中,调用本类的构造方法,创建实例对象,并返回实例对象

自定义类工厂方法是苹果的一个规范, 一般情况下, 我们会给一个类提供自定义构造方法和自定义类工厂方法用于创建一个对象。

类工厂方法在继承中的注意点 
以后但凡自定义类工厂方法, 在类工厂方法中创建对象一定要使用self来创建,一定不要使用类名来创建。

类的本质及存储细节

  1. 类的本质是什么? 
    答: 
    (1)类其实也是一个对象, 这个对象会在这个类第一次被使用的时候创建 
    (2)只要有了类对象, 将来就可以通过类对象来创建实例对象 
    (3)实例对象中有一个isa指针, 指向创建自己的类对象 
    (4)类对象中保存了当前对象所有的对象方法 
    (5)当给一个实例对象发送消息的时候, 会根据实例对象中的isa指针去对应的类对象中查找 
    (6)所有类对象的继承关系就是元类对象的继承关系

类的启动过程

  1. 1.load方法 
    “load方法调用时间:” 
    只要程序启动就会将所有类的代码加载到内存中, 放到代码区 
    “调用次数” 
    load方法会在当前类被加载到内存的时候调用, 有且仅会调用一次

  2. 2.initialize方法 
    “initialize方法调用时间:” 
    当当前类第一次被使用的时候就会调用(创建类对象的时候) 
    “调用次数”

SEL类型

  1. SEL是什么类型? 
    答: 
    SEL类型代表着方法的签名,在类对象的方法列表中存储着该签名与方法代码的对应关系

  2. SEL有什么作用? 
    答: 
    (1)SEL类型的第一个作用, 配合对象/类来检查对象/类中有没有实现某一个方法 
    (2)SEL类型的第二个作用, 配合对象/类来调用某一个SEL方法 
    (3)配合对象将SEL类型作为方法的形参

  3. 哪个方法是用来检验对象是否实现了某个方法? 
    判断实例是否实现某个对象方法 
    (BOOL)respondsToSelector: (SEL)selector 
    判断类是否实现某个类方法 
    (BOOL)instancesRespondToSelector:(SEL)aSelector;

  4. 哪些方法是用来调用对象中SEL类型对应的方法? 
    答: 
    让对象执行某个方法

<code class="hljs erlang has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-pp" style="box-sizing: border-box;">- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id)</span>performSelector:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">SEL</span>)</span>aSelector;- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id)</span>performSelector:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">SEL</span>)</span>aSelector withObject:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id)</span>object;- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id)</span>performSelector:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">SEL</span>)</span>aSelector withObject:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id)</span>object1 withObject:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(id)</span>object2;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

内存管理

  1. 什么是堆?什么是栈? 
    答: 
    栈(操作系统):由操作系统自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈(先进后出); 
    堆(操作系统):一般由程序员分配释放,若程序员不释放,程序结束时可能由系统回收,分配方式类似于链表。

  2. 什么是内存管理? 
    答: 
    所谓内存管理, 就是对内存进行管理, 涉及的操作有 
    分配内存 : 比如创建一个对象, 会增加内存占用 
    清除内存 : 比如销毁一个对象, 能减小内存占用

  3. 内存管理的本质是什么? 
    答: 
    OC对象存放于堆里面 
    非OC对象一般放在栈里面(栈内存会被系统自动回收)

MRC内存管理

  1. 什么是引用计数器? 
    答:每个OC对象都有自己的引用计数器,它是一个整数,表示有多少人正在用这个对象

  2. 引用计数器的作用? 
    答: 
    (1)当使用alloc、new或者copy创建一个对象时,对象的引用计数器默认就是1 
    (2)当对象的引用计数器为0时,对象占用的内存就会被系统回收 
    如果对象的计数器不为0,那么在整个程序运行过程,它占用的内存就不可能被回收(除非整个程序已经退出 )

  3. 怎么操作引用计数器? 
    答: 
    给对象发送一条retain消息,可以使引用计数器值+1(retain方法返回对象本身) 
    给对象发送一条release消息, 可以使引用计数器值-1 
    给对象发送retainCount消息, 可以获得当前的引用计数器值 
    需要注意的是: release并不代表销毁\回收对象, 仅仅是计数器-1

  4. dealloc 方法的作用? 
    答:对象即将被销毁时系统会自动给对象发送一条dealloc消息 (因此, 从dealloc方法有没有被调用,就可以判断出对象是否被销毁)

  5. 重写dealloc方法有什么注意点? 
    答:重写dealloc方法, [super dealloc]一定要写到所有代码的最后

  6. 内存管理的原则? 
    答: 
    (1)谁创建谁release : 
    如果你通过alloc、new、copy或mutableCopy来创建一个对象,那么你必须调用release或autorelease

(2)谁retain谁release: 
只要你调用了retain,就必须调用一次release

(3)总结: 
有加就有减,曾经让对象的计数器+1,就必须在最后让对象的计数器-1

setter方法的内存管理 
实现set方法内存管理有哪几步? 
答: 
(1)retain需要使用的对象 
(2)release之前的对象 
(3)只有传入的对象和之前的不同才需要release和retain

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)setRoom:(Room *)room{    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 只有房间不同才需用release和retain</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (_room != room) {    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 0ffe1 != 0ffe1</span>        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 2.将以前的房间释放掉 -1</span>        [_room release];        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// retain不仅仅会对引用计数器+1, 而且还会返回当前对象</span>        _room = [room retain];    }}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

property修饰符 
1.readonly: 只会生成getter方法 
readwrite: 既会生成getter也会生成setter, 默认什么都不写就是readwrite

2.getter: 可以给生成的getter方法起一个名称 
setter: 可以给生成的setter方法起一个名称

3.retain: 就会自动帮我们生成getter/setter方法内存管理的代码 
assign: 不会帮我们生成set方法内存管理的代码, 仅仅只会生成普通的getter/setter方法, 默认什么都不写就是assign

4.多线程 
atomic :性能低(默认) 
nonatomic :性能高 
在iOS开发中99.99%都是写nonatomic

property修饰符 有拿几点需要注意的问题? 
答: 
1.相同类型的property修饰符不能同时使用 
2.不同类型的property修饰符可以多个结合在一起使用, 多个之间用,号隔开 
3.iOS开发中只要写上property, 那么就立刻写上nonatomic

什么是循环retain? 
答: 
如果A对用要拥有B对象, 而B对应又要拥有A对象, 此时会形成循环retain 
解决循环retain的方法,一边用retain一边用assign

autorelease 自动释放池

  1. 什么是自动释放池? 
    答: 
    autorelease是一种支持引用计数的内存管理方式,只要给对象发送一条autorelease消息,会将对象放到一个自动释放池中,当自动释放池被销毁时,会对池子里面的所有对象做一次release操作

  2. 自动释放池的优点是什么? 
    答: 
    不用再关心对象释放的时间 
    不用再关心什么时候调用release

  3. 简述自动释放池的原理? 
    答: 
    autorelease实际上只是把对release的调用延迟了,对于每一个autorelease,系统只是把该 Object放入了当前的autorelease pool中,当该pool被释放时,该pool中的所有Object会被调用Release。

  4. 自动释放池有哪些注意事项? 
    答: 
    (1)在自动释放池中创建了对象, 一定要调用autorelease,才会将对象放入自动释放池中 
    (2)一个程序中可以创建N个自动释放池, 并且自动释放池还可以嵌套 
    (3)不要再自动释放池中使用比较消耗内存的对象, 占用内存比较大的对象 
    (4)尽量不要再自动释放池中使用循环, 特别是循环的次数非常多, 并且还非常占用内存 
    (5)千万不要写多次autorelease 
    (6)一个alloc/new对应一个autorelease或者release

  5. 自动释放池是以什么形式存储的? 
    答: 如果存在多个自动释放池的时候, 自动释放池是以 “栈” 的形式存储在堆区 
    栈的特点: 先进后出

ARC内存管理

  1. 问题1:ARC的原理是什么? 
    答:当ARC开启时,编译器将自动在代码合适的地方插入retain, release和autorelease,而作为程序猿,完全不需要担心编译器会做错(除非开发者自己错用ARC了)。

  2. ARC有什么优点? 
    答: 
    1.完全消除了手动管理内存的烦琐, 让程序猿更加专注于app的业务 
    2.基本上能够避免内存泄露 
    3.有时还能更加快速,因为编译器还可以执行某些优化

  3. ARC的原则是什么?什么是强指针?什么是弱指针? 
    答:只要还有一个强指针变量指向对象,对象就会保持在内存中 
    (1)强指针 
    默认所有指针变量都是强指针 
    被__strong修饰的指针 
    (2)弱指针 
    被__weak修饰的指针

  4. ARC下@property修饰符有哪些? 
    答: 
    strong : 用于OC对象, 相当于MRC中的retain 
    weak : 用于OC对象, 相当于MRC中的assign 
    assign : 用于基本数据类型, 跟MRC中的assign一样

  5. ARC中是怎么对对象进行内存管理的? 
    答: 
    (1)ARC下单对象内存管理 
    (2)ARC下,所有的指针都是强指针 
    (3)ARC, A对象想拥有B对象, 那么就需要用一个强指针指向B对象 
    (4)A对象不用B对象了, 什么都不需要做, 编译器会自动帮我们做

  6. ARC怎么解决循环引用问题? 
    答: 
    ARC和MRC一样, 如果A拥有B, B也拥有A, 那么必须一方使用弱指针 
    也就是说 一端用strong ,一端用weak

Category 分类

  1. 书写Category的格式? 
    答: 
    // 分类的声明 
    @interface ClassName (CategoryName) 
    NewMethod; //在类别中添加方法 
    //不允许在类别中添加变量 
    @end

ClassName: 需要给哪个类扩充方法 
CategoryName: 分类的名称 
NewMethod: 扩充的方法

// 分类的实现 
@implementation ClassName(CategoryName)

NewMethod 
… … 
@end

ClassName: 需要给哪个类扩充方法 
CategoryName: 分类的名称 
NewMethod: 扩充的方法

  1. Category的作用? 
    答: 
    (1)在不改变原来的类内容的基础上,为类增加一些方法。 
    (2)一个庞大的类可以分模块开发,由多个人来编写,更有利于团队合作

  2. 分类,原来类或者父类中的方法调用的顺序? 
    答:先调用分类中的方法(最后参与编译的分类优先),再调用原来类中的方法,最后掉用父类中的方法

Extension 匿名扩展

  1. 什么是类扩展? 
    答:延展类别又称为扩展(Extension),Extension是Category的一个特例

  2. 类扩展格式? 
    答: 
    类扩展书写格式 
    @interface 类名 () 
    @end

  3. 类扩展的作用是什么? 
    答:写在.m文件中,可以为某个类扩充一些私有的成员变量和方法

Block

  1. 什么是Block? 
    答:Block是iOS中一种比较特殊的数据类型,用来保存某一段代码

  2. Block的作用? 
    答:Block用来保存某一段代码, 可以在恰当的时间再取出来调用 
    功能类似于函数和方法

  3. Block的格式? 
    答:Block的格式: 
    返回值类型 (^block变量名)(形参列表) = ^(形参列表) { 
    };

协议

  1. 什么是协议? 
    答:其他语言有接口的概念,接口就是一堆方法的声明没有实现. 
    OC中没有接口的概念,OC中的接口就是协议. 
    协议Protocol是由一系列的方法声明组成的

  2. 书写协议的格式? 
    答: 
    格式: 
    @protocol 协议名称 
    // 方法声明列表 
    @end

  3. 一个类怎么遵循协议? 
    答:类遵守协议格式: 
    @interface 类名 : 父类 <协议名称1, 协议名称2,…> 
    @end

注意: 
(1)一个类可以遵守1个或多个协议 
(2)任何类只要遵守了Protocol,就相当于拥有了Protocol的所有方法声明

4.协议和继承有什么区别? 
答: 
(1)继承之后默认就有实现, 而protocol只有声明没有实现 
(2)相同类型的类可以使用继承, 但是不同类型的类只能使用protocol 
(3)protocol可以用于存储方法的声明, 可以将多个类中共同的方法抽取出来, 以后让这些类遵守协议即可

  1. 什么是基协议? 
    答:基协议:是基协议,是最根本最基本的协议,其中声明了很多最基本的方法。 
    注意:建议每个新的协议都要遵守NSObject协议

6.协议有哪些注意事项? 
答: 
(1)协议只能声明方法, 不能声明属性 
(2)父类遵守了某个协议, 那么子类也会自动遵守这个协议 
(3)在OC中一个类可以遵守1个或多个协议 
注意: OC中的类只能有一个父类, 也就是说OC只有单继承 
(4)OC中的协议又可以遵守其它协议, 只要一个协议遵守了其它协议, 那么这个协议中就会自动包含其它协议的声明

7.协议中控制方法的能否实现的关键字是什么?各有什么作用? 
(1)注意: 如果没有使用任何关键字修饰协议中的方法, 那么该方法默认就是required的 
(2)注意:@required和@optional仅仅使用程序员之间交流, 并不能严格的控制某一个遵守该协议的类必须要实现该方法, 因为即便不是实现也不会报错, 只会报一个警告 
(3) 
@required 
如果协议中的方法是@required的, 要求遵守协议的类实现@required所修饰的方法,如果没有实现该方法, 那么会报一个警告 
(4) 
@optional 
如果协议中的方法是@optional的, 遵守协议的类可选择实现@optional所修饰的方法,如果没有实现该方法, 那么不会报警告

类型限定

  1. 什么是类型限定? 
    答:类型限定就是限定一个类必须遵守某个协议

  2. 类型限定的格式? 
    答:数据类型<协议名称> 变量名 
    @property (nonatomic, strong) Wife *wife;

  3. 类型限定有什么注意点? 
    答: 
    (1)类型限定是写在数据类型的右边的 
    (2)虽然在接受某一个对象的时候, 对这个对象进行了类型限定(限定它必须实现某个协议), 
    但是并不意味着这个对象就真正的实现了该方法. 所以每次在调用对象的协议方法时应该进行一次验证

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.wife <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">respondsToSelector:</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@selector</span>(cooking)]) {    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.wife cooking];}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

代理设计模式

  1. 代理模式的应用场景? 
    (1)当A对象想监听B对象的一些变化时, 可以使用代理设计模式 
    (2)当B对象发生一些事情, 想通知A对象的时候, 可以使用代理设计模式 
    (3)当对象A无法处理某些行为的时候,想让对象B帮忙处理(让对象B成为对象A的代理对象)

  2. 用什么类型来接收遵守协议的代理对象? 
    答:使用id类型接收代理对象

  3. 简述一下协议的编写规范? 
    答: 
    (1)一般情况下, 当前协议属于谁, 我们就将协议定义到谁的头文件中 
    (2)协议的名称一般以它属于的那个类的类名开头, 后面跟上protocol或者delegate 
    (3)协议中的方法名称一般以协议的名称protocol之前的作为开头 
    (4)一般情况下协议中的方法会将触发该协议的对象传递出去

5.一般情况下一个类中的代理属于的名称叫做 delegate

6.当某一个类要成为另外一个类的代理的时候, 
一般情况下在.h中用@protocol 协议名称;告诉当前类 这是一个协议. 
在.m中用#import真正的导入一个协议的声明

Foundation

  1. 什么是框架? 
    答: 
    众多功能\API的集合. 
    框架是由许多类、方法、函数、文档按照一定的逻辑组织起来的集合,以便使研发程序变得更容易,在OS X下的Mac操作系统中大约有80个框架为所有程序开发奠定基础的框架称为Foundation 框架

  2. Foundation 框架有什么作用? 
    答: 
    1.Foundation框架是Mac\iOS中其他框架的基础 
    2.Foundation框架包含了很多开发中常用的数据类型:结构体,枚举, 类

  3. 什么是NSString? 
    答:一个NSString对象就代表一个字符串(文字内容) 
    一般称NSString为字符串类

  4. 如何创建NSString对象?有几种方法创建一个NSString字符串? 
    答: 
    (1)通过@”“直接创建 
    如果通过@”“创建字符串, 那么会将字符串放到常量区中 
    如果是字符串常量, 那么只要内容相同 , 不会重复创建

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str1 = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

(2)通过alloc或者类工厂方法创建 
如果是通过alloc或者类工厂方法创建, 那么会将字符串放到堆区中

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str2 = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>];    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str3 = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"jack"</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
  1. 如何将字符串写入到文件中?
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"iOS"</span>;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *path2 = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/abc.txt"</span>;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> flag = [str writeToFile:path2 atomically:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span> encoding:NSUTF8StringEncoding error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"flag = %i"</span>, flag);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

6.什么是URL? 
答: 
(1)URL的全称是Uniform Resource Locator(统一资源定位符) 
(2)URL是互联网上标准资源的地址 
(3)互联网上的每个资源都有一个唯一的URL,它包含的信息指出资源的位置 
(4)根据一个URL就能找到唯一的一个资源

7.写URL格式? 
答: URL = 协议头://主机地址/路径

8.如何创建URL 
答: 
(1)通过alloc 或者类工厂方法创建

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> *url = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> URLWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"file:///Users/james/Desktop/str.txt"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> *url = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> alloc] initWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"file:///Users/james/Desktop/str.txt"</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

(2)通过文件路径创建(默认就是file协议的)

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> *url = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> fileURLWithPath:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/str.txt"</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

9.如何获取本地路径的信息?获取本地路径信息的方法有什么注意点? 
答:获取本地路径信息–fileURLWithPath 
方法一: 
(1)字符串保存路径

<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">NSString <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">*path</span> = <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@"</span><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">file</span>:<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//192.168.13.10/Users/james/Desktop/note/ja.txt";</span>    NSLog(<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@"</span>url编码前: <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">%@</span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">", path);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

(2)将路径中中文转换为UTF-8编码格式

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"url编码后: %@"</span>, path);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

方法二: 
(1)字符串保存路径,如果访问本机的文件, 可以省略主机地址 
NSString *path = @”file:///Users/james/Desktop/note/ja.txt”; 
NSLog(@”url编码前: %@”, path);

(2)将路径中中文转换为UTF-8编码格式 
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSLog(@”url编码后: %@”, path); 
NSURL *url = [NSURL URLWithString:path];

10.获取本地路径的信息

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> *url = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> fileURLWithPath:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/note/ja.txt"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSError</span> *error = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (error == <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>) {<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"str = %@"</span>, str);    }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"error = %@"</span>, [error localizedDescription]);    }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

注意点: 
URLWithString: 方法不支持中文,所以无法成功创建URL,必须先对路径字符串进行编码

fileURLWithPath: 方法支持中文,并且省略协议头,但是只能获取本地方法

11.如何获取网络路径的信息? 
答: 
获取网络路径的信息–URLWithString

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> *url = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> URLWithString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http://www.baidu.com"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"str = %@"</span>, str);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

12.如何将信息写入到指定文件? 
答: 
方法一:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *path = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"file:///Users/james/Desktop/未命名文件夹/abc.txt"</span>;path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> *url = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> URLWithString:path]; [str writeToURL:url atomically:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span> encoding:NSUTF8StringEncoding error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

方法二:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *path = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/未命名文件夹/abc.txt"</span>;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> *url = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSURL</span> fileURLWithPath:path];[str writeToURL:url atomically:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span> encoding:NSUTF8StringEncoding error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

总结: 
1.如果多次往同一个文件中写入内容,那么后一次的会覆盖前一次的 \ 
2.方法名中没有file,路径中要添加上file协议头,如果方法名中有file,路径中就不需要file协议头

NSString

  1. 如何比较两个字符串的”内容”是否相同? 
    答: 
    BOOL flag = [str1 isEqualToString:str2]; 
    NSLog(@”flag = %i”, flag);

  2. 如何比较两个字符串的”地址”是否相同? 
    flag = (str1 == str2); 
    NSLog(@”flag = %i”, flag);

  3. 如何比较字符串的大小? 
    答:使用方法compare: 
    NSOrderedAscending 前面的小于后面的 
    NSOrderedSame, 两个字符串相等 
    NSOrderedDescending 前面的大于后面的

  4. 如何忽略大小写进行比较? 
    [str1 caseInsensitiveCompare:str2];

  5. 如何判断字符串是否以什么结尾的?本质是什么? 
    答: 
    本质就是从字符串的最后一个字符开始匹配, 只要不匹配就返回NO

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([str hasSuffix:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">".gif"</span>]) {    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"动态图片"</span>);}<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"不是动态图片"</span>);}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

6.如何获取指定范围内的字符串? 
答: 
(1)动态获取截取的起始位置 
NSUInteger location = [str rangeOfString:@”>”].location + 1; 
(2)动态获取截取的长度

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">NSUInteger length = [str rangeOfString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"</"</span>]<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.location</span> - location;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSRange</span> range = NSMakeRange(location, length);<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *newStr = [str substringWithRange:range];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"str = %@"</span>, str);<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"newStr = %@"</span>, newStr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

7.如何替换字符串中的部分字符? 
答: 
需求: 将&符号替换为/

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"http:&&www.weibo.iosjames.com&img&james.gif"</span>;.<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.OccurrencesOfString</span>: 要替换谁<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// withString: 用谁替换</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *newStr = [str stringByReplacingOccurrencesOfString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"&"</span> withString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"newStr = %@"</span>, newStr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

8.如何对字符串首位进行处理? 
答:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"HTTP://www.baidu.com/img/james.GIF"</span>;NSCharacterSet *set = [NSCharacterSet uppercaseLetterCharacterSet];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *newStr = [str stringByTrimmingCharactersInSet:set];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"newStr = |%@|"</span>, newStr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

9.如何给文件路径添加一个目录? 
答: 
本质就是在字符串的末尾加上一个/ 和指定的内容 
注意: 如果路径后面已经有了/, 那么就不会添加了 
如果路径后面有多个/, 那么会自动删除多余的/, 只保留一个

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *newStr = [str stringByAppendingPathComponent:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, newStr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

10.如何获取路径中文件的扩展名? 
答: 
本质就是从字符串的末尾开始查找., 截取第一个.后面的内容

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *newStr = [str pathExtension];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, newStr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

11.如何删除路径中文件的扩展名? 
答: 
本质就是从字符串的末尾开始查找.,删除第一个.和.后面的内容

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *newStr = [str stringByDeletingPathExtension];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, newStr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

12.如何给文件路径添加一个扩展名? 
答: 
本质就是在字符串的末尾加上一个.和指定的内容

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *newStr = [str stringByAppendingPathExtension:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"jpg"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, newStr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

13.如何将将字符串转换为大写? 
答:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *newStr = [str uppercaseString];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, newStr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

14.如何将字符串转换为小写? 
答:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *newStr2 = [newStr lowercaseString];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, newStr2);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

15.如何将字符串的首字符转换为大写 
答:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *newStr = [str capitalizedString];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, newStr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

NSMutalbleString

  1. NSMutableString和NSString的区别? 
    答: 
    (1)NSString是不可变的, 里面的文字内容是不能进行修改的 
    (2)NSMutableString是可变的, 里面的文字内容可以随时更改 
    (3)NSMutableString能使用NSString的所有方法

  2. 什么是可变字符串?什么是不可变字符串? 
    答: 
    不可变字符串:指的是字符串在内存中占用的存储空间固定,并且存储的内容不能发生变化 
    可变字符串:指的是字符串在内存中占用的存储空间可以不固定,并且存储的内容可以被修改

  3. 在字符串后面添加一段字符串? 
    答:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[strM appendString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/image"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"strM = %@"</span>, strM);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

4.如何删除字符串中的字符? 
技巧: 在开发中, 我们经常利用rangeOfString和deleteCharactersInRange方法配合起来删除指定的字符串

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSRange</span> range = [strM rangeOfString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"xx"</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
<code class="hljs scss has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[strM deleteCharactersInRange:range]</span>;<span class="hljs-function" style="box-sizing: border-box;">NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"strM = %@"</span>, strM)</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

5.如何在某个字符前面插入love这个单词? 
答: 
insertString : 需要插入的字符串 
atIndex: 从哪里开始插入

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSRange</span> range = [strM rangeOfString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"xx"</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[strM insertString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"love"</span> atIndex:range<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.location</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"strM = %@"</span>, strM);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

NSArray

  1. 什么是NSArray? 
    答:NSArray是OC中的数组类,开发中建议尽量使用NSArray替代C语言中的数组

  2. NSArray有哪些使用注意? 
    答: 
    (1)只能存放任意OC对象, 并且是有顺序的 
    (2)不能存储非OC对象, 比如int\float\double\char\enum\struct等 
    (3)它是不可变的,一旦初始化完毕后,它里面的内容就永远是固定的, 不能删除里面的元素, 也不能再往里面添加元素 
    (4)NSArray使用NSLog()打印,输出的是小括号的格式。 
    (5)NSArray中不能存储nil,因为NSArray认为nil是数组的结束(nil是数组元素结束的标记)。nil就是0。0也是基本数据类型,不能存放到NSArray中。

  3. NSArray有哪些常用方法? 
    答:

<code class="hljs haml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">-<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">NSUInteger</span>)count; 获取集合元素个数</span>-<span class="ruby" style="box-sizing: border-box;"> (id)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">objectAtIndex:</span>(<span class="hljs-constant" style="box-sizing: border-box;">NSUInteger</span>)index; 获得index位置的元素</span>-<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">BOOL</span>)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">containsObject:</span>(id)anObject; 是否包含某一个元素</span>-<span class="ruby" style="box-sizing: border-box;"> (id)lastObject; 返回最后一个元素</span>-<span class="ruby" style="box-sizing: border-box;"> (id)firstObject; 返回最后一个元素</span>-<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">NSUInteger</span>)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">indexOfObject:</span>(id)anObject; 查找anObject元素在数组中的位置(如果找不到,返回-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>)</span>-<span class="ruby" style="box-sizing: border-box;"> (<span class="hljs-constant" style="box-sizing: border-box;">NSUInteger</span>)<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">indexOfObject:</span>(id)anObject <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">inRange:</span>(<span class="hljs-constant" style="box-sizing: border-box;">NSRange</span>)range;</span>在range范围内查找anObject元素在数组中的位置</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

4.书写NSArray简写形式? 
答:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *arr = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithObjects:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ldda"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"jjj"</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *arr = @[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"jasc"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"jjj"</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

获取数组元素的简写

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">NSLog</span>(<span class="hljs-at_rule" style="box-sizing: border-box;">@<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">"%@",</span> [arr objectAtIndex:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>])</span>;<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">NSLog</span>(<span class="hljs-at_rule" style="box-sizing: border-box;">@<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">"%@",</span> arr[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>])</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

5.如何使用增强for循环,遍历NSArray数组? 
答: 
逐个取出arr中的元素, 将取出的元素赋值给obj 
注意: obj的类型可以根据数组中元素的类型来写, 不一定要写NSObject 
for (NSString *obj in arr) { 
NSLog(@”obj = %@”, obj); 
}

6.如何使用OC数组的迭代器来遍历数组? 
答: 
每取出一个元素就会调用一次block 
每次调用block都会将当前取出的元素和元素对应的索引传递给我们 
obj就是当前取出的元素, idx就是当前元素对应的索引 
stop用于控制什么时候停止遍历

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[arr enumerateObjectsUsingBlock:^(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> obj, NSUInteger idx, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> *stop) {    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (idx == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>) {        *stop = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>;    }    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"obj = %@, idx = %lu"</span>, obj, idx);}];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

7.如何对数据进行排序?NSArray *arr = @[@10, @20, @5, @7, @15]; 
NSLog(@”排序前: %@”, arr);意:** 
想使用compare方法对数组中的元素进行排序, 那么数组中的元素必须是Foundation框架中的对象, 也就是说不能是自定义对象

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *newArr = [arr sortedArrayUsingSelector:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@selector</span>(compare:)];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"排序后: %@"</span>, newArr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

8.如何将数组写入文件中? 
答:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *arr = @[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eee"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"aaa"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"jjj"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> flag = [arr writeToFile:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/abc.plist"</span> atomically:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"flag = %i"</span>, flag);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

9.将数组写入文件中,有什么注意事项? 
答: 
注意(1): 
如果将一个数组写入到文件中之后, 本质是写入了一个XML文件 
在iOS开发中一般情况下我们会将XML文件的扩展名保存为plist

注意(2) 
writeToFile只能写入数组中保存的元素都是Foundation框架中的类创建的对象, 如果保存的是自定义对象那么不能写入

10.如何从文件中读取数据到NSArray中? 
答: 
从文件中读取一个数组,此方法在字典转模型中,经常用到

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *newArray = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithContentsOfFile:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/abc.plist"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, newArray);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

NSMutableArray

1.什么是可变数组?和NSArray有什么区别? 
答: 
(1)NSMutableArray是NSArray的子类 
(2)NSArray是不可变的,一旦初始化完毕后,它里面的内容就永远是固定的, 不能删除里面的元素, 也不能再往里面添加元素 
(3)NSMutableArray是可变的,随时可以往里面添加\更改\删除元素

2.如何创建一个空的数组?创建可变数组有什么注意点?

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> *arrM = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> array];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> *arrM = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> alloc] init];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

注意: 
不能通过@[]来创建一个可变数组, 因为@[]创建出来的是一个不可变的数组

3.如何给可变数组增加内容? 
答: 
方法一:

<code class="hljs ini has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 102, 102);">[arrM addObject:@"james"]</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

方法二: 
将指定数组中的元素都取出来, 放到arrM中 \ 
并不是将整个数组作为一个元素添加到arrM中

<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[arrM addObjectsFromArray:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@[</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@"</span>james<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">", @"</span>jjj<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"]];</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

注意: 
以下是将整个数组作为一个元素添加

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> [arrM addObject:@[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ss"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"jjj"</span>]];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, arrM);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

4.如何给可变数组插入内容? 
答:

<code class="hljs vhdl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[arrM insertObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"xcq"</span> atIndex:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>];NSRange <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">range</span> = NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>);NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">range</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

插入一组数据, 指定数组需要插入的位置, 和插入多少个 
[arrM insertObjects:@[@”A”, @”B”] atIndexes:set];

5.如何删除可变数组中的内容? 
答:

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">删除<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[arrM removeObjectAtIndex:0]</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">删除数组中最后一个元素<span class="hljs-attr_selector" style="color: rgb(0, 136, 0); box-sizing: border-box;">[arrM removeLastObject]</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
<code class="hljs axapta has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">删除<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">index</span>位置的元素[arrM removeObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"A"</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

6.如何替换可变数组中的内容? 
答:

<code class="hljs ini has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 102, 102);">[arrM replaceObjectAtIndex:1 withObject:@"M"]</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

//简写:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">arrM[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>] = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ZS"</span>;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, arrM);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

7.如何获取可变数组中的内容? 
答:

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">NSLog</span>(<span class="hljs-at_rule" style="box-sizing: border-box;">@<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">"%@",</span> [arrM objectAtIndex:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>])</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

NSDictionary

  1. 什么是字典? 
    答:OC中的NSDictionary:根据key找到value,字典中存储的东西都是键值对

  2. 如何创建字典? 
    答:

方法一:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dict = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> dictionaryWithObject:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span> forKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *name = [dict objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name = %@"</span>, name);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

方法二: 
注意: key和value 是一一对应

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dict = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> dictionaryWithObjects:@[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"22"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1.75"</span>] forKeys:@[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"height"</span>]];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@ %@ %@"</span>, [dict objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>], [dict objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>], [dict objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"height"</span>]);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

方法三:简写:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dict = @{key:value};<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dict = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>: @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>};<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, dict[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>]);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dict = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"30"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"height"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1.75"</span>};<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@ %@ %@"</span>, dict[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>], dict[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>], dict[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"height"</span>]);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

3.如何对字典进行遍历? 
答:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dict = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"22"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"height"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1.75"</span>};</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

//获取字典中key和value的个数, 在字典中key称之为键, value称之为值

<code class="hljs axapta has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"count = %lu"</span>, [dict <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">count</span>]);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

方法一:老式for循环写法

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < dict<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.count</span>; ++i) {    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 获取字典中所有的key</span>    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *keys = [dict allKeys];    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 取出当前位置对应的key</span>    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//        NSLog(@"%@", keys[i]);</span>    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *key = keys[i];    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *value = dict[key];    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key = %@, value = %@"</span>, key, value);}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

方法二:增强for循环写法 
如何通过forin遍历字典, 会将所有的key赋值给前面的obj

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *key in dict) {<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, key);<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *value = dict[key];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key = %@, value = %@"</span>, key, value);}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

方法三:OC字典的迭代器来遍历

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[dict enumerateKeysAndObjectsUsingBlock:^(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> key, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> obj, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> *stop) {    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"key = %@, value = %@"</span>, key, obj);}];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

4.如何对字典文件进行读写? 
答: 
(1)将字典数据写入文件中

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dict = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"22"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"height"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1.75"</span>};[dict writeToFile:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/info.plist"</span> atomically:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

(2)从文件中读取字典数据 
注意: 字典和数组不同, 字典中保存的数据是无序的

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *newDict = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> dictionaryWithContentsOfFile:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/info.plist"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, newDict);<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *arr = @[@<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, @<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>, @<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>, @<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>];[arr writeToFile:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/abc.plist"</span> atomically:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

NSMutableDictionary

  1. 如何创建一个空的可变字典? 
    答: 
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];

  2. 如何给可变字典添加键值对? 
    答:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[dictM <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span>Object:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lnj"</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>Key:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

简写:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">dictM[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>] = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

会将传入字典中所有的键值对取出来添加到dictM中

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[dictM <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span>ValuesForKeysWithDictionary:@{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"30"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"height"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1.75"</span>}];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

3.如何删除可变字典键值对? 
答:

<code class="hljs ini has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 102, 102);">[dictM removeObjectForKey:@"name"]</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[dictM removeObjectsForKeys:<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@[</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@"</span>age<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">", @"</span>height<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"]];</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

4.如何修改可变字典中的键值对? 
答:利用setObject方法给同名的key赋值, 那么新值会覆盖旧值

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">[dictM <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span>Object:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"88"</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>Key:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

简写:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">dictM[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"age"</span>] = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"88"</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

5.使用可变字典有什么注意事项? 
答: 
(1)不能使用@{}来创建一个可变的字典 
NSMutableDictionary *dictM = @{@”name”:@”james”};//编译就会报错 
[dictM setObject:@”30” forKey:@”age”];

(2) 
如果是不可变字典, 那么key不能相同 
如果是不可变字典出现了同名的key, 那么后面的key对应的值不会被保存 
如果是在可变数组中, 后面的会覆盖前面的

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dict = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lkl"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lll"</span>};<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"dict = %@"</span>, dict);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableDictionary</span> *dictM = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableDictionary</span> dictionaryWithObjects:@[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lkl"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lll"</span>] forKeys:@[@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name"</span>]];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"dict = %@"</span>, dictM);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

6.NSArray和NSDictionary的区别? 
答: 
NSArray是有序的,NSDictionary是无序的 
NSArray是通过下标访问元素,NSDictionary是通过key访问元素

NSNumber

1.NSNumber的应用场景? 
答: 
NSNumber可以将基本数据类型包装成对象,这样就可以间接将基本数据类型存进NSArray\NSDictionary中

2.如何将基本数据类型转换为对象类型? 
答:

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> age = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> number= <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5.1</span>;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">value</span> =  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> *ageN = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithInt:age];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> *numberN = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithDouble:number];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> *valueN = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithInt:value];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *arr = @[ageN, numberN, valueN];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arr = %@"</span>, arr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

3.如何将对象类型转换为基本数据类型? 
答:

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> temp = [ageN intValue];<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> temp = [numberN doubleValue];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

4.如何将基本数据类型转换对象类型简写?有什么注意点? 
答: 
注意: 如果传入的是变量那么必须在@后面写上(), 如果传入的常量, 那么@后面的()可以省略

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> *temp = @(number);<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> *temp  =@<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10.10</span>;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, temp);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

NSValue

1.NSValue的应用场景? 
答: 
(1)NSNumber是NSValue的子类, 但NSNumber只能包装数字类型 
(2)NSValue可以包装任意值 
因此, 可以用NSValue将结构体包装后,加入NSArray\NSDictionary中

2.如何利用NSValue包装常用的结构体? 
答:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">CGPoint</span> point = NSMakePoint(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>);NSValue *value = [NSValue valueWithPoint:point];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *arr = @[value];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, arr);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

3.如何利用NSValue包装自定义的结构体? 
答:

<code class="hljs d has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span>{    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> age;    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">char</span> *name;    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">double</span> height;}Person;Person p = {<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lld"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.75</span>};</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

NSDate

1.NSDate的应用场景? 
答:NSDate可以用来表示时间, 可以进行一些常见的日期\时间处理 
[NSDate date]返回的就是当前0时区的时间

2.如何获取当前时区的时间? 
答: 
(1) date方法创建的时间对象, 对象中就保存了当前的时间

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDate</span> *now = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDate</span> date];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

(2)获取当前所处的时区

<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">NSTimeZone *zone </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> [NSTimeZone systemTimeZone];</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

(3)获取当前时区和指定时区时间的时间差

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-constant" style="box-sizing: border-box;">NSInteger</span> seconds = [zone <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">secondsFromGMTForDate:</span>now];<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>    <span class="hljs-constant" style="box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"seconds = %lu"</span>, seconds);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

(4)计算出当前时区的时间

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDate</span> *newDate = [now dateByAddingTimeInterval:seconds];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"newDate = %@"</span>, newDate);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

3.如何格式化时间? 
答: 
NSDate 转 NSString 
(1)创建时间

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDate</span> *now = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDate</span> date];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

(2)创建时间格式化

<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">NSDateFormatter *formatter </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> [[NSDateFormatter alloc] init];</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

(3)指定格式

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">formatter<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.dateFormat</span> = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"yyyy-MM-dd HH:mm:ss"</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

(4)格式化时间

<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">NSString *<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = [formatter stringFromDate:now];NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

NSString 转 NSDate 
(1)获取时间

<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">NSString *<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2015-08-20 07:05:26 +0000"</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

2.创建时间格式化

<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">NSDateFormatter *formatter </span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> [[NSDateFormatter alloc] init];</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

注意: 
如果是从NSString格式化为NSDate, 那么dateFormat的格式, 必须和字符串中的时间格式一致, 否则可能转换失败 
(3)指定格式

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">formatter<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.dateFormat</span> = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"yyyy-MM-dd HH:mm:ss Z"</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

4.格式化日期

<code class="hljs axapta has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">NSDate *<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">date</span> = [formatter dateFromString:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>];NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">date</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

NSCalendar

1.如何获取当前时间的年月日时分秒? 
答: 
(1)获取当前时间

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDate</span> *now = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDate</span> date];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"now = %@"</span>, now);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

获得NSCalendar 日历对象

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSCalendar</span> *calendar1 = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSCalendar</span> currentCalendar];<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 利用日历类从当前时间对象中获取 年月日时分秒(单独获取出来)</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// components: 参数的含义是, 问你需要获取什么?</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 一般情况下如果一个方法接收一个参数, 这个参数是是一个枚举 , 那么可以通过|符号, 连接多个枚举值</span>NSCalendarUnit type = NSCalendarUnitYear |NSCalendarUnitMonth |NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;NSDateComponents *cmps = [calendar1 components:type fromDate:now];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"year = %ld"</span>, cmps<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.year</span>);<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"month = %ld"</span>, cmps<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.month</span>);<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"day = %ld"</span>, cmps<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.day</span>);<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hour = %ld"</span>, cmps<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.hour</span>);<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"minute = %ld"</span>, cmps<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.minute</span>);<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"second = %ld"</span>, cmps<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.second</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>

2.如何获取当前时间和指定时间的时间差? 
答:

<code class="hljs vbscript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.</span>过去的一个时间NSString *str = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2015-06-29 07:05:26 +0000"</span>;NSDateFormatter *formatter = [[NSDateFormatter alloc] init];formatter.dateFormat = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"yyyy-MM-dd HH:mm:ss Z"</span>;NSDate *<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">date</span> = [formatter dateFromString:str];<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.</span>当前的时间NSDate *<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">now</span> = [NSDate <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">date</span>];NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"date = %@"</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">date</span>);NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"now = %@"</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">now</span>);<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.</span>比较两个时间NSCalendar *calendar = [NSCalendar currentCalendar];NSCalendarUnit type = NSCalendarUnitYear |NSCalendarUnitMonth |NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;NSDateComponents *cmps = [calendar components:type fromDate:<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">date</span> toDate:<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">now</span> options:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>];NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%ld年%ld月%ld日%ld小时%ld分钟%ld秒钟"</span>, cmps.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">year</span>, cmps.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">month</span>, cmps.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">day</span>, cmps.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">hour</span>, cmps.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">minute</span>, cmps.<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">second</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li></ul>

NSFileManager

1.什么是NSFileManager?如何获取NSFileManager 对象? 
答: 
NSFileManager是用来管理文件系统的 
它可以用来进行常见的文件\文件夹操作

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> *manager = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSFileManager</span> defaultManager];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

2.如何判断一个文件或者文件夹是否存在? 
答:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> flag = [manager fileExistsAtPath:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/video/01-NSArray.text"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"flag = %i"</span>, flag);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

3.如何获取文件或文件夹的属性? 
答:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *info = [manager attributesOfItemAtPath:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/video/acn.mp4"</span> error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"info = %@"</span>, info);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

4.如何获取文件夹中所有的文件? 
答: 
注意:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *res = [manager contentsOfDirectoryAtPath:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/xiaomage/Desktop/video"</span> error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"res = %@"</span>, res);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

获取当前文件夹下所有的文件, 能获取子文件夹下面的文件

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *res = [manager subpathsAtPath:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/video"</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *res = [manager subpathsOfDirectoryAtPath:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/video"</span> error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"res = %@"</span>, res);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

5.如何创建文件夹? 
答: 
createDirectoryAtPath: 告诉系统文件夹需要创建到什么位置 
withIntermediateDirectories: 如果指定的文件中有一些文件夹不存在, 是否自动创建不存在的文件夹 
attributes: 指定创建出来的文件夹的属性 
error: 是否创建成功, 如果失败会给传入的参数赋值 
注意: 该方法只能用于创建文件夹, 不能用于创建文件

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> flag = [manager createDirectoryAtPath:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Users/james/Desktop/abc/llq"</span> withIntermediateDirectories:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span> attributes:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span> error:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%i"</span>, flag);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

6.如何创建文件? 
createFileAtPath: 指定文件创建出来的位置 
contents : 文件中的内容 
attributes: 创建出来的文件的属性 
NSData : 二进制数据 
注意: 该方法只能用于创建文件, 不能用于创建文件夹

Copy

1.使用copy功能的前提是什么? 
答: 
copy : 需要遵守NSCopying协议,实现copyWithZone:方法 
使用mutableCopy的前提 
需要遵守NSMutableCopying协议,实现mutableCopyWithZone:方法

2.如何使用copy功能? 
答: 
一个对象可以调用copy或mutableCopy方法来创建一个副本对象 
copy : 创建的是不可变副本(如NSString、NSArray、NSDictionary) 
mutableCopy :创建的是可变副本(如NSMutableString、NSMutableArray、NSMutableDictionary)

3.copy基本原则? 
答: 
修改源对象的属性和行为,不会影响副本对象 
修改副本对象的属性和行为,不会影响源对象

4.为什么通过不可变对象调用了copy方法, 不会生成一个新的对象? 
答: 
因为原来的对象是不能修改的, 拷贝出来的对象也是不能修改的 
既然两个都不能修改, 所以永远不能影响到另外一个对象, 那么已经符合需求 
所以: OC为了对内存进行优化, 就不会生成一个新的对象

copy内存管理

1.浅复制(浅拷贝,指针拷贝,shallow copy) 
源对象和副本对象是同一个对象 
源对象(副本对象)引用计数器+1,相当于做一次retain操作 
本质是:没有产生新的对象

2.深复制(深拷贝,内容拷贝,deep copy) 
源对象和副本对象是不同的两个对象 
源对象引用计数器不变,副本对象计数器为1(因为是新产生的) 
本质是:产生了新的对象

copy和Property

1.@property中的copy的作用是什么? 
答: 
(1)防止外界修改内部的数据 
(2)可以使用copy保存block, 这样可以保住block中使用的外界对象的命 
block默认存储在栈中, 栈中的block访问到了外界的对象, 不会对对象进行retain

2.@property内存管理原则? 
答: 
MRC 
1> copy : 只用于NSString\block 
2> retain : 除NSString\block以外的OC对象 
3> assign :基本数据类型、枚举、结构体(非OC对象),当2个对象相互引用,一端用retain,一端用assign

ARC 
1> copy : 只用于NSString\block 
2> strong : 除NSString\block以外的OC对象 
3> weak : 当2个对象相互引用,一端用strong,一端用weak 
4> assgin : 基本数据类型、枚举、结构体(非OC对象)

3.如何解决block中的循环引用? 
答:如果对象中的block又用到了对象自己, 那么为了避免内存泄露, 应该将对象修饰为__block

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">__block Person *p = [[Person alloc] init]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 1</span>p<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.name</span> = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"james"</span>;<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"retainCount = %lu"</span>, [p retainCount]);p<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.pBlock</span> = ^{    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"name = %@"</span>, p<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.name</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 2</span>};<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"retainCount = %lu"</span>, [p retainCount]);p<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.pBlock</span>();</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

自定义类实现Copy

1.自定义类如何实现copy操作? 
答: 
(1)以后想让自定义的对象能够被copy只需要遵守NSCopying协议 
(2)实现协议中的- (id)copyWithZone:(NSZone *)zone 
(3)在- (id)copyWithZone:(NSZone *)zone方法中创建一个副本对象, 然后将当前对象的值赋值给副本对象即可

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>)copyWithZone:(NSZone *)zone{    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 1.创建一个新的对象</span>    Person *p = [[[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> class] allocWithZone:zone] init];    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 2.设置当前对象的内容给新的对象</span>    p<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.age</span> = _age;    p<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.name</span> = _name;    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 3.返回新的对象</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> p;}- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>)mutableCopyWithZone:(NSZone *)zone{    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 1.创建一个新的对象</span>    Person *p = [[[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> class] allocWithZone:zone] init];    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 2.设置当前对象的内容给新的对象</span>    p<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.age</span> = _age;    p<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.name</span> = _name;    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 3.返回新的对象</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> p;}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>

单例ARC和MRC写法

1.什么是单例模式? 
答:类的对象成为系统中唯一的实例,提供一个访问点,供客户类 共享资源 
单例就是无论怎么创建都只能有一个实例对象

2.什么情况下使用单例? 
答: 
(1)类只能有一个实例,而且必须从一个为人熟知的访问点对其进行访问,比如工厂方法。 
(2)这个唯一的实例只能通过子类化进行扩展,而且扩展的对象不会破坏客户端代码。

3.创建单例对象的方法一般以什么开头? 
答: 
(1)一般情况下创建一个单利对象都有一个与之对应的类方法 
(2)一般情况下用于创建单利对象的方法名称都以share开头, 或者以default开头 
单例在多线程的应用? 
答:多线程中的单例

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">+ (instancetype)allocWithZone:(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> _NSZone *)zone{    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 以下代码在多线程中也能保证只执行一次</span>    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_once_t</span> onceToken;    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_once</span>(&onceToken, ^{        _instance = [[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> allocWithZone:zone] init];    });    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _instance;}</code>
0 0