对象存储swift使用_使用NSCopy()在Swift中复制对象

对象存储swift使用

Copying objects (reference types) is quite often challenging. How are you supposed to make sure they copy properly if you are using Boxing?

复制对象(引用类型)通常很困难。 如果使用Boxing,应该如何确保它们正确复制?

Enter NSCopy()

输入NSCopy()

Difficulty: Beginner | Easy | Normal | Challenging

难度:初学者| 简单 | 普通 | 具有挑战性的

先决条件: (Prerequisites:)

  • Coding in Swift Playgrounds (guide HERE)

    Swift操场上的编码(指南HERE )

  • A good understanding of value and reference types (guide HERE)

    对价值和参考类型有很好的理解(指南HERE )

This article leans rather substantially on my Boxing article, although the basics are repeated here. I’ve also assumed understanding of the equatable protocol.

这篇文章相当依赖我的拳击文章 ,尽管在此重复了基础知识。 我还假定了对平等协议的理解。

术语 (Terminology)

Boxing: The process of converting a value type to a reference type

装箱:将值类型转换为引用类型的过程

Class: An object that defines properties and methods in common, using pass by reference semantics

类:使用按引用传递语义来共同定义属性和方法的对象

Reference: A pointer to a variable or object

参考:指向变量或对象的指针

Reference type: A type that means instances have a single copy of data

引用类型:一种类型,表示实例具有单个数据副本

Struct: An object defined in Swift, using pass by value semantics

结构:在Swift中定义的对象,使用按值传递语义

Value: The something that would be stored in a variable

值:将存储在变量中的内容

Value type: A type that means each instance has a copy of data

值类型:表示每个实例都有数据副本的类型

动机 (The Motivation)

When you are using boxing to use reference semantics for a value type you risk coming unstuck — when copying objects. This can lead to rather difficult to identify bugs.

当您使用装箱对值类型使用引用语义时,在复制对象时可能会被卡住。 这可能导致很难发现错误。

Want to go there? No? Read on…

想去那里? 没有? 继续阅读...

人类 (The PersonClass)

For the means of this article I am creating a rather perfunctory PersonClass that conforms to the Equatable protocol. Yes, I know that two people with the same name aren’t necessarily the same person — but this is an example.

对于本文的手段我创建一个比较敷衍PersonClass一个符合Equatable协议。 是的,我知道两个名字相同的人不一定是同一个人-但这是一个例子

盒子 (The Box)

Our box is a class so works with reference semantics, and conforms to Equatable.

我们的box是一个类,因此可以使用引用语义来工作,并且符合Equatable

把一个人放在盒子里 (Putting a Person into a Box)

Our PersonStruct is going to go into a lightly sealed box.

我们的PersonStruct将放入一个密封的盒子中。

The structBox then reference semantics have been brought to the Person Struct. Awesome.

然后structBox引用语义已带入Person Struct 。 太棒了

Now what is happening in the code is creating a copy of the box using the (rather secretly added) copy method on box.

现在,代码中正在发生的事情是使用box上的(而不是秘密添加的)copy方法创建box的副本。

func copyBox() -> Box<T> {
let newElement = element
let newBox = Box(element: newElement)
return newBox
}

This takes our element, and places it into a new box (and then returns the box).

这将获取我们的元素,并将其放入新框(然后返回该框)。

It all seems fine…but how is that element being copied?Are we sure that it is using value semantics for the element? What if the underlying element type (represented by T) were a class?

一切似乎都很好……但是该元素是如何被复制的?我们确定该元素正在使用值语义吗? 如果基础元素类型(由T表示)是一个class怎么办?

Now you’re talking. To ensure this you would want to have a a protocol that guarantees you are copying all levels of your struct properly. NSCopying is just that protocol.

现在你说。 为了确保这一点,您需要一个协议,以确保您正确复制了结构的所有级别。 NSCopying 只是那个协议。

This involves a little bit of Protocol Composition, and of course updating our PersonStruct to conform to NSCopying.

这涉及到协议组合的一点点,当然更新我们的PersonStruct符合NSCopying

then the box will conform to the same protocol (you see that the protocol means that we need to conform to it for each level of the class — great.)

那么该框将符合相同的协议(您会看到该协议意味着我们需要在该类的每个级别都符合它-很好。)

Now we have adopted the NSCopying protocol so we can copy-me-do.

现在,我们采用了NSCopying协议,因此我们可以进行复制。

And each level of the class is adequately copied. Awesome!

并充分地复制了班级的每个级别。 太棒了!

The NSZone? Well, Apple say that you can ignore it. Ok, thanks Apple, I’ll do just that!

NSZone? 好吧, 苹果公司说你可以忽略它 。 好的,谢谢苹果,我就那样做!

结论 (Conclusion)

When you copy objects you need to be careful if all of the layers of the class are copied. If not you might be doing some sort of surface copy and have unwanted side-effects under the hood.

复制对象时,如果要复制该类的所有层,则需要小心。 如果不是这样,您可能正在做某种表面复制,并且在引擎盖下有不良的副作用。

Do you want that?

你想要那个吗?

No, no you don’t.

不,不,你不知道。

So I hope I’ve helped you out a little bit with the article above, explaining not just NSCopy() but NSZone and such good stuff in between.

因此,我希望我对上述文章有所帮助,不仅解释了NSCopy() ,还解释了NSZone及其之间的好东西。

Got a question? Just forward it to me on Twitter and I’ll get to it!

有问题吗? 只需在Twitter上将其转发给我,我就可以解决!

扩展您的知识 (Extend your knowledge)

Twitter联系人: (The Twitter contact:)

Any questions? You can get in touch with me HERE

任何问题? 您可以在这里与我联系

翻译自: https://medium.com/@stevenpcurtis.sc/nscopy-to-duplicate-objects-in-swift-ec5a52556bbb

对象存储swift使用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值