zoukankan      html  css  js  c++  java
  • Object copying

    NSObject类定义了copy方法,但这只是一个convinence method for classes who adopt NSCopying协议。如果某个类没有实现copyWithZone:,那调用copy方法会抛出异常。NSObject类的方法mutableCopy对应于NSMutableCopying protocol的方法mutableCopyWithZone:

    NSObject does not itself support the NSCopying protocol. Subclasses must support the protocol and implement the copyWithZone: method. A subclass version of the copyWithZone: method should send the message to super first, to incorporate its implementation, unless the subclass descends directly from NSObject.

    如果一个类考虑adopt NSCopying协议,那还要考虑是否应该选择NSMutableCopying?如何选择呢?先看官方文档。

    The returned object is implicitly retained by the sender, who is responsible for releasing it. The copy returned is immutable if the consideration “immutable vs. mutable” applies to the receiving object; otherwise the exact nature of the copy is determined by the class.

    也就是说,对于一个类/对象来说,在考虑adopt NSCopying或者NSMutableCopying协议时,需要考虑receiving object是由有“immutable vs. mutable”之分,如果有此区分,那copyWithZone:返回的是immutable的copy,mutable的copy由mutableCopyWithZone:实现。如果没有“immutable vs. mutable”之分,那就不要实现mutableCopyWithZone:, 并且从通常意义上来考虑copy行为是什么样的,毕竟每个类的Copy行为是不一样的,比如有的是做shallow copy,有的是做deep copy,或者部分shallo,部分deep,完全依需求而定。

  • 相关阅读:
    on asp.net
    总结
    CSS的一点使用体会
    existence way of The malicious software
    算法空山幽谷的佳人
    杀毒软件工程师看的书籍
    经典sql语句大全
    客户提的一个需求
    机器什么时候能够学习?
    当实证资产定价遇上机器学习
  • 原文地址:https://www.cnblogs.com/whyandinside/p/2958152.html
Copyright © 2011-2022 走看看