Skip to content

Commit 6867398

Browse files
committed
fix pointer encoding
1 parent 9ac5526 commit 6867398

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

Foundation/NSAffineTransform.swift

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -286,16 +286,18 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
286286
preconditionFailure("Unkeyed coding is unsupported.")
287287
}
288288

289-
let pointer = UnsafePointer<Float>([
289+
let array = [
290290
Float(transformStruct.m11),
291291
Float(transformStruct.m12),
292292
Float(transformStruct.m21),
293293
Float(transformStruct.m22),
294294
Float(transformStruct.tX),
295295
Float(transformStruct.tY),
296-
])
297-
298-
aCoder.encodeArray(ofObjCType: "[f]", count: 6, at: pointer)
296+
]
297+
298+
array.withUnsafeBytes { pointer in
299+
aCoder.encodeValue(ofObjCType: "[6f]", at: UnsafeRawPointer(pointer.baseAddress!))
300+
}
299301
}
300302

301303
open func copy(with zone: NSZone? = nil) -> Any {
@@ -312,20 +314,20 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
312314
preconditionFailure("Unkeyed coding is unsupported.")
313315
}
314316

315-
let pointer = UnsafeMutablePointer<Float>.allocate(capacity: 6)
317+
let pointer = UnsafeMutableRawPointer.allocate(bytes: MemoryLayout<Float>.stride * 6, alignedTo: 1)
316318
defer {
317-
pointer.deinitialize()
318-
pointer.deallocate(capacity: 6)
319+
pointer.deallocate(bytes: MemoryLayout<Float>.stride * 6, alignedTo: 1)
319320
}
320-
aDecoder.decodeArray(ofObjCType: "[f]", count: 6, at: pointer)
321-
322-
let m11 = pointer[0]
323-
let m12 = pointer[1]
324-
let m21 = pointer[2]
325-
let m22 = pointer[3]
326-
let tX = pointer[4]
327-
let tY = pointer[5]
321+
aDecoder.decodeValue(ofObjCType: "[6f]", at: pointer)
328322

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+
329331
self.transformStruct = AffineTransform(m11: CGFloat(m11), m12: CGFloat(m12),
330332
m21: CGFloat(m21), m22: CGFloat(m22),
331333
tX: CGFloat(tX), tY: CGFloat(tY))

0 commit comments

Comments
 (0)