- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我刚刚阅读了对 this 的公认优秀答案这个问题澄清了 Objective-C 中强指针和弱指针之间的概念差异,我仍在努力理解实际差异。我来自 C++ 背景,那里不存在这些概念,而且我无法弄清楚我应该在哪里使用一个与另一个。
有人可以提供一个使用 Objective-C 代码的实际示例,说明强指针和弱指针的不同用法吗?
最佳答案
一切都与保留计数有关。 ARC 是一种方便,避免开发人员担心手动保留和释放。从本质上讲,强变量会将保留计数增加 1,而弱变量则不会。
见下文:
@interface Test () {
NSString* strongVariable; // Instance variables default to strong
__weak NSString* weakVariable;
}
@end
// This has a retain count of 1, it has been allocated to memory by the user, and stored in a local variable (which is strong)
NSString* str = [[NSString alloc] initWithString:@"Test"];
// The string object will now have a retain count of 2, as the strong variable has increased its retain count
strongVariable = str;
// This weak variable does **not** increase the retain count, and as such it will still be 2
weakVariable = str;
// --
// Now, lets remove some references
// This will reduce the retain count to 1, as a strong variable has lost its link
strongVariable = nil;
// This will also reduce the retain count, as another strong variable has lost it's reference. This means the retain count is 0, and the object can now be considered to not exist
str = nil;
// What happens to weakVariable?
// Because it is referencing an object with a 0 retain count, the runtime will set the value of this variable automatically to nil (iOS 5 and above).
NSLog(@"%@", (weakVariable == nil) ? @"nil" : @"Not nil") // Will print "nil"
您不能遇到强变量引用保留计数为 0 的对象的情况,这违背了强变量的核心概念。值得注意的是,除了 __weak
之外,还有 __unsafe_unretained
。这就像一个弱变量,除了它不会在保留计数达到零后自动设置为 nil,这意味着它将包含一个指向内存随机部分的指针(如果你访问它会崩溃,你需要将它设为 nil)你自己)。这是因为 iOS 4 支持 ARC,而不是 __weak
。在大多数情况下,您会使用 __weak
。
以上描述只是实用的一瞥,你可以深入阅读很多using this documentation.
默认情况下一切都是__strong
。如果你想要弱,你需要使用__weak
。
当您在概念上不想拥有一个特定对象时,您通常会使用弱变量。虽然汽车拥有它的引擎和车轮,但它不会拥有司机。
Wheel* wheel;
Engine* engine;
__weak Driver* driver;
相反,司机将拥有汽车。
Car* car;
如果汽车拥有司机,我们就会有一个保留周期。汽车拥有司机,司机拥有汽车。如果我们要释放一个,另一个会怎样?保留循环的整个概念超过了这个问题的范围,但你可以 read about it here.
相同的概念适用于编程模式,例如委托(delegate)。对于 TableView , View Controller 将拥有 TableView ,但 TableView 不拥有 View Controller (用作委托(delegate))
//ViewController
UITableView* tableView;
tableView.delegate = self;
//UITableView
@property (nonatomic, weak) id<UITableViewDelegate> delegate;
__weak
的一个重要用途是在 block 内。没有它们,您将面临在没有意识到的情况下导致保留周期的严重风险。同样,这超出了这个问题的范围,但是 see here for more information .
在 TR1 中,您可以使用共享指针,这些允许您将堆分配的对象放在堆栈分配的对象中,它为我们管理内存。它通过使用引用计数来做到这一点。每次将共享指针传递给另一个变量时,引用计数都会增加。这类似于在 Obj-C 中分配给强变量。
关于objective-c - 深入了解 Objective-C 中强指针和弱指针的实际应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13623884/
我开始在 Ethereum blockchain 上了解如何开发智能合约以及如何写 web-script用于与智能合约交互(购买、销售、统计......)我得出了该怎么做的结论。我想知道我是否正确理解
我正在 UIView 中使用 CATransform3DMakeRotation,并且我正在尝试进行 45º,变换就像向后放置一样: 这是我拥有的“代码”,但显然没有这样做。 CATransform3
我目前正在测试 WebRTC 的功能,但我有一些脑逻辑问题。 WebRTC 究竟是什么? 我只读了“STUN”、“P2P”和其他...但是在技术方面什么是正确的 WebRTC(见下一个) 我需要什么
我在看 DelayedInit在 Scala in Depth ... 注释是我对代码的理解。 下面的 trait 接受一个非严格计算的参数(由于 => ),并返回 Unit .它的行为类似于构造函数
谁能给我指出一个用图片和简单的代码片段解释 WCF 的资源。我厌倦了谷歌搜索并在所有搜索结果中找到相同的“ABC”文章。 最佳答案 WCF 是一项非常复杂的技术,在我看来,它的文档记录非常少。启动和运
我期待以下 GetArgs.hs打印出传递给它的参数。 import System.Environment main = do args main 3 4 3 :39:1: Coul
private int vbo; private int ibo; vbo = glGenBuffers(); ibo = glGenBuffers(); glBindBuffer(GL_ARRAY_
我正在尝试一个 for 循环。我添加了一个 if 语句以在循环达到 30 时停止循环。 我见过i <= 10将运行 11 次,因为循环在达到 10 次时仍会运行。 如果有设置 i 的 if 语句,为什
我正在尝试了解 WSGI 的功能并需要一些帮助。 到目前为止,我知道它是一种服务器和应用程序之间的中间件,用于将不同的应用程序框架(位于服务器端)与应用程序连接,前提是相关框架具有 WSGI 适配器。
我是 Javascript 的新手,我正在尝试绕过 while 循环。我了解它们的目的,我想我了解它们的工作原理,但我在使用它们时遇到了麻烦。 我希望 while 值自身重复,直到两个随机数相互匹配。
我刚刚偶然发现Fabric并且文档并没有真正说明它是如何工作的。 我有根据的猜测是您需要在客户端和服务器端都安装它。 Python 代码存储在客户端,并在命令运行时通过 Fabric 的有线协议(pr
我想了解 ConditionalWeakTable .和有什么区别 class ClassA { static readonly ConditionalWeakTable OtherClass
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 5年前关闭。 Improve this questi
我还没有成功找到任何可以引导我理解 UIPickerView 和 UIPickerView 模型的好例子。有什么建议吗? 最佳答案 为什么不使用默认的 Apple 文档示例?这是来自苹果文档的名为 U
我在看foldM为了获得关于如何使用它的直觉。 foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a 在这个简单的例子中,我只返回 [Just
答案What are _mm_prefetch() locality hints?详细说明提示的含义。 我的问题是:我想要哪一个? 我正在处理一个被重复调用数十亿次的函数,其中包含一些 int 参数。
我一直在读这个article了解 gcroot 模板。我明白 gcroot provides handles into the garbage collected heap 然后 the handle
提供了一个用例: 流处理架构;事件进入 Kafka,然后由带有 MongoDB 接收器的作业进行处理。 数据库名称:myWebsite集合:用户 并且作业接收 users 集合中的 user 记录。
你好 我想更详细地了解 NFS 文件系统。我偶然发现了《NFS 图解》这本书,不幸的是它只能作为谷歌图书提供,所以有些页面丢失了。有人可能有另一个很好的资源,这将是在较低级别上了解 NFS 的良好开始
我无法理解这个问题,哪个更随机? rand() 或: rand() * rand() 我发现这是一个真正的脑筋急转弯,你能帮我吗? 编辑: 凭直觉,我知道数学答案是它们同样随机,但我忍不住认为,如果您
我是一名优秀的程序员,十分优秀!