Skip to content

[NSAffineTransform] NSCoding implementation #815

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 4 commits into from
Feb 2, 2017
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
2 changes: 1 addition & 1 deletion Docs/Status.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ There is no _Complete_ status for test coverage because there are always additio
| `NSGeometry` | Mostly Complete | Substantial | `NSIntegralRectWithOptions` `.AlignRectFlipped` support remains unimplemented |
| `CGFloat` | Complete | Substantial | |
| `AffineTransform` | Complete | None | |
| `NSAffineTransform` | Mostly Complete | Substnatial | `NSCoding` remains unimplemented |
| `NSAffineTransform` | Complete | Substnatial | |
| `NSNumber` | Complete | Incomplete | |
| `NSConcreteValue` | N/A | N/A | For internal use only |
| `NSSpecialValue` | N/A | N/A | For internal use only |
Expand Down
52 changes: 50 additions & 2 deletions Foundation/NSAffineTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,66 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {

open func encode(with aCoder: NSCoder) {
NSUnimplemented()
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

let array = [
Float(transformStruct.m11),
Float(transformStruct.m12),
Float(transformStruct.m21),
Float(transformStruct.m22),
Float(transformStruct.tX),
Float(transformStruct.tY),
]

array.withUnsafeBytes { pointer in
aCoder.encodeValue(ofObjCType: "[6f]", at: UnsafeRawPointer(pointer.baseAddress!))
}
}

open func copy(with zone: NSZone? = nil) -> Any {
return NSAffineTransform(transform: self)
}

// Necessary because `NSObject.copy()` returns `self`.
open override func copy() -> Any {
return copy(with: nil)
}

public required init?(coder aDecoder: NSCoder) {
NSUnimplemented()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

let pointer = UnsafeMutableRawPointer.allocate(bytes: MemoryLayout<Float>.stride * 6, alignedTo: 1)
defer {
pointer.deallocate(bytes: MemoryLayout<Float>.stride * 6, alignedTo: 1)
}
aDecoder.decodeValue(ofObjCType: "[6f]", at: pointer)

let floatPointer = pointer.bindMemory(to: Float.self, capacity: 6)
let m11 = floatPointer[0]
let m12 = floatPointer[1]
let m21 = floatPointer[2]
let m22 = floatPointer[3]
let tX = floatPointer[4]
let tY = floatPointer[5]

self.transformStruct = AffineTransform(m11: CGFloat(m11), m12: CGFloat(m12),
m21: CGFloat(m21), m22: CGFloat(m22),
tX: CGFloat(tX), tY: CGFloat(tY))
}

open override func isEqual(_ object: Any?) -> Bool {
if let other = object as? NSAffineTransform {
return other === self
|| (other.transformStruct == self.transformStruct)
}

return false
}

public static var supportsSecureCoding: Bool {
return true
}
Expand Down
17 changes: 17 additions & 0 deletions TestFoundation/TestNSAffineTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class TestNSAffineTransform : XCTestCase {
("test_AppendTransform", test_AppendTransform),
("test_PrependTransform", test_PrependTransform),
("test_TransformComposition", test_TransformComposition),
("test_Equal", test_Equal),
("test_NSCoding", test_NSCoding),
]
}

Expand Down Expand Up @@ -343,6 +345,21 @@ class TestNSAffineTransform : XCTestCase {
XCTAssertEqual(ref.hashValue, val.hashValue)
}
}

func test_Equal() {
let transform = NSAffineTransform()
let transform1 = NSAffineTransform()

XCTAssertEqual(transform1, transform)
XCTAssertFalse(transform === transform1)
}

func test_NSCoding() {
let transformA = NSAffineTransform()
transformA.scale(by: 2)
let transformB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: transformA)) as! NSAffineTransform
XCTAssertEqual(transformA, transformB, "Archived then unarchived `NSAffineTransform` must be equal.")
}
}

extension NSAffineTransform {
Expand Down