Skip to content

Implement missing functions in Date #620

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 2 commits into from
Sep 8, 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
141 changes: 69 additions & 72 deletions Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,25 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
}

public convenience required init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
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.decodeObject(of: NSTimeZone.self, forKey: "NS.timezone") {
self.timeZone = timeZone._swiftObject
}
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.decodeObject(of: NSDate.self, forKey: "NS.gstartdate") {
self._startDate = startDate._swiftObject
}
} else {
NSUnimplemented()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
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.decodeObject(of: NSTimeZone.self, forKey: "NS.timezone") {
self.timeZone = timeZone._swiftObject
}
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.decodeObject(of: NSDate.self, forKey: "NS.gstartdate") {
self._startDate = startDate._swiftObject
}
}

Expand All @@ -169,16 +168,15 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding {
}

open func encode(with aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
aCoder.encode(self.calendarIdentifier.rawValue._bridgeToObjectiveC(), forKey: "NS.identifier")
aCoder.encode(self.timeZone._nsObject, forKey: "NS.timezone")
aCoder.encode(self.locale?._bridgeToObjectiveC(), forKey: "NS.locale")
aCoder.encode(self.firstWeekday, forKey: "NS.firstwkdy")
aCoder.encode(self.minimumDaysInFirstWeek, forKey: "NS.mindays")
aCoder.encode(self._startDate?._nsObject, forKey: "NS.gstartdate")
} else {
NSUnimplemented()
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.calendarIdentifier.rawValue._bridgeToObjectiveC(), forKey: "NS.identifier")
aCoder.encode(self.timeZone._nsObject, forKey: "NS.timezone")
aCoder.encode(self.locale?._bridgeToObjectiveC(), forKey: "NS.locale")
aCoder.encode(self.firstWeekday, forKey: "NS.firstwkdy")
aCoder.encode(self.minimumDaysInFirstWeek, forKey: "NS.mindays")
aCoder.encode(self._startDate?._nsObject, forKey: "NS.gstartdate")
}

static public var supportsSecureCoding: Bool {
Expand Down Expand Up @@ -1373,53 +1371,52 @@ open class NSDateComponents : NSObject, NSCopying, NSSecureCoding {
}

public convenience required init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
self.init()

self.era = aDecoder.decodeInteger(forKey: "NS.era")
self.year = aDecoder.decodeInteger(forKey: "NS.year")
self.quarter = aDecoder.decodeInteger(forKey: "NS.quarter")
self.month = aDecoder.decodeInteger(forKey: "NS.month")
self.day = aDecoder.decodeInteger(forKey: "NS.day")
self.hour = aDecoder.decodeInteger(forKey: "NS.hour")
self.minute = aDecoder.decodeInteger(forKey: "NS.minute")
self.second = aDecoder.decodeInteger(forKey: "NS.second")
self.nanosecond = aDecoder.decodeInteger(forKey: "NS.nanosec")
self.weekOfYear = aDecoder.decodeInteger(forKey: "NS.weekOfYear")
self.weekOfMonth = aDecoder.decodeInteger(forKey: "NS.weekOfMonth")
self.yearForWeekOfYear = aDecoder.decodeInteger(forKey: "NS.yearForWOY")
self.weekday = aDecoder.decodeInteger(forKey: "NS.weekday")
self.weekdayOrdinal = aDecoder.decodeInteger(forKey: "NS.weekdayOrdinal")
self.isLeapMonth = aDecoder.decodeBool(forKey: "NS.isLeapMonth")
self.calendar = aDecoder.decodeObject(of: NSCalendar.self, forKey: "NS.calendar")?._swiftObject
self.timeZone = aDecoder.decodeObject(of: NSTimeZone.self, forKey: "NS.timezone")?._swiftObject
} else {
NSUnimplemented()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

self.init()

self.era = aDecoder.decodeInteger(forKey: "NS.era")
self.year = aDecoder.decodeInteger(forKey: "NS.year")
self.quarter = aDecoder.decodeInteger(forKey: "NS.quarter")
self.month = aDecoder.decodeInteger(forKey: "NS.month")
self.day = aDecoder.decodeInteger(forKey: "NS.day")
self.hour = aDecoder.decodeInteger(forKey: "NS.hour")
self.minute = aDecoder.decodeInteger(forKey: "NS.minute")
self.second = aDecoder.decodeInteger(forKey: "NS.second")
self.nanosecond = aDecoder.decodeInteger(forKey: "NS.nanosec")
self.weekOfYear = aDecoder.decodeInteger(forKey: "NS.weekOfYear")
self.weekOfMonth = aDecoder.decodeInteger(forKey: "NS.weekOfMonth")
self.yearForWeekOfYear = aDecoder.decodeInteger(forKey: "NS.yearForWOY")
self.weekday = aDecoder.decodeInteger(forKey: "NS.weekday")
self.weekdayOrdinal = aDecoder.decodeInteger(forKey: "NS.weekdayOrdinal")
self.isLeapMonth = aDecoder.decodeBool(forKey: "NS.isLeapMonth")
self.calendar = aDecoder.decodeObject(of: NSCalendar.self, forKey: "NS.calendar")?._swiftObject
self.timeZone = aDecoder.decodeObject(of: NSTimeZone.self, forKey: "NS.timezone")?._swiftObject
}

