IT English Collection(10) of Object encoding

1 前言

    今天我们来学习一篇关于对象编码的文章,具体内容如下。

2 详述

2.1 原文

    Object encoding converts an object’s class identity and state to a format that can be stored or transferred between processes. The class type and instance data are written to a byte stream that can persist after a program terminates. When the program is launched again, a newly allocated object can decode the storedrepresentation of itself and restore itself to its previous runtime state. Encoding usually occursin concert with archiving, which puts a graph of objects into a format (an archive) that can be written to the file system; unarchiving operates on an archive, asking each object in the stored graph to decode itself.


    Object encoding is also used in the OS Xdistributed objects API for transferring objects from one process to another. However, its most common use is for archiving, which like a property list, is amechanism for object persistence.

How to Encode and Decode Objects
    For your subclass to encode and decode its instances, it must conform to the NSCoding protocol and implement two methods: initWithCoder: and encodeWithCoder:. When a program unarchives or archives an object graph, these methods are invoked. In the encodeWithCoder: method, you encode the values of an object’s important instance variables; in the initWithCoder: method, you decode those values andreassign them to their instance variables. If an object receives an initWithCoder: message, none of its initializer methods are invoked.

    The sole argument of initWithCoder: and encodeWithCoder: is an NSCoder object whose purpose is to perform theactual decoding or encoding. Because NSCoder is an abstract class, the coder object is in mostcases an instance of one of the following concrete subclasses: NSKeyedArchiver, NSKeyedUnarchiver, NSArchiver, NSUnarchiver. The archiver classes declare methods for encoding an object’s instance variables; the unarchiver classes declare methods for decoding instance variables.

    The NSCoder methods work on objects, scalars, C arrays, structures, and strings, and on pointers to these types. Before you encode or decode an instance variable of your own class, be sure to first invoke the superclass implementation of initWithCoder: or encodeWithCoder:. When you decode objects from the byte stream, be sure to retain or copy them when you assign them to their instance variables.

Keyed Versus Sequential Archiving
    Two of the concrete NSCoder subclasses are different from each other in a fundamental way. The “keyed” archiver class (NSKeyedArchiver and NSKeyedUnarchiver) associate an encoded value with a string key and use that same key when decoding that value; thus instance variables can be encoded and decoded in any sequence. With the other type of coder (NSArchiver and NSUnarchiver) you encode instance variables in a certain sequence, and you must decode them in the same sequence. The sequential coders should be used only withlegacy code; new subclasses should use keyed archive coders.

2.2 生词

representation[,reprɪzen'teɪʃ(ə)n] n. 代表;表现;

in concert with相呼应;与合作;和一致

distributed[dɪ'strɪbjʊtɪd]  adj. 分布式的,分散式的

mechanism['mek(ə)nɪz(ə)m]  n. 机制;原理

conform[kən'fɔːm] vi. 符合;遵照

reassign[riːə'saɪn] vt. 再分配;再指定

sole[səʊl]  n. 基础;  adj. 唯一的;单独的;仅有的

actual['æktʃʊəl; -tjʊəl] adj. 真实的,实际的;

in most cases 大部分情况下;

concrete['kɒŋkriːt] adj. 具体的;有形的

scalars['skeɪlə] n. [数] 标量;[数] 数量

Versus['vɜːsəs] prep. 对;与...相对;对抗

Sequential[sɪ'kwenʃ(ə)l] adj. 连续的;相继的;

fundamental[fʌndə'ment(ə)l]  adj. 基本的,根本的

legacy['legəsɪ]  n. 遗赠,遗产

3 结语

    以上是所有内容,希望对大家有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值