用NSCoding协议完成“编码/解码”操作-Object-C
Archiving Objective-C Objects with NSCoding
For the seasoned Cocoa developer, this is a piece of cake. For newer developers, this can be a real pain, especially if you don’t know what you’re looking for. I get this question a decent amount, so I figured I’d put a quick guide together.
The Problem
You can’t put just any object in a plist. This mainly gets people when they want to put something into NSUserDefaults and get an error (because NSUserDefaults archives to a plist under the hood).
Plists only support the core types: NSString, NSNumber, NSDate, NSData, NSArray, NSDictionary (and their CF buddies thanks to the toll-free bridge). The key here is NSData. You can convert any object to NSData with the NSCoding protocol.
The Solution
There are two things you have to do: implement NSCoding and then use the archiver and unarchiver.
Implementing NSCoding
Say you have an object that looks like this:
@interface Note : NSObject {
NSString *title;
NSString *author;
BOOL published;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *author;
@property (nonatomic) BOOL published;
@end
#import "Note.h" @implementation Note @synthesize title;
@synthesize author;
@synthesize published; - (void)dealloc {
[title release];
[author release];
[super dealloc];
} @end
Pretty simple, right?
Now, all you have to do to implement NSCoding is the following:
@interface Note : NSObject <NSCoding> {
NSString *title;
NSString *author;
BOOL published;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *author;
@property (nonatomic) BOOL published;
@end
#import "Note.h" @implementation Note @synthesize title;
@synthesize author;
@synthesize published; - (void)dealloc {
[title release];
[author release];
[super dealloc];
} - (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
self.title = [decoder decodeObjectForKey:@"title"];
self.author = [decoder decodeObjectForKey:@"author"];
self.published = [decoder decodeBoolForKey:@"published"];
}
return self;
} - (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:title forKey:@"title"];
[encoder encodeObject:author forKey:@"author"];
[encoder encodeBool:published forKey:@"published"];
} @end
Pretty simple. All I did was add the <NSCoding> protocol delectation in the header file and initWithCoder: and encodeWithCoder: in the implementation. You use these methods to tell NSCoder how to encode your object into data. Notice how two of the variables are objects and one was a BOOL. You have to use different methods for non-objects. The NSCoder documentation has the full list.
Remember, that you can use NSCoder to archive your object however whatever you want. It doesn’t have to just be all of the instance variables in your object, although that’s what you’ll do 90% of the time.
Using the Archiver and Unarchiver
This part is also really easy. Let’s say you have an array of notes that you want to put into NSUserDefaults, here’s the code:
// Given `notes` contains an array of Note objects
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notes];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"notes"];
Unarchiving is just as easy:
NSData *notesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"notes"];
NSArray *notes = [NSKeyedUnarchiver unarchiveObjectWithData:notesData];
用NSCoding协议完成“编码/解码”操作-Object-C的更多相关文章
- java中文乱码解决之道(五)-----java是如何编码解码的
在上篇博客中LZ阐述了java各个渠道转码的过程,阐述了java在运行过程中那些步骤在进行转码,在这些转码过程中如果一处出现问题就很有可能会产生乱码!下面LZ就讲述java在转码过程中是如何来进行编码 ...
- java中文乱码解决之道(六)-----javaWeb中的编码解码
在上篇博客中LZ介绍了前面两种场景(IO.内存)中的java编码解码操作,其实在这两种场景中我们只需要在编码解码过程中设置正确的编码解码方式一般而言是不会出现乱码的.对于我们从事java开发的人而言, ...
- java中文乱码解决之道(六)—–javaWeb中的编码解码
在上篇博客中LZ介绍了前面两种场景(IO.内存)中的java编码解码操作,其实在这两种场景中我们只需要在编码解码过程中设置正确的编码解码方式一般而言是不会出现乱码的.对于我们从事java开发的人而言, ...
- java中文乱码解决之道(五)—–java是如何编码解码的
原文出处:http://cmsblogs.com/?p=1491 在上篇博客中LZ阐述了java各个渠道转码的过程,阐述了java在运行过程中那些步骤在进行转码,在这些转码过程中如果一处出现问题就很有 ...
- java是如何编码解码的
在上篇博客中LZ阐述了java各个渠道转码的过程,阐述了java在运行过程中那些步骤在进行转码,在这些转码过程中如果一处出现问题就很有可能会产生乱码!下面LZ就讲述java在转码过程中是如何来进行编码 ...
- url参数的编码解码Demo
为了保证在页面传递数据的安全性,我们通常会对Url传递的参数进行编码解码操作.我们写一个Demo剖析URL编码解码过程. 完整Demo下载地址 1. Url参数如何在服务端进行编码和解码. 1.1 U ...
- 第57节:Java中流的操作以及编码解码
我的博客: https://huangguangda.cn/ https://huangguangda.github.io/ 前言: 编码解码:编码时将信息从一种形式变成为另一种形式,成为编码.编码为 ...
- day8_文件操作及编码解码
一.文件操作基本流程 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所 ...
- python基础3之文件操作、字符编码解码、函数介绍
内容概要: 一.文件操作 二.字符编码解码 三.函数介绍 一.文件操作 文件操作流程: 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 基本操作: #/usr/bin/env ...
随机推荐
- c++友元函数友元类
友元函数和友元类的需要: 类具有封装和信息隐藏的特性.只有类的成员函数才能访问类的私有成员,程序中的其他函数是无法访问私有成员的.非成员函数可以访问类中的公有成员,但是如果将数据成员都定义为公有的,这 ...
- [工作笔记]JDK版本不同导致的SSL异常
前言 遇到这个问题得说一下笔者的开发环境,笔者所在公司,平时开发用的web容器是jboss,使用的JDK是oracle的JDK,但是测试和生产环境用的是WAS,JDK用的是IBM的JDK,由于项目的不 ...
- 【Linux学习】Linux文件系统4—Linux文件硬链接与软连接
Linux文件系统4-Linux文件硬链接与软连接 inode:索引节点 (连接文件)link 一.文件硬链接 1.Linux文件系统中,inode只相同的文件是硬链接文件 2.不同文件名,inode ...
- Identity Server 4 原理和实战(完结)_----选看 OAuth 2.0 简介(上)
https://www.yuque.com/yuejiangliu/dotnet/cg95ni 代表资源所有者的凭据 授权 Authorization Grant 授权是一个代表着资源所有者权限的凭据 ...
- Flink on Yarn模式启动流程源代码分析
此文已由作者岳猛授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Flink on yarn的启动流程可以参见前面的文章 Flink on Yarn启动流程,下面主要是从源码角 ...
- AtCoder Beginner Contest 087 D People on a Line(DFS)
题意 给出n个点,m组关系L,R,D,L在R的左边距离D,判断是否存在n个人的位置满足m组关系 分析 Consider the following directed graph G: There ar ...
- github的上传本地文档
自从使用github以来,一直都是在github网站在线上传文件到仓库中,但是有时因为网络或者电脑的原因上传失败.最重要的原因是我习惯本地编辑,完成以后再一起上传github.看过了几个教程,总结出最 ...
- 当前View的坐标相对其他View的位置坐标
// 将rect由rect所在视图转换到目标视图view中,返回在目标视图view中的rect - (CGRect)convertRect:(CGRect)rect toView:(UIView *) ...
- 根据日期计算发布时间段(NSCalendar)
// 返回发布时间dateWithString - (NSString *)backReleaseTimeWithDateStr:(NSString *)dateWithString{ // 获取当前 ...
- Day2课后作业:sed替换程序
#!/usr/bin/env python #_*_conding:utf-8_*_ import sys,os old_file = sys.argv[1] new_file = sys.argv[ ...