open func encode(with aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
aCoder.encode(self.era, forKey: "NS.era")
aCoder.encode(self.year, forKey: "NS.year")
aCoder.encode(self.quarter, forKey: "NS.quarter")
aCoder.encode(self.month, forKey: "NS.month")
aCoder.encode(self.day, forKey: "NS.day")
aCoder.encode(self.hour, forKey: "NS.hour")
aCoder.encode(self.minute, forKey: "NS.minute")
aCoder.encode(self.second, forKey: "NS.second")
aCoder.encode(self.nanosecond, forKey: "NS.nanosec")
aCoder.encode(self.weekOfYear, forKey: "NS.weekOfYear")
aCoder.encode(self.weekOfMonth, forKey: "NS.weekOfMonth")
aCoder.encode(self.yearForWeekOfYear, forKey: "NS.yearForWOY")
aCoder.encode(self.weekday, forKey: "NS.weekday")
aCoder.encode(self.weekdayOrdinal, forKey: "NS.weekdayOrdinal")
aCoder.encode(self.isLeapMonth, forKey: "NS.isLeapMonth")
aCoder.encode(self.calendar?._nsObject, forKey: "NS.calendar")
aCoder.encode(self.timeZone?._nsObject, forKey: "NS.timezone")
} else {
NSUnimplemented()
}
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.era, forKey: "NS.era")
aCoder.encode(self.year, forKey: "NS.year")
aCoder.encode(self.quarter, forKey: "NS.quarter")
aCoder.encode(self.month, forKey: "NS.month")
aCoder.encode(self.day, forKey: "NS.day")
aCoder.encode(self.hour, forKey: "NS.hour")
aCoder.encode(self.minute, forKey: "NS.minute")
aCoder.encode(self.second, forKey: "NS.second")
aCoder.encode(self.nanosecond, forKey: "NS.nanosec")
aCoder.encode(self.weekOfYear, forKey: "NS.weekOfYear")
aCoder.encode(self.weekOfMonth, forKey: "NS.weekOfMonth")
aCoder.encode(self.yearForWeekOfYear, forKey: "NS.yearForWOY")
aCoder.encode(self.weekday, forKey: "NS.weekday")
aCoder.encode(self.weekdayOrdinal, forKey: "NS.weekdayOrdinal")
aCoder.encode(self.isLeapMonth, forKey: "NS.isLeapMonth")
aCoder.encode(self.calendar?._nsObject, forKey: "NS.calendar")
aCoder.encode(self.timeZone?._nsObject, forKey: "NS.timezone")
}

static public var supportsSecureCoding: Bool {
Expand Down
32 changes: 15 additions & 17 deletions Foundation/NSConcreteValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,28 +118,26 @@ internal class NSConcreteValue : NSValue {
}

convenience required init?(coder aDecoder: NSCoder) {
if !aDecoder.allowsKeyedCoding {
NSUnimplemented()
} else {
guard let type = aDecoder.decodeObject() as? NSString else {
return nil
}

let typep = type._swiftObject

// FIXME: This will result in reading garbage memory.
self.init(bytes: [], objCType: typep)
aDecoder.decodeValue(ofObjCType: typep, at: self.value)
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
guard let type = aDecoder.decodeObject() as? NSString else {
return nil
}

let typep = type._swiftObject

// FIXME: This will result in reading garbage memory.
self.init(bytes: [], objCType: typep)
aDecoder.decodeValue(ofObjCType: typep, at: self.value)
}

override func encode(with aCoder: NSCoder) {
if !aCoder.allowsKeyedCoding {
NSUnimplemented()
} else {
aCoder.encode(String(cString: self.objCType)._bridgeToObjectiveC())
aCoder.encodeValue(ofObjCType: self.objCType, at: self.value)
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(String(cString: self.objCType)._bridgeToObjectiveC())
aCoder.encodeValue(ofObjCType: self.objCType, at: self.value)
}

private var _size : Int {
Expand Down
26 changes: 11 additions & 15 deletions Foundation/NSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ open class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
return _timeIntervalSinceReferenceDate
}

open class var timeIntervalSinceReferenceDate: TimeInterval { NSUnimplemented() }
open class var timeIntervalSinceReferenceDate: TimeInterval {
return Date().timeIntervalSinceReferenceDate
}

public convenience override init() {
var tv = timeval()
Expand All @@ -69,18 +71,13 @@ open class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
}

public convenience required init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
let ti = aDecoder.decodeDouble(forKey: "NS.time")
self.init(timeIntervalSinceReferenceDate: ti)
} else {
var ti: TimeInterval = 0.0
withUnsafeMutablePointer(to: &ti) { (ptr: UnsafeMutablePointer<Double>) -> Void in
aDecoder.decodeValue(ofObjCType: "d", at: UnsafeMutableRawPointer(ptr))
}
self.init(timeIntervalSinceReferenceDate: ti)
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
let ti = aDecoder.decodeDouble(forKey: "NS.time")
self.init(timeIntervalSinceReferenceDate: ti)
}

open override func copy() -> Any {
return copy(with: nil)
}
Expand All @@ -94,11 +91,10 @@ open class NSDate : NSObject, NSCopying, NSSecureCoding, NSCoding {
}

open func encode(with aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
aCoder.encode(_timeIntervalSinceReferenceDate, forKey: "NS.time")
} else {
NSUnimplemented()
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(_timeIntervalSinceReferenceDate, forKey: "NS.time")
}

/**
Expand Down
46 changes: 21 additions & 25 deletions Foundation/NSGeometry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,25 +282,23 @@ extension NSEdgeInsets: NSSpecialValueCoding {
}

init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
self.top = aDecoder._decodeCGFloatForKey("NS.edgeval.top")
self.left = aDecoder._decodeCGFloatForKey("NS.edgeval.left")
self.bottom = aDecoder._decodeCGFloatForKey("NS.edgeval.bottom")
self.right = aDecoder._decodeCGFloatForKey("NS.edgeval.right")
} else {
NSUnimplemented()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
self.top = aDecoder._decodeCGFloatForKey("NS.edgeval.top")
self.left = aDecoder._decodeCGFloatForKey("NS.edgeval.left")
self.bottom = aDecoder._decodeCGFloatForKey("NS.edgeval.bottom")
self.right = aDecoder._decodeCGFloatForKey("NS.edgeval.right")
}

func encodeWithCoder(_ aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
aCoder._encodeCGFloat(self.top, forKey: "NS.edgeval.top")
aCoder._encodeCGFloat(self.left, forKey: "NS.edgeval.left")
aCoder._encodeCGFloat(self.bottom, forKey: "NS.edgeval.bottom")
aCoder._encodeCGFloat(self.right, forKey: "NS.edgeval.right")
} else {
NSUnimplemented()
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder._encodeCGFloat(self.top, forKey: "NS.edgeval.top")
aCoder._encodeCGFloat(self.left, forKey: "NS.edgeval.left")
aCoder._encodeCGFloat(self.bottom, forKey: "NS.edgeval.bottom")
aCoder._encodeCGFloat(self.right, forKey: "NS.edgeval.right")
}

static func objCType() -> String {
Expand Down Expand Up @@ -884,22 +882,20 @@ extension NSCoder {

private extension NSCoder {
func _encodeCGFloat(_ value: CGFloat) {
if let keyedArchiver = self as? NSKeyedArchiver {
keyedArchiver._encodeValue(NSNumber(value: value.native))
} else {
NSUnimplemented()
guard let keyedArchiver = self as? NSKeyedArchiver else {
preconditionFailure("Unkeyed coding is unsupported.")
}
keyedArchiver._encodeValue(NSNumber(value: value.native))
}

func _decodeCGFloat() -> CGFloat {
if let keyedUnarchiver = self as? NSKeyedUnarchiver {
guard let result : NSNumber = keyedUnarchiver._decodeValue() else {
return CGFloat(0.0)
}
return CGFloat(result.doubleValue)
} else {
NSUnimplemented()
guard let keyedUnarchiver = self as? NSKeyedUnarchiver else {
preconditionFailure("Unkeyed coding is unsupported.")
}
guard let result : NSNumber = keyedUnarchiver._decodeValue() else {
return CGFloat(0.0)
}
return CGFloat(result.doubleValue)
}

func _encodeCGFloat(_ value: CGFloat, forKey key: String) {
Expand Down
22 changes: 10 additions & 12 deletions Foundation/NSLocale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ open class NSLocale: NSObject, NSCopying, NSSecureCoding {
}

public required convenience init?(coder aDecoder: NSCoder) {
if aDecoder.allowsKeyedCoding {
guard let identifier = aDecoder.decodeObject(of: NSString.self, forKey: "NS.identifier") else {
return nil
}
self.init(localeIdentifier: String._unconditionallyBridgeFromObjectiveC(identifier))
} else {
NSUnimplemented()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
guard let identifier = aDecoder.decodeObject(of: NSString.self, forKey: "NS.identifier") else {
return nil
}
self.init(localeIdentifier: String._unconditionallyBridgeFromObjectiveC(identifier))
}

open override func copy() -> Any {
Expand All @@ -68,12 +67,11 @@ open class NSLocale: NSObject, NSCopying, NSSecureCoding {
}

open func encode(with aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
let identifier = CFLocaleGetIdentifier(self._cfObject)._nsObject
aCoder.encode(identifier, forKey: "NS.identifier")
} else {
NSUnimplemented()
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
let identifier = CFLocaleGetIdentifier(self._cfObject)._nsObject
aCoder.encode(identifier, forKey: "NS.identifier")
}

public static var supportsSecureCoding: Bool {
Expand Down
Loading