Skip to content

Remove non-keyed encoding for data objects #626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,10 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
}

public required convenience init?(coder aDecoder: NSCoder) {
if !aDecoder.allowsKeyedCoding {
var cnt: UInt32 = 0
// We're stuck with (int) here (rather than unsigned int)
// because that's the way the code was originally written, unless
// we go to a new version of the class, which has its own problems.
withUnsafeMutablePointer(to: &cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
aDecoder.decodeValue(ofObjCType: "i", at: UnsafeMutableRawPointer(ptr))
}
let objects = UnsafeMutablePointer<AnyObject>.allocate(capacity: Int(cnt))
for idx in 0..<cnt {
// If conversion to NSObject fails then we really can't hold it anyway
objects.advanced(by: Int(idx)).initialize(to: aDecoder.decodeObject() as! NSObject)
}
self.init(objects: UnsafePointer<AnyObject>(objects), count: Int(cnt))
objects.deinitialize(count: Int(cnt))
objects.deallocate(capacity: Int(cnt))
} else if type(of: aDecoder) == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.objects") {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
if type(of: aDecoder) == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.objects") {
let objects = aDecoder._decodeArrayOfObjectsForKey("NS.objects")
self.init(array: objects as! [NSObject])
} else {
Expand All @@ -74,6 +61,9 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
}

