Skip to content

Instantly share code, notes, and snippets.

@hyukhur
Last active April 15, 2016 04:59
Show Gist options
  • Save hyukhur/87eecabbd4c762432d49524c106d29ee to your computer and use it in GitHub Desktop.
Save hyukhur/87eecabbd4c762432d49524c106d29ee to your computer and use it in GitHub Desktop.
Objective-C super class & Swift subclass with NSCoding
@interface ObjcClass : NSObject <NSCoding>
@property (nonatomic, strong) NSString *name;
@end
@implementation ObjcClass
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:_name forKey:@"name"];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [self init];
if (self) {
_name = [coder decodeObjectForKey:@"name"];
}
return self;
}
@end
class SwiftClass: ObjcClass {
let address: String
override func encodeWithCoder(aCoder: NSCoder) {
super.encodeWithCoder(aCoder)
aCoder.encodeObject(address, forKey: "address")
}
required init?(coder aDecoder: NSCoder) {
guard let addressValue = aDecoder.decodeObjectForKey("address") as? String else {
return nil
}
address = addressValue
super.init(coder: aDecoder)
}
override init() {
address = ""
super.init()
}
}
@hyukhur
Copy link
Author

hyukhur commented Apr 15, 2016

swiftClass.init(coder:aDecoder) -> objcClass.initWithCoder -> swiftClass.init -> objcClass.init

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment