Skip to content

Commit 01ffe57

Browse files
authored
Merge pull request #815 from naithar/NSAffineTransform
2 parents 26ac0e7 + 6867398 commit 01ffe57

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

Docs/Status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ There is no _Complete_ status for test coverage because there are always additio
226226
| `NSGeometry` | Mostly Complete | Substantial | `NSIntegralRectWithOptions` `.AlignRectFlipped` support remains unimplemented |
227227
| `CGFloat` | Complete | Substantial | |
228228
| `AffineTransform` | Complete | None | |
229-
| `NSAffineTransform` | Mostly Complete | Substnatial | `NSCoding` remains unimplemented |
229+
| `NSAffineTransform` | Complete | Substnatial | |
230230
| `NSNumber` | Complete | Incomplete | |
231231
| `NSConcreteValue` | N/A | N/A | For internal use only |
232232
| `NSSpecialValue` | N/A | N/A | For internal use only |

Foundation/NSAffineTransform.swift

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,18 +282,66 @@ public struct AffineTransform : ReferenceConvertible, Hashable, CustomStringConv
282282
open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
283283

284284
open func encode(with aCoder: NSCoder) {
285-
NSUnimplemented()
285+
guard aCoder.allowsKeyedCoding else {
286+
preconditionFailure("Unkeyed coding is unsupported.")
287+
}
288+
289+
let array = [
290+
Float(transformStruct.m11),
291+
Float(transformStruct.m12),
292+
Float(transformStruct.m21),
293+
Float(transformStruct.m22),
294+
Float(transformStruct.tX),
295+
Float(transformStruct.tY),
296+
]
297+
298+
array.withUnsafeBytes { pointer in
299+
aCoder.encodeValue(ofObjCType: "[6f]", at: UnsafeRawPointer(pointer.baseAddress!))
300+
}
286301
}
302+
287303
open func copy(with zone: NSZone? = nil) -> Any {
288304
return NSAffineTransform(transform: self)
289305
}
306+
290307
// Necessary because `NSObject.copy()` returns `self`.
291308
open override func copy() -> Any {
292309
return copy(with: nil)
293310
}
311+
294312
public required init?(coder aDecoder: NSCoder) {
295-
NSUnimplemented()
313+
guard aDecoder.allowsKeyedCoding else {
314+
preconditionFailure("Unkeyed coding is unsupported.")
315+
}
316+
317+
let pointer = UnsafeMutableRawPointer.allocate(bytes: MemoryLayout<Float>.stride * 6, alignedTo: 1)
318+
defer {
319+
pointer.deallocate(bytes: MemoryLayout<Float>.stride * 6, alignedTo: 1)
320+
}
321+
aDecoder.decodeValue(ofObjCType: "[6f]", at: pointer)
322+
323+
let floatPointer = pointer.bindMemory(to: Float.self, capacity: 6)
324+
let m11 = floatPointer[0]
325+
let m12 = floatPointer[1]
326+
let m21 = floatPointer[2]
327+
let m22 = floatPointer[3]
328+
let tX = floatPointer[4]
329+
let tY = floatPointer[5]
330+
331+
self.transformStruct = AffineTransform(m11: CGFloat(m11), m12: CGFloat(m12),
332+
m21: CGFloat(m21), m22: CGFloat(m22),
333+
tX: CGFloat(tX), tY: CGFloat(tY))
334+
}
335+
336+
open override func isEqual(_ object: Any?) -> Bool {
337+
if let other = object as? NSAffineTransform {
338+
return other === self
339+
|| (other.transformStruct == self.transformStruct)
340+
}
341+
342+
return false
296343
}
344+
297345
public static var supportsSecureCoding: Bool {
298346
return true
299347
}

TestFoundation/TestNSAffineTransform.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class TestNSAffineTransform : XCTestCase {
3838
("test_AppendTransform", test_AppendTransform),
3939
("test_PrependTransform", test_PrependTransform),
4040
("test_TransformComposition", test_TransformComposition),
41+
("test_Equal", test_Equal),
42+
("test_NSCoding", test_NSCoding),
4143
]
4244
}
4345

@@ -343,6 +345,21 @@ class TestNSAffineTransform : XCTestCase {
343345
XCTAssertEqual(ref.hashValue, val.hashValue)
344346
}
345347
}
348+
349+
func test_Equal() {
350+
let transform = NSAffineTransform()
351+
let transform1 = NSAffineTransform()
352+
353+
XCTAssertEqual(transform1, transform)
354+
XCTAssertFalse(transform === transform1)
355+
}
356+
357+
func test_NSCoding() {
358+
let transformA = NSAffineTransform()
359+
transformA.scale(by: 2)
360+
let transformB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: transformA)) as! NSAffineTransform
361+
XCTAssertEqual(transformA, transformB, "Archived then unarchived `NSAffineTransform` must be equal.")
362+
}
346363
}
347364

348365
extension NSAffineTransform {

0 commit comments

Comments
 (0)