open func encode(with aCoder: NSCoder) {
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
if let keyedArchiver = aCoder as? NSKeyedArchiver {
keyedArchiver._encodeArrayOfObjects(self, forKey:"NS.objects")
} else {
Expand Down
11 changes: 4 additions & 7 deletions Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,10 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
}

public required convenience init?(coder aDecoder: NSCoder) {
if !aDecoder.allowsKeyedCoding {
if let data = aDecoder.decodeData() {
self.init(data: data)
} else {
return nil
}
} else if type(of: aDecoder) == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.data") {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
if type(of: aDecoder) == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.data") {
guard let data = aDecoder._decodePropertyListForKey("NS.data") as? NSData else {
return nil
}
Expand Down
8 changes: 6 additions & 2 deletions Foundation/NSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ open class NSDateInterval : NSObject, NSCopying, NSSecureCoding {


public required convenience init?(coder: NSCoder) {
precondition(coder.allowsKeyedCoding)
guard coder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
guard let start = coder.decodeObject(of: NSDate.self, forKey: "NS.startDate") else {
coder.failWithError(NSError(domain: NSCocoaErrorDomain, code: CocoaError.coderValueNotFound.rawValue, userInfo: nil))
return nil
Expand Down Expand Up @@ -304,7 +306,9 @@ open class NSDateInterval : NSObject, NSCopying, NSSecureCoding {
}

open func encode(with aCoder: NSCoder) {
precondition(aCoder.allowsKeyedCoding)
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(startDate._nsObject, forKey: "NS.startDate")
aCoder.encode(endDate._nsObject, forKey: "NS.endDate")
}
Expand Down
25 changes: 4 additions & 21 deletions Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,10 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
}

public required convenience init?(coder aDecoder: NSCoder) {
if !aDecoder.allowsKeyedCoding {
var cnt: UInt32 = 0
// We're stuck with (int) here (rather than unsigned int)
// because that's the way the code was originally written, unless
// we go to a new version of the class, which has its own problems.
withUnsafeMutablePointer(to: &cnt) { (ptr: UnsafeMutablePointer<UInt32>) -> Void in
aDecoder.decodeValue(ofObjCType: "i", at: UnsafeMutableRawPointer(ptr))
}
let keys = UnsafeMutablePointer<NSObject>.allocate(capacity: Int(cnt))
let objects = UnsafeMutablePointer<AnyObject>.allocate(capacity: Int(cnt))
for idx in 0..<cnt {
keys.advanced(by: Int(idx)).initialize(to: aDecoder.decodeObject()! as! NSObject)
objects.advanced(by: Int(idx)).initialize(to: aDecoder.decodeObject()! as! NSObject)
}
self.init(objects: UnsafePointer<AnyObject>(objects), forKeys: UnsafePointer<NSObject>(keys), count: Int(cnt))
keys.deinitialize(count: Int(cnt))
keys.deallocate(capacity: Int(cnt))
objects.deinitialize(count: Int(cnt))
objects.deallocate(capacity: Int(cnt))

} else if type(of: aDecoder) == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.objects") {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
if type(of: aDecoder) == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.objects") {
let keys = aDecoder._decodeArrayOfObjectsForKey("NS.keys").map() { return $0 as! NSObject }
let objects = aDecoder._decodeArrayOfObjectsForKey("NS.objects")
self.init(objects: objects as! [NSObject], forKeys: keys)
Expand Down
52 changes: 17 additions & 35 deletions Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,20 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
}

public required init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
_code = aDecoder.decodeInteger(forKey: "NSCode")
_domain = aDecoder.decodeObject(of: NSString.self, forKey: "NSDomain")!._swiftObject
if let info = aDecoder.decodeObject(of: [NSSet.self, NSDictionary.self, NSArray.self, NSString.self, NSNumber.self, NSData.self, NSURL.self], forKey: "NSUserInfo") as? NSDictionary {
var filteredUserInfo = [String : Any]()
// user info must be filtered so that the keys are all strings
info.enumerateKeysAndObjects(options: []) {
if let key = $0.0 as? NSString {
filteredUserInfo[key._swiftObject] = $0.1
}
}
_userInfo = filteredUserInfo
}
} else {
var codeValue: Int32 = 0
aDecoder.decodeValue(ofObjCType: "i", at: &codeValue)
_code = Int(codeValue)
_domain = (aDecoder.decodeObject() as? NSString)!._swiftObject
if let info = aDecoder.decodeObject() as? NSDictionary {
var filteredUserInfo = [String : Any]()
// user info must be filtered so that the keys are all strings
info.enumerateKeysAndObjects(options: []) {
if let key = $0.0 as? NSString {
filteredUserInfo[key._swiftObject] = $0.1
}
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
_code = aDecoder.decodeInteger(forKey: "NSCode")
_domain = aDecoder.decodeObject(of: NSString.self, forKey: "NSDomain")!._swiftObject
if let info = aDecoder.decodeObject(of: [NSSet.self, NSDictionary.self, NSArray.self, NSString.self, NSNumber.self, NSData.self, NSURL.self], forKey: "NSUserInfo") as? NSDictionary {
var filteredUserInfo = [String : Any]()
// user info must be filtered so that the keys are all strings
info.enumerateKeysAndObjects(options: []) {
if let key = $0.0 as? NSString {
filteredUserInfo[key._swiftObject] = $0.1
}
_userInfo = filteredUserInfo
}
_userInfo = filteredUserInfo
}
}

Expand All @@ -98,16 +84,12 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
}

open func encode(with aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
aCoder.encode(_domain._bridgeToObjectiveC(), forKey: "NSDomain")
aCoder.encode(Int32(_code), forKey: "NSCode")
aCoder.encode(_userInfo?._bridgeToObjectiveC(), forKey: "NSUserInfo")
} else {
var codeValue: Int32 = Int32(self._code)
aCoder.encodeValue(ofObjCType: "i", at: &codeValue)
aCoder.encode(self._domain._bridgeToObjectiveC())
aCoder.encode(self._userInfo?._bridgeToObjectiveC())
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(_domain._bridgeToObjectiveC(), forKey: "NSDomain")
aCoder.encode(Int32(_code), forKey: "NSCode")
aCoder.encode(_userInfo?._bridgeToObjectiveC(), forKey: "NSUserInfo")
}

open override func copy() -> Any {
Expand Down
42 changes: 18 additions & 24 deletions Foundation/NSGeometry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,17 @@ extension CGPoint: NSSpecialValueCoding {
}

init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
self = aDecoder.decodePointForKey("NS.pointval")
} else {
self = aDecoder.decodePoint()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
self = aDecoder.decodePointForKey("NS.pointval")
}

func encodeWithCoder(_ aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
aCoder.encodePoint(self, forKey: "NS.pointval")
} else {
aCoder.encodePoint(self)
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encodePoint(self, forKey: "NS.pointval")
}

static func objCType() -> String {
Expand Down Expand Up @@ -103,19 +101,17 @@ extension CGSize: NSSpecialValueCoding {
}

init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
self = aDecoder.decodeSizeForKey("NS.sizeval")
} else {
self = aDecoder.decodeSize()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
self = aDecoder.decodeSizeForKey("NS.sizeval")
}

func encodeWithCoder(_ aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
aCoder.encodeSize(self, forKey: "NS.sizeval")
} else {
aCoder.encodeSize(self)
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encodeSize(self, forKey: "NS.sizeval")
}

static func objCType() -> String {
Expand Down Expand Up @@ -187,19 +183,17 @@ extension CGRect: NSSpecialValueCoding {
}

init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
self = aDecoder.decodeRectForKey("NS.rectval")
} else {
self = aDecoder.decodeRect()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
self = aDecoder.decodeRectForKey("NS.rectval")
}

func encodeWithCoder(_ aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
aCoder.encodeRect(self, forKey: "NS.rectval")
} else {
aCoder.encodeRect(self)
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encodeRect(self, forKey: "NS.rectval")
}

static func objCType() -> String {
Expand Down
36 changes: 13 additions & 23 deletions Foundation/NSNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,24 @@ open class NSNotification: NSObject, NSCopying, NSCoding {
}

public convenience required init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
guard let name = aDecoder.decodeObject(of: NSString.self, forKey:"NS.name") else {
return nil
}
let object = aDecoder.decodeObject(forKey: "NS.object")
// let userInfo = aDecoder.decodeObject(of: NSDictionary.self, forKey: "NS.userinfo")
self.init(name: Name(rawValue: String._unconditionallyBridgeFromObjectiveC(name)), object: object as! NSObject, userInfo: nil)

} else {
guard let name = aDecoder.decodeObject() as? NSString else {
return nil
}
let object = aDecoder.decodeObject()
// let userInfo = aDecoder.decodeObject() as? NSDictionary
self.init(name: Name(rawValue: String._unconditionallyBridgeFromObjectiveC(name)), object: object, userInfo: nil)
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
guard let name = aDecoder.decodeObject(of: NSString.self, forKey:"NS.name") else {
return nil
}
let object = aDecoder.decodeObject(forKey: "NS.object")
// let userInfo = aDecoder.decodeObject(of: NSDictionary.self, forKey: "NS.userinfo")
self.init(name: Name(rawValue: String._unconditionallyBridgeFromObjectiveC(name)), object: object as! NSObject, userInfo: nil)
}

open func encode(with aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
aCoder.encode(self.name.rawValue._bridgeToObjectiveC(), forKey:"NS.name")
aCoder.encode(self.object, forKey:"NS.object")
aCoder.encode(self.userInfo?._bridgeToObjectiveC(), forKey:"NS.userinfo")
} else {
aCoder.encode(self.name.rawValue._bridgeToObjectiveC())
aCoder.encode(self.object)
aCoder.encode(self.userInfo?._bridgeToObjectiveC())
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.name.rawValue._bridgeToObjectiveC(), forKey:"NS.name")
aCoder.encode(self.object, forKey:"NS.object")
aCoder.encode(self.userInfo?._bridgeToObjectiveC(), forKey:"NS.userinfo")
}

open override func copy() -> Any {
Expand Down
19 changes: 4 additions & 15 deletions Foundation/NSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,21 +336,10 @@ open class NSNumber : NSValue {
}

public required convenience init?(coder aDecoder: NSCoder) {
if !aDecoder.allowsKeyedCoding {
var objCType: UnsafeMutablePointer<Int8>? = nil
withUnsafeMutablePointer(to: &objCType, { (ptr: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>) -> Void in
aDecoder.decodeValue(ofObjCType: String(_NSSimpleObjCType.CharPtr), at: UnsafeMutableRawPointer(ptr))
})
if objCType == nil {
return nil
}
var size: Int = 0
let _ = NSGetSizeAndAlignment(objCType!, &size, nil)
let buffer = malloc(size)!
aDecoder.decodeValue(ofObjCType: objCType!, at: buffer)
self.init(bytes: buffer, objCType: objCType!)
free(buffer)
} else if type(of: aDecoder) == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.number") {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
if type(of: aDecoder) == NSKeyedUnarchiver.self || aDecoder.containsValue(forKey: "NS.number") {
let number = aDecoder._decodePropertyListForKey("NS.number")
if let val = number as? Double {
self.init(value:val)
Expand Down
34 changes: 16 additions & 18 deletions Foundation/NSOrderedSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,28 @@ open class NSOrderedSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
}

open func encode(with aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
for idx in 0..<self.count {
aCoder.encode(_SwiftValue.store(self.object(at: idx)), forKey:"NS.object.\(idx)")
}
} else {
NSUnimplemented()
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
for idx in 0..<self.count {
aCoder.encode(_SwiftValue.store(self.object(at: idx)), forKey:"NS.object.\(idx)")
}
}

public required convenience init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
var idx = 0
var objects : [AnyObject] = []
while aDecoder.containsValue(forKey: ("NS.object.\(idx)")) {
guard let object = aDecoder.decodeObject(forKey: "NS.object.\(idx)") else {
return nil
}
objects.append(object as! NSObject)
idx += 1
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
var idx = 0
var objects : [AnyObject] = []
while aDecoder.containsValue(forKey: ("NS.object.\(idx)")) {
guard let object = aDecoder.decodeObject(forKey: "NS.object.\(idx)") else {
return nil
}
self.init(array: objects)
} else {
NSUnimplemented()
objects.append(object as! NSObject)
idx += 1
}
self.init(array: objects)
}

open var count: Int {
Expand Down
Loading