如何在 swift 中使用 UnsafeMutablePointer<T?> 3
How to use UnsafeMutablePointer<T?> in swift 3
我是 Swift 的新手,我不明白如何使用 UnsafeMutablePointer<T?>
。谁能帮帮我?
struct TestStruct {
var a = 0
}
func foo(ptr: UnsafeMutablePointer<TestStruct?>?) {
ptr?.pointee?.a = 123 // pointee is nil, but why?
}
func bar(ptr: TestStruct?) {
print("hello: \(ptr?.a)") // hello: nil
}
var ref = UnsafeMutablePointer<TestStruct?>.allocate(capacity: 1)
foo(ptr: ref)
bar(ptr: ref.pointee)
ref.deallocate(capacity: 1)
我正在为 TestStruct
分配内存,但是当我将 ref
传递给 foo
时,pointee
指向 nil
。如果我将 TestStruct
( UnsafeMutablePointer<TestStruct>
) 设为非可选 - 一切都会很好 - 条形打印 hello: 123
.
更新:
感谢@MartinR 和@Hamish!工作代码:
struct TestStruct {
var a = 0
}
func foo(ptr: UnsafeMutablePointer<TestStruct?>?) {
ptr?.pointee?.a = 123
}
func bar(ptr: TestStruct?) {
print("hello: \(ptr?.a)")
}
var ref = UnsafeMutablePointer<TestStruct?>.allocate(capacity: 1)
ref.initialize(to: TestStruct())
foo(ptr: ref)
bar(ptr: ref.pointee)
ref.deinitialize()
ref.deallocate(capacity: 1)
您的程序的行为未定义。
allocate()
returns 未初始化内存:
/// Allocates and points at uninitialized aligned memory for `count`
/// instances of `Pointee`.
///
/// - Postcondition: The pointee is allocated, but not initialized.
public static func allocate(capacity count: Int) -> UnsafeMutablePointer<Pointee>
使用前必须先初始化
(并在释放内存之前取消初始化):
let ref = UnsafeMutablePointer<TestStruct?>.allocate(capacity: 1)
ref.initialize(to: TestStruct())
foo(ptr: ref)
bar(ptr: ref.pointee)
ref.deinitialize()
ref.deallocate(capacity: 1)
这是另一个实际没有初始化内存的例子
导致崩溃:
class TestClass {
init() { print("init") }
deinit { print("deinit") }
}
let ptr = UnsafeMutablePointer<TestClass>.allocate(capacity: 1)
ptr.pointee = TestClass()
赋值语句期望ptr.pointee
处于初始化状态
并指向一个 TestClass
实例。由于 ARC(自动
引用计数),之前指向的对象 已释放
分配新值时。如果 ptr.pointee
是
未初始化。
我是 Swift 的新手,我不明白如何使用 UnsafeMutablePointer<T?>
。谁能帮帮我?
struct TestStruct {
var a = 0
}
func foo(ptr: UnsafeMutablePointer<TestStruct?>?) {
ptr?.pointee?.a = 123 // pointee is nil, but why?
}
func bar(ptr: TestStruct?) {
print("hello: \(ptr?.a)") // hello: nil
}
var ref = UnsafeMutablePointer<TestStruct?>.allocate(capacity: 1)
foo(ptr: ref)
bar(ptr: ref.pointee)
ref.deallocate(capacity: 1)
我正在为 TestStruct
分配内存,但是当我将 ref
传递给 foo
时,pointee
指向 nil
。如果我将 TestStruct
( UnsafeMutablePointer<TestStruct>
) 设为非可选 - 一切都会很好 - 条形打印 hello: 123
.
更新:
感谢@MartinR 和@Hamish!工作代码:
struct TestStruct {
var a = 0
}
func foo(ptr: UnsafeMutablePointer<TestStruct?>?) {
ptr?.pointee?.a = 123
}
func bar(ptr: TestStruct?) {
print("hello: \(ptr?.a)")
}
var ref = UnsafeMutablePointer<TestStruct?>.allocate(capacity: 1)
ref.initialize(to: TestStruct())
foo(ptr: ref)
bar(ptr: ref.pointee)
ref.deinitialize()
ref.deallocate(capacity: 1)
您的程序的行为未定义。
allocate()
returns 未初始化内存:
/// Allocates and points at uninitialized aligned memory for `count`
/// instances of `Pointee`.
///
/// - Postcondition: The pointee is allocated, but not initialized.
public static func allocate(capacity count: Int) -> UnsafeMutablePointer<Pointee>
使用前必须先初始化 (并在释放内存之前取消初始化):
let ref = UnsafeMutablePointer<TestStruct?>.allocate(capacity: 1)
ref.initialize(to: TestStruct())
foo(ptr: ref)
bar(ptr: ref.pointee)
ref.deinitialize()
ref.deallocate(capacity: 1)
这是另一个实际没有初始化内存的例子 导致崩溃:
class TestClass {
init() { print("init") }
deinit { print("deinit") }
}
let ptr = UnsafeMutablePointer<TestClass>.allocate(capacity: 1)
ptr.pointee = TestClass()
赋值语句期望ptr.pointee
处于初始化状态
并指向一个 TestClass
实例。由于 ARC(自动
引用计数),之前指向的对象 已释放
分配新值时。如果 ptr.pointee
是
未初始化。