Skip to content

Commit 2dd8f10

Browse files
committed
Migrate Swift 3 Darwin for Coder/Archival
1 parent 5871161 commit 2dd8f10

18 files changed

+130
-98
lines changed

Foundation/NSCalendar.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,21 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
135135

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

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

144-
if let timeZone = aDecoder.decodeObjectOfClass(NSTimeZone.self, forKey: "NS.timezone") {
144+
if let timeZone = aDecoder.decodeObject(of: NSTimeZone.self, forKey: "NS.timezone") {
145145
self.timeZone = timeZone._swiftObject
146146
}
147-
if let locale = aDecoder.decodeObjectOfClass(NSLocale.self, forKey: "NS.locale") {
147+
if let locale = aDecoder.decodeObject(of: NSLocale.self, forKey: "NS.locale") {
148148
self.locale = locale._swiftObject
149149
}
150150
self.firstWeekday = aDecoder.decodeInteger(forKey: "NS.firstwkdy")
151151
self.minimumDaysInFirstWeek = aDecoder.decodeInteger(forKey: "NS.mindays")
152-
if let startDate = aDecoder.decodeObjectOfClass(NSDate.self, forKey: "NS.gstartdate") {
152+
if let startDate = aDecoder.decodeObject(of: NSDate.self, forKey: "NS.gstartdate") {
153153
self._startDate = startDate._swiftObject
154154
}
155155
} else {
@@ -1385,8 +1385,8 @@ open class NSDateComponents : NSObject, NSCopying, NSSecureCoding {
13851385
self.weekday = aDecoder.decodeInteger(forKey: "NS.weekday")
13861386
self.weekdayOrdinal = aDecoder.decodeInteger(forKey: "NS.weekdayOrdinal")
13871387
self.isLeapMonth = aDecoder.decodeBool(forKey: "NS.isLeapMonth")
1388-
self.calendar = aDecoder.decodeObjectOfClass(NSCalendar.self, forKey: "NS.calendar")?._swiftObject
1389-
self.timeZone = aDecoder.decodeObjectOfClass(NSTimeZone.self, forKey: "NS.timezone")?._swiftObject
1388+
self.calendar = aDecoder.decodeObject(of: NSCalendar.self, forKey: "NS.calendar")?._swiftObject
1389+
self.timeZone = aDecoder.decodeObject(of: NSTimeZone.self, forKey: "NS.timezone")?._swiftObject
13901390
} else {
13911391
NSUnimplemented()
13921392
}

Foundation/NSCoder.swift

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
extension NSCoder {
11+
/*!
12+
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.
13+
*/
14+
public enum DecodingFailurePolicy : Int {
15+
case setErrorAndReturn
16+
}
17+
}
18+
19+
1020
public protocol NSCoding {
1121
func encode(with aCoder: NSCoder)
1222
init?(coder aDecoder: NSCoder)
@@ -30,23 +40,23 @@ open class NSCoder : NSObject {
3040
NSRequiresConcreteImplementation()
3141
}
3242

33-
open func encodeDataObject(_ data: Data) {
43+
open func encode(_ data: Data) {
3444
NSRequiresConcreteImplementation()
3545
}
3646

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

41-
open func decodeDataObject() -> Data? {
51+
open func decodeData() -> Data? {
4252
NSRequiresConcreteImplementation()
4353
}
4454

4555
open func version(forClassName className: String) -> Int {
4656
NSRequiresConcreteImplementation()
4757
}
4858

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

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

6877
open func decodeTopLevelObject() throws -> Any? {
6978
NSUnimplemented()
7079
}
7180

72-
open func decodeTopLevelObjectForKey(_ key: String) throws -> Any? {
81+
open func decodeTopLevelObject(forKey key: String) throws -> Any? {
7382
NSUnimplemented()
7483
}
7584

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

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

95-
internal var error: NSError? {
96-
return nil
97-
}
98-
99-
100103
open func encode(_ object: Any?) {
101104
var object = object
102105
withUnsafePointer(to: &object) { (ptr: UnsafePointer<Any?>) -> Void in
@@ -227,6 +230,12 @@ open class NSCoder : NSObject {
227230
NSRequiresConcreteImplementation()
228231
}
229232

233+
// NOTE: this equivalent to the decodeIntForKey: in Objective-C implementation
234+
open func decodeCInt(forKey key: String) -> Int32 {
235+
236+
NSRequiresConcreteImplementation()
237+
}
238+
230239
open func decodeInt32(forKey key: String) -> Int32 {
231240
NSRequiresConcreteImplementation()
232241
}
@@ -282,12 +291,21 @@ open class NSCoder : NSObject {
282291
NSUnimplemented()
283292
}
284293

285-
open func failWithError(_ error: NSError) {
286-
if let debugDescription = error.userInfo["NSDebugDescription"] {
287-
NSLog("*** NSKeyedUnarchiver.init: \(debugDescription)")
288-
} else {
289-
NSLog("*** NSKeyedUnarchiver.init: decoding error")
290-
}
294+
open func failWithError(_ error: Error) {
295+
NSUnimplemented()
296+
// NOTE: disabled for now due to bridging uncertainty
297+
// if let debugDescription = error.userInfo["NSDebugDescription"] {
298+
// NSLog("*** NSKeyedUnarchiver.init: \(debugDescription)")
299+
// } else {
300+
// NSLog("*** NSKeyedUnarchiver.init: decoding error")
301+
// }
302+
}
303+
304+
open var decodingFailurePolicy: NSCoder.DecodingFailurePolicy {
305+
return .setErrorAndReturn
306+
}
307+
open var error: Error? {
308+
NSRequiresConcreteImplementation()
291309
}
292310

293311
internal func _decodeArrayOfObjectsForKey(_ key: String) -> [Any] {

Foundation/NSData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
169169

170170
public required convenience init?(coder aDecoder: NSCoder) {
171171
if !aDecoder.allowsKeyedCoding {
172-
if let data = aDecoder.decodeDataObject() {
172+
if let data = aDecoder.decodeData() {
173173
self.init(data: data)
174174
} else {
175175
return nil

Foundation/NSDate.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ open class NSDateInterval : NSObject, NSCopying, NSSecureCoding {
278278

279279
public required convenience init?(coder: NSCoder) {
280280
precondition(coder.allowsKeyedCoding)
281-
guard let start = coder.decodeObjectOfClass(NSDate.self, forKey: "NS.startDate") else {
281+
guard let start = coder.decodeObject(of: NSDate.self, forKey: "NS.startDate") else {
282282
coder.failWithError(NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.CoderValueNotFoundError.rawValue, userInfo: nil))
283283
return nil
284284
}
285-
guard let end = coder.decodeObjectOfClass(NSDate.self, forKey: "NS.startDate") else {
285+
guard let end = coder.decodeObject(of: NSDate.self, forKey: "NS.startDate") else {
286286
coder.failWithError(NSError(domain: NSCocoaErrorDomain, code: NSCocoaError.CoderValueNotFoundError.rawValue, userInfo: nil))
287287
return nil
288288
}

Foundation/NSError.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
5555
public required init?(coder aDecoder: NSCoder) {
5656
if aDecoder.allowsKeyedCoding {
5757
_code = aDecoder.decodeInteger(forKey: "NSCode")
58-
_domain = aDecoder.decodeObjectOfClass(NSString.self, forKey: "NSDomain")!._swiftObject
59-
if let info = aDecoder.decodeObjectOfClasses([NSSet.self, NSDictionary.self, NSArray.self, NSString.self, NSNumber.self, NSData.self, NSURL.self], forKey: "NSUserInfo") as? NSDictionary {
58+
_domain = aDecoder.decodeObject(of: NSString.self, forKey: "NSDomain")!._swiftObject
59+
if let info = aDecoder.decodeObject(of: [NSSet.self, NSDictionary.self, NSArray.self, NSString.self, NSNumber.self, NSData.self, NSURL.self], forKey: "NSUserInfo") as? NSDictionary {
6060
var filteredUserInfo = [String : Any]()
6161
// user info must be filtered so that the keys are all strings
6262
info.enumerateKeysAndObjects([]) {

Foundation/NSGeometry.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -926,23 +926,23 @@ extension NSCoder {
926926
}
927927

928928
public func decodePointForKey(_ key: String) -> NSPoint {
929-
if let string = self.decodeObjectOfClass(NSString.self, forKey: key) {
929+
if let string = self.decodeObject(of: NSString.self, forKey: key) {
930930
return NSPointFromString(String._unconditionallyBridgeFromObjectiveC(string))
931931
} else {
932932
return NSPoint()
933933
}
934934
}
935935

936936
public func decodeSizeForKey(_ key: String) -> NSSize {
937-
if let string = self.decodeObjectOfClass(NSString.self, forKey: key) {
937+
if let string = self.decodeObject(of: NSString.self, forKey: key) {
938938
return NSSizeFromString(String._unconditionallyBridgeFromObjectiveC(string))
939939
} else {
940940
return NSSize()
941941
}
942942
}
943943

944944
public func decodeRectForKey(_ key: String) -> NSRect {
945-
if let string = self.decodeObjectOfClass(NSString.self, forKey: key) {
945+
if let string = self.decodeObject(of: NSString.self, forKey: key) {
946946
return NSRectFromString(String._unconditionallyBridgeFromObjectiveC(string))
947947
} else {
948948
return NSRect()

Foundation/NSKeyedArchiver.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ open class NSKeyedArchiver : NSCoder {
164164
return finishedEncoding
165165
}
166166

167+
public override init() {
168+
NSUnimplemented()
169+
}
170+
167171
private init(output: AnyObject) {
168172
self._stream = output
169173
super.init()
@@ -195,6 +199,12 @@ open class NSKeyedArchiver : NSCoder {
195199
private func _writeBinaryData(_ plist : NSDictionary) -> Bool {
196200
return __CFBinaryPlistWriteToStream(plist, self._stream) > 0
197201
}
202+
203+
204+
/// 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.
205+
open var encodedData: Data {
206+
NSUnimplemented()
207+
}
198208

199209
open func finishEncoding() {
200210
if _flags.contains(ArchiverFlags.finishedEncoding) {
@@ -746,7 +756,7 @@ open class NSKeyedArchiver : NSCoder {
746756
_encodeValue(NSNumber(value: intv), forKey: key)
747757
}
748758

749-
open override func encodeDataObject(_ data: Data) {
759+
open override func encode(_ data: Data) {
750760
// this encodes as a reference to an NSData object rather than encoding inline
751761
encode(data._nsObject)
752762
}

0 commit comments

Comments
 (0)