Skip to content

Migrate Swift 3 Darwin for Coder/Archival #561

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
Aug 18, 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
12 changes: 6 additions & 6 deletions Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {

public convenience required init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
guard let calendarIdentifier = aDecoder.decodeObjectOfClass(NSString.self, forKey: "NS.identifier") else {
guard let calendarIdentifier = aDecoder.decodeObject(of: NSString.self, forKey: "NS.identifier") else {
return nil
}

self.init(identifier: NSCalendar.Identifier.init(rawValue: calendarIdentifier._swiftObject))

if let timeZone = aDecoder.decodeObjectOfClass(NSTimeZone.self, forKey: "NS.timezone") {
if let timeZone = aDecoder.decodeObject(of: NSTimeZone.self, forKey: "NS.timezone") {
self.timeZone = timeZone._swiftObject
}
if let locale = aDecoder.decodeObjectOfClass(NSLocale.self, forKey: "NS.locale") {
if let locale = aDecoder.decodeObject(of: NSLocale.self, forKey: "NS.locale") {
self.locale = locale._swiftObject
}
self.firstWeekday = aDecoder.decodeInteger(forKey: "NS.firstwkdy")
self.minimumDaysInFirstWeek = aDecoder.decodeInteger(forKey: "NS.mindays")
if let startDate = aDecoder.decodeObjectOfClass(NSDate.self, forKey: "NS.gstartdate") {
if let startDate = aDecoder.decodeObject(of: NSDate.self, forKey: "NS.gstartdate") {
self._startDate = startDate._swiftObject
}
} else {
Expand Down Expand Up @@ -1385,8 +1385,8 @@ open class NSDateComponents : NSObject, NSCopying, NSSecureCoding {
self.weekday = aDecoder.decodeInteger(forKey: "NS.weekday")
self.weekdayOrdinal = aDecoder.decodeInteger(forKey: "NS.weekdayOrdinal")
self.isLeapMonth = aDecoder.decodeBool(forKey: "NS.isLeapMonth")
self.calendar = aDecoder.decodeObjectOfClass(NSCalendar.self, forKey: "NS.calendar")?._swiftObject
self.timeZone = aDecoder.decodeObjectOfClass(NSTimeZone.self, forKey: "NS.timezone")?._swiftObject
self.calendar = aDecoder.decodeObject(of: NSCalendar.self, forKey: "NS.calendar")?._swiftObject
self.timeZone = aDecoder.decodeObject(of: NSTimeZone.self, forKey: "NS.timezone")?._swiftObject
} else {
NSUnimplemented()
}
Expand Down
58 changes: 38 additions & 20 deletions Foundation/NSCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

extension NSCoder {
/*!
Describes the action an NSCoder should take when it encounters decode failures (e.g. corrupt data) for non-TopLevel decodes. Darwin platfrom supports exceptions here, and there may be other approaches supported in the future, so its included for completeness.
*/
public enum DecodingFailurePolicy : Int {
case setErrorAndReturn
}
}


public protocol NSCoding {
func encode(with aCoder: NSCoder)
init?(coder aDecoder: NSCoder)
Expand All @@ -30,23 +40,23 @@ open class NSCoder : NSObject {
NSRequiresConcreteImplementation()
}

open func encodeDataObject(_ data: Data) {
open func encode(_ data: Data) {
NSRequiresConcreteImplementation()
}

open func decodeValue(ofObjCType type: UnsafePointer<Int8>, at data: UnsafeMutableRawPointer) {
NSRequiresConcreteImplementation()
}

open func decodeDataObject() -> Data? {
open func decodeData() -> Data? {
NSRequiresConcreteImplementation()
}

open func version(forClassName className: String) -> Int {
NSRequiresConcreteImplementation()
}

open func decodeObjectOfClass<DecodedObjectType : NSCoding>(_ cls: DecodedObjectType.Type, forKey key: String) -> DecodedObjectType? where DecodedObjectType : NSObject {
open func decodeObject<DecodedObjectType: NSCoding>(of cls: DecodedObjectType.Type, forKey key: String) -> DecodedObjectType? where DecodedObjectType: NSObject {
NSUnimplemented()
}

Expand All @@ -60,20 +70,19 @@ open class NSCoder : NSObject {
classes is an array of Classes, not a NSSet. This is because AnyClass cannot
be casted to NSObject, nor is it Hashable.
*/
/// - Experiment: This is a draft API currently under consideration for official import into Foundation
open func decodeObjectOfClasses(_ classes: [AnyClass], forKey key: String) -> Any? {
open func decodeObject(of classes: [AnyClass]?, forKey key: String) -> Any? {
NSUnimplemented()
}

open func decodeTopLevelObject() throws -> Any? {
NSUnimplemented()
}

open func decodeTopLevelObjectForKey(_ key: String) throws -> Any? {
open func decodeTopLevelObject(forKey key: String) throws -> Any? {
NSUnimplemented()
}

open func decodeTopLevelObjectOfClass<DecodedObjectType : NSCoding>(_ cls: DecodedObjectType.Type, forKey key: String) throws -> DecodedObjectType? where DecodedObjectType : NSObject {
open func decodeTopLevelObject<DecodedObjectType: NSCoding>(of cls: DecodedObjectType.Type, forKey key: String) throws -> DecodedObjectType? where DecodedObjectType: NSObject {
NSUnimplemented()
}

Expand All @@ -87,16 +96,10 @@ open class NSCoder : NSObject {
classes is an array of Classes, not a NSSet. This is because AnyClass cannot
be casted to NSObject, nor is it Hashable.
*/
/// - Experiment: This is a draft API currently under consideration for official import into Foundation
open func decodeTopLevelObjectOfClasses(_ classes: [AnyClass], forKey key: String) throws -> Any? {
open func decodeTopLevelObject(of classes: [AnyClass], forKey key: String) throws -> Any? {
NSUnimplemented()
}

internal var error: NSError? {
return nil
}


open func encode(_ object: Any?) {
var object = object
withUnsafePointer(to: &object) { (ptr: UnsafePointer<Any?>) -> Void in
Expand Down Expand Up @@ -227,6 +230,12 @@ open class NSCoder : NSObject {
NSRequiresConcreteImplementation()
}

// NOTE: this equivalent to the decodeIntForKey: in Objective-C implementation
open func decodeCInt(forKey key: String) -> Int32 {

NSRequiresConcreteImplementation()
}

open func decodeInt32(forKey key: String) -> Int32 {
NSRequiresConcreteImplementation()
}
Expand Down Expand Up @@ -282,12 +291,21 @@ open class NSCoder : NSObject {
NSUnimplemented()
}

open func failWithError(_ error: NSError) {
if let debugDescription = error.userInfo["NSDebugDescription"] {
NSLog("*** NSKeyedUnarchiver.init: \(debugDescription)")
} else {
NSLog("*** NSKeyedUnarchiver.init: decoding error")
}
open func failWithError(_ error: Error) {
NSUnimplemented()
// NOTE: disabled for now due to bridging uncertainty
// if let debugDescription = error.userInfo["NSDebugDescription"] {
// NSLog("*** NSKeyedUnarchiver.init: \(debugDescription)")
// } else {
// NSLog("*** NSKeyedUnarchiver.init: decoding error")
// }
}

open var decodingFailurePolicy: NSCoder.DecodingFailurePolicy {
return .setErrorAndReturn
}
open var error: Error? {
NSRequiresConcreteImplementation()
}

internal func _decodeArrayOfObjectsForKey(_ key: String) -> [Any] {
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {

public required convenience init?(coder aDecoder: NSCoder) {
if !aDecoder.allowsKeyedCoding {
if let data = aDecoder.decodeDataObject() {
if let data = aDecoder.decodeData() {
self.init(data: data)
} else {
return nil
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,11 @@ open class NSDateInterval : NSObject, NSCopying, NSSecureCoding {

public required convenience init?(coder: NSCoder) {
precondition(coder.allowsKeyedCoding)
guard let start = coder.decodeObjectOfClass(NSDate.self, forKey: "NS.startDate") else {
guard let start = coder.decodeObject(of: NSDate.self, forKey: "NS.startDate") else {
coder.failWithError(NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.CoderValueNotFoundError.rawValue, userInfo: nil))
return nil
}
guard let end = coder.decodeObjectOfClass(NSDate.self, forKey: "NS.startDate") else {
guard let end = coder.decodeObject(of: NSDate.self, forKey: "NS.startDate") else {
coder.failWithError(NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.CoderValueNotFoundError.rawValue, userInfo: nil))
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
public required init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
_code = aDecoder.decodeInteger(forKey: "NSCode")
_domain = aDecoder.decodeObjectOfClass(NSString.self, forKey: "NSDomain")!._swiftObject
if let info = aDecoder.decodeObjectOfClasses([NSSet.self, NSDictionary.self, NSArray.self, NSString.self, NSNumber.self, NSData.self, NSURL.self], forKey: "NSUserInfo") as? NSDictionary {
_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([]) {
Expand Down
6 changes: 3 additions & 3 deletions Foundation/NSGeometry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -926,23 +926,23 @@ extension NSCoder {
}

public func decodePointForKey(_ key: String) -> NSPoint {
if let string = self.decodeObjectOfClass(NSString.self, forKey: key) {
if let string = self.decodeObject(of: NSString.self, forKey: key) {
return NSPointFromString(String._unconditionallyBridgeFromObjectiveC(string))
} else {
return NSPoint()
}
}

public func decodeSizeForKey(_ key: String) -> NSSize {
if let string = self.decodeObjectOfClass(NSString.self, forKey: key) {
if let string = self.decodeObject(of: NSString.self, forKey: key) {
return NSSizeFromString(String._unconditionallyBridgeFromObjectiveC(string))
} else {
return NSSize()
}
}

public func decodeRectForKey(_ key: String) -> NSRect {
if let string = self.decodeObjectOfClass(NSString.self, forKey: key) {
if let string = self.decodeObject(of: NSString.self, forKey: key) {
return NSRectFromString(String._unconditionallyBridgeFromObjectiveC(string))
} else {
return NSRect()
Expand Down
12 changes: 11 additions & 1 deletion Foundation/NSKeyedArchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ open class NSKeyedArchiver : NSCoder {
return finishedEncoding
}

public override init() {
NSUnimplemented()
}

private init(output: AnyObject) {
self._stream = output
super.init()
Expand Down Expand Up @@ -195,6 +199,12 @@ open class NSKeyedArchiver : NSCoder {
private func _writeBinaryData(_ plist : NSDictionary) -> Bool {
return __CFBinaryPlistWriteToStream(plist, self._stream) > 0
}


/// If encoding has not yet finished, then invoking this property will call finishEncoding and return the data. If you initialized the keyed archiver with a specific mutable data instance, then it will be returned from this property after finishEncoding is called.
open var encodedData: Data {
NSUnimplemented()
}

open func finishEncoding() {
if _flags.contains(ArchiverFlags.finishedEncoding) {
Expand Down Expand Up @@ -746,7 +756,7 @@ open class NSKeyedArchiver : NSCoder {
_encodeValue(NSNumber(value: intv), forKey: key)
}

open override func encodeDataObject(_ data: Data) {
open override func encode(_ data: Data) {
// this encodes as a reference to an NSData object rather than encoding inline
encode(data._nsObject)
}
Expand Down
Loading