I have IBOutlet UILabel *label;
and I want to do this
UILabel *label = [titleLabel copy];
label.text = @\"Clone\";
titleLabel.text = @
I created it as an extension to the UILabel
.
extension UILabel {
func createCopy() -> UILabel {
let archivedData = NSKeyedArchiver.archivedData(withRootObject: self)
return NSKeyedUnarchiver.unarchiveObject(with: archivedData) as! UILabel
}
}
let anotherNameLabel = nameLabel.createCopy()
NSKeyedArchiver
will take an object from memory and convert it to text. "Keyed" means it uses words to describe the properties of this object, like "LabelFrame", "LabelText", etc. (These are fake keys.) This also called Serialization in other technologies.
NSKeyedUnarchiver
does the opposite. It will take that text and build an object. Here we take that object (Any?
) and convert it to a UILabel
.