gpt4 book ai didi

swift - 如何理解 Swift 中的引用类型或 "Pointer"?

转载 作者:搜寻专家 更新时间:2023-10-31 19:38:38 25 4
gpt4 key购买 nike

Pointers

If you have experience with C, C++, or Objective-C, you may know that these languages use pointers to refer to addresses in memory. A Swift constant or variable that refers to an instance of some reference type is similar to a pointer in C, but is not a direct pointer to an address in memory, and does not require you to write an asterisk (*) to indicate that you are creating a reference. Instead, these references are defined like any other constant or variable in Swift.

Here是对 Apple doc 中“Pointers”的解释。我们都知道在C++或者Objective-c中,指针直接指向内存中的地址。但是在 Swift 中,Apple 说 A Swift constant or variable that references an instance of some reference type is similar to a pointer in C, but is not a direct pointer to an address in memory, 这让我很困惑很多。我的问题是引用类型背后的场景是什么,如果不是直接指向内存中的地址,它指向哪里?

任何提示或线索将不胜感激。

最佳答案

类是引用类型,因此引用它们的变量 (var) 和常量 (let) 实际上是“幕后”的指针。文档试图说明的是,这对程序员是隐藏的。 Swift 中没有指针运算符。

关于swift - 如何理解 Swift 中的引用类型或 "Pointer"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33749583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com