Create a copy of a UIView in Swift

后端 未结 7 1853
长发绾君心
长发绾君心 2020-11-29 05:01

Because objects are reference types, not value types, if you set a UIView equal to another UIView, the views are the same object. If you modify one

7条回答
  •  萌比男神i
    2020-11-29 05:24

    You can't arbitrarily copy an object. Only objects that implement the NSCopying protocol can be copied.

    However, there is a workaround: Since UIViews can be serialized to disk (e.g. to load from a XIB), you could use NSKeyedArchiver and NSKeyedUnarchiver to create a serialized NSData describing your view, then de-serialize that again to get an independent but identical object.

提交回复
热议问题