decodeBoolForKey
decodeBoolForKey is a method of the NSCoder class used in Apple’s Cocoa and Cocoa Touch frameworks to retrieve a boolean value that was previously encoded with a matching key. It is commonly employed when implementing the NSCoding protocol to support object serialization and deserialization.
Users typically implement encoding and decoding methods in a class that conforms to NSCoding. During encoding,
In Objective-C, the method is called decodeBoolForKey:. In Swift, the equivalent is decodeBool(forKey:) on NSCoder or
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeBool:YES forKey:@"isEnabled"];
}
- (instancetype)initWithCoder:(NSCoder *)coder {
_isEnabled = [coder decodeBoolForKey:@"isEnabled"];
}
}
required init?(coder aDecoder: NSCoder) {
isEnabled = aDecoder.decodeBool(forKey: "isEnabled")
}
encodeBool:forKey:, decodeObjectForKey:, NSCoding, NSSecureCoding.