Working with files
- NSFileManager: 对文件和目录的基本操作
Create a new file
Read from an existing file
Write data to a file
Rename a file
Remove (delete) a file
Test for the existence of a file
Determine the size of a file as well as other attributes Make a copy of a file
Test two files to see whether their contents are equal
- NSFileHandle: 对文件内容的基本操作
Open a file for reading, writing, or updating (reading and writing)
Seek to a specified position within a file
Read or write a specified number of bytes from and to a file
- NSURL —— work with URLs
- NSBundle —— work with application’s bundle
Managing Files and Directories: NSFileManager
- relative pathname: relative to the current directory
- absolute pathname: begin with root directory “/”
- “~username”: 用户的home directory地址(=/User/username)
创建一个NSFileManeger对象:[NSFileManager defaultManager];
attributes dictionary 属性词典:includes information such as the file’s owner, its size, its creation date, ……
symbolic link(符号链接、软链接):一类特殊的文件, 其包含有一条以绝对路径或者相对路径的形式指向其它文件或者目录的引用。一个符号链接文件仅包含有一个文本字符串,其被操作系统解释为一条指向另一个文件或者目录的路径。它是一个独立文件,其存在并不依赖于目标文件。(与快捷方式不同,快捷方式是普通文件,具有.lnk扩展名)
NSData:
set up a buffer(内存缓冲区), read the contents of the file into it, or write the contents of a buffer out to a file.
immutable (NSData) or mutable (NSMutableData)
枚举目录的内容:获得目录内容的列表,enumeratorAtPath:(枚举子目录内容)和contentsOfDirectoryAtPath:方法(不检索子目录)
NSPathUtilities.h
Work with path:包含NSString的函数和category扩展,可以操作路径名。尽量使用
函数NSTemporaryDirectory返回系统用来存放临时文件的目录路径名,如果在此目录中创建临时文件,在运行结束时一定要删除之,并且要保证文件名的唯一性,特别是应用程序有多个实例运行时(多用户登录系统且运行程序时)。
lastPathComponent方法用来从路径中提取最后一个组成部分:从绝对路径中提取文件名
常用iOS应用文件目录
【注】iOS Data Storage Guidelines
NSProcessInfo class
set and retrieve various types of information about your running application(进程)
NSFileHandle class
处理文件的通用步骤(3 steps):
- 打开文件,生成NSFileHandle对象以在接下来的I/O操作中引用文件
- 在打开的文件中进行I/O操作
- 关闭文件
NSFileHandle class不包含创建文件的方法,需要使用NSFileManager class中的方法完成
NSBundle class
application bundle: 应用所使用的一些资源,包括图片、本地化语言、图标…
Memory Management and Automatic Reference Counting(ARC)
- Memory Management的核心是回收不用的内存空间以重复利用
- 两种内存管理模式:Manual Reference Counting和Automatic Reference Counting(垃圾回收机制已经被抛弃了)
Manual Reference Counting
- learn how to work with reference counts
- general concept:当一个对象创建时,其初始引用计数(initial reference count)置为1。每次需要确认对象存在时,你会通过引用计数加1的方式创建一个指向对象的引用: (具体方法)向对象发送retain消息。当不需要对象时,引用计数减1:(具体方法)向对象发送release消息。当对象的引用计数为0时,系统认为对象不会再被使用,就释放其所占用的内存(deallocate):(具体方法)向对象发送dealloc消息。dealloc方法可能需要根据具体情形进行override
有些方法会改变引用计数:对象使用NSMutableArray的addObject:方法加入到一个array中,一个view通过UIView的addSubview:方法加入为子视图;与之相反的动作则减少引用计数
对象被销毁(引用计数降为0且发送了dealloc消息)后,就不能被引用
Autorelease pool 自动释放池
- 某些方法使用完对象后因为需要返回对象而不能释放之 → 需要自动释放池来辅助管理
- 当自动释放池“干涸”(drained)时,就释放其管理的对象:向自动释放池对象发送drain消息
- 将对象加入自动释放池:向对象发送autorelease消息
- 当程序使用Foundation、UIKit、或AppKit框架中的class时,需要用@autorelease指令创建一个自动释放池
- 使用以alloc、copy、mutableCopy 或 new 开头的方法创建的对象都不会自动释放,需要手动释放或手动加入自动释放池
- Event Loop 事件轮询机制
- 当外界操作出发事件时,系统自动创建一个自动释放池然后调用应用程序的一些方法处理之。当事件处理完毕时,系统销毁自动释放池,返回event loop等待下一个事件,而所有在这个自动释放池中的对象也都会被释放,除非被retained
- override dealloc方法:先释放自己创建的对象,再[super dealloc];
Automatic Reference Counting
Strong variables ——(变量的default值)
- 声明变量的关键字:_strong —— “_strong ClassName var;”
- assigning an object reference to such a variable causes that object to be automatically retained
- the old object reference will be released before the assignment is made
- strong variables are initialized to zero by default
- properties的默认属性不是strong,而是unsafe_unretained(相当于assign) → 需要手动声明:(strong, nonatomic)
Weak variables
- 背景:互相引用的对象(main view 与 subview)→ 若两者都是strong,则产生retain circle,导致两者都不能销毁 → 解决:weak reference,拥有另个对象的对象(main view 拥有 subview)设置为strong,而余下一个设置为weak
- 声明变量的关键字:_weak ——
_weak … ;
- 相应的properties的声明:(weak, nonatomic)
- 另一个用处:delegates —— By making the variable that holds the reference to the delegate a weak variable, you’re assured that the variable will be zeroed if the delegate object gets deallocated.