Skip to content

Instantly share code, notes, and snippets.

@seka
Created April 27, 2020 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seka/6f1a79273638d34d5f5cfda6b0a87a37 to your computer and use it in GitHub Desktop.
Save seka/6f1a79273638d34d5f5cfda6b0a87a37 to your computer and use it in GitHub Desktop.
NSDataとCFDataの関係をメモ (Toll-Free Bridge)
import Foundation
var data: NSData = NSData.init(bytes: "Hello, World!", length: 13)
withUnsafePointer(to: data) {
print("value \(String(describing: data)) has address: \($0)")
}
var cfData: CFData = data
withUnsafePointer(to: cfData) {
print("value \(String(describing: cfData)) has address: \($0)")
}
var ptr = data.bytes.assumingMemoryBound(to: UInt8.self)
var cfDataRef: CFTypeRef = CFDataCreate(kCFAllocatorDefault, ptr, 13)
withUnsafePointer(to: cfDataRef) {
print("value \(String(describing: cfDataRef)) has address: \($0)")
}
/**
* 自分の環境だと
* str value {length = 13, bytes = 0x48656c6c6f2c20576f726c6421} has address: 0x00007ffeeaee9b10
* str value {length = 13, bytes = 0x48656c6c6f2c20576f726c6421} has address: 0x00007ffeeaee9b10
* str value {length = 13, bytes = 0x48656c6c6f2c20576f726c6421} has address: 0x00007ffeeaee9b10
*/
@seka
Copy link
Author

seka commented Apr 27, 2020

nsdata -> cfdata にキャストしても、メモリは同じアドレスを参照しているように見える

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment