- (void)encodeWithCoder:(NSCoder *)aCoder;//编码
- (id)initWithCoder:(NSCoder *)aDecoder;//解码
6.其中的字典和数组里面的元素也必须是这四个类型中的一个
获取文件路径
-(NSString *)filePath
{
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path=[docPath stringByAppendingPathComponent:@"picture"];
return path;
}
这 个函数几乎是固定的,其中NSDocumentDirectory是常量代表需要查找的路径为Documents,还有其他的路径如 NSCacheDirectory来表明要查找的路径是Caches文件夹。第二个NSUserDomainMask表示检索范围是在沙盒内。第三个是决 定是否展开波浪线符号。
//添加 home键 通知。 home键被点之后执行 self的homeClicked方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(homeClicked) name:UIApplicationDidEnterBackgroundNotification object:nil];
注意其中的UIApplicationDidBackgroundNotifacation是表示按下了home键
文件的读取时通常要用到,NSFileManager来对数据进行增删改查等操作。而通常还有用到判断是否文件在路径下存在也用到它。如:
if([fm fileExistsAtPath:[self filePath]])
{
NSArray *array = [NSArray arrayWithContentsOfFile:[self filePath]];
_tf1.text = [array objectAtIndex:0];
_tf2.text = [array objectAtIndex:1];
_tf3.text = [array objectAtIndex:2];
}
可 以根据image得到数据,也可以根据数据得到image;方法分别是 self.data=UIImageJPEGRepresentation(image1,1),此处的data定义为属性,定义为属性之后要用 self.来调用,不然就等同于实例变量了。只有jpeg和png图片可以转化成数据形式,png图片对应的是 UIImagePNGRepresentation()。
而 根据数据及其途径可以找到图片,image=[UIImage imageWithData:[NSData dataWithContentsOfFile: [self filePath]]];对于这些简单类型的用文件读写就够了,不需要使用归档及后面的数据库。还可以根据路径找到图片image=[UIImage imageWithContentsOfFile[self filePath]];