• <small id='dYDWf'></small><noframes id='dYDWf'>

      <i id='dYDWf'><tr id='dYDWf'><dt id='dYDWf'><q id='dYDWf'><span id='dYDWf'><b id='dYDWf'><form id='dYDWf'><ins id='dYDWf'></ins><ul id='dYDWf'></ul><sub id='dYDWf'></sub></form><legend id='dYDWf'></legend><bdo id='dYDWf'><pre id='dYDWf'><center id='dYDWf'></center></pre></bdo></b><th id='dYDWf'></th></span></q></dt></tr></i><div id='dYDWf'><tfoot id='dYDWf'></tfoot><dl id='dYDWf'><fieldset id='dYDWf'></fieldset></dl></div>

      • <bdo id='dYDWf'></bdo><ul id='dYDWf'></ul>

    1. <legend id='dYDWf'><style id='dYDWf'><dir id='dYDWf'><q id='dYDWf'></q></dir></style></legend><tfoot id='dYDWf'></tfoot>

      1. 如何在 Objective-C 中复制对象

        How to copy an object in Objective-C(如何在 Objective-C 中复制对象)
        1. <i id='oi2vI'><tr id='oi2vI'><dt id='oi2vI'><q id='oi2vI'><span id='oi2vI'><b id='oi2vI'><form id='oi2vI'><ins id='oi2vI'></ins><ul id='oi2vI'></ul><sub id='oi2vI'></sub></form><legend id='oi2vI'></legend><bdo id='oi2vI'><pre id='oi2vI'><center id='oi2vI'></center></pre></bdo></b><th id='oi2vI'></th></span></q></dt></tr></i><div id='oi2vI'><tfoot id='oi2vI'></tfoot><dl id='oi2vI'><fieldset id='oi2vI'></fieldset></dl></div>
            <tbody id='oi2vI'></tbody>
          <tfoot id='oi2vI'></tfoot>

              • <small id='oi2vI'></small><noframes id='oi2vI'>

                <legend id='oi2vI'><style id='oi2vI'><dir id='oi2vI'><q id='oi2vI'></q></dir></style></legend>

                  <bdo id='oi2vI'></bdo><ul id='oi2vI'></ul>
                • 本文介绍了如何在 Objective-C 中复制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I need to deep copy a custom object that has objects of its own. I've been reading around and am a bit confused as to how to inherit NSCopying and how to use NSCopyObject.

                  解决方案

                  As always with reference types, there are two notions of "copy". I'm sure you know them, but for completeness.

                  1. A bitwise copy. In this, we just copy the memory bit for bit - this is what NSCopyObject does. Nearly always, it's not what you want. Objects have internal state, other objects, etc, and often make assumptions that they're the only ones holding references to that data. Bitwise copies break this assumption.
                  2. A deep, logical copy. In this, we make a copy of the object, but without actually doing it bit by bit - we want an object that behaves the same for all intents and purposes, but isn't (necessarily) a memory-identical clone of the original - the Objective C manual calls such an object "functionally independent" from it's original. Because the mechanisms for making these "intelligent" copies varies from class to class, we ask the objects themselves to perform them. This is the NSCopying protocol.

                  You want the latter. If this is one of your own objects, you need simply adopt the protocol NSCopying and implement -(id)copyWithZone:(NSZone *)zone. You're free to do whatever you want; though the idea is you make a real copy of yourself and return it. You call copyWithZone on all your fields, to make a deep copy. A simple example is

                  @interface YourClass : NSObject <NSCopying> 
                  {
                     SomeOtherObject *obj;
                  }
                  
                  // In the implementation
                  -(id)copyWithZone:(NSZone *)zone
                  {
                    // We'll ignore the zone for now
                    YourClass *another = [[YourClass alloc] init];
                    another.obj = [obj copyWithZone: zone];
                  
                    return another;
                  }
                  

                  这篇关于如何在 Objective-C 中复制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  what#39;s property copy means in Cocoa#39;s Framework?(like UITabBar#39;s items property)(Cocoa 框架中的属性副本是什么意思?(如 UITabBar 的 items 属性))
                  Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
                  SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
                  WebKit on th iPhone: is it possible to copy text to the clipboad with JavaScript(iPhone上的WebKit:是否可以使用JavaScript将文本复制到剪贴板)
                  Difference between retain and copy?(保留和复制的区别?)
                  Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)

                  <small id='ase4t'></small><noframes id='ase4t'>

                    <tbody id='ase4t'></tbody>
                • <legend id='ase4t'><style id='ase4t'><dir id='ase4t'><q id='ase4t'></q></dir></style></legend>
                    <bdo id='ase4t'></bdo><ul id='ase4t'></ul>
                      <tfoot id='ase4t'></tfoot>

                          <i id='ase4t'><tr id='ase4t'><dt id='ase4t'><q id='ase4t'><span id='ase4t'><b id='ase4t'><form id='ase4t'><ins id='ase4t'></ins><ul id='ase4t'></ul><sub id='ase4t'></sub></form><legend id='ase4t'></legend><bdo id='ase4t'><pre id='ase4t'><center id='ase4t'></center></pre></bdo></b><th id='ase4t'></th></span></q></dt></tr></i><div id='ase4t'><tfoot id='ase4t'></tfoot><dl id='ase4t'><fieldset id='ase4t'></fieldset></dl></div>