弱和分配属性之间的差异? [英] differences between weak and assign property?

查看:80
本文介绍了弱和分配属性之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎没有问题。

1)其中,分配属性将占用内存,因为我们不需要释放以减​​少引用次数?

1)where assign property will take memory as we dont need to release for reducing reference count?

2)自动零参考和非自动零参考有什么区别?它是如何工作的?如何记忆?

2)what is the difference between auto zero reference and non-auto zero reference?how does it work? how will take memory?

推荐答案

适用于对象(它们有引用计数和所有东西),但弱引用不会增加refcount。但是一旦对象被释放(从代码中的任何地方),对该对象的任何弱引用都设置为 nil 。这非常有用,因为如果你只使用强引用和弱引用,你就不能得到一个无效的指针(指向已经解除分配的对象的指针)。

weak applies to objects (they have reference counts and all the stuff), but weak references don't increase refcount. But once the object is deallocated (from anywhere in the code), any weak reference to that object is set to nil. This is extremely useful, because if you use only strong and weak references, you can't end up with an invalid pointer (pointer to an already deallocated object).

assign 与引用完全没有关系,它通常用于int,float和其他非对象类型。你当然可以为这样的变量分配一个对象引用,但是如果对象被释放,你仍然会有一个指向它内存的指针(现在是垃圾,当你使用它时会伤到你)。

assign does absolutely nothing with the reference, it is usually used for ints, floats and other non-object types. You can of course assign an object reference to such a variable, but if the object is deallocated, you will still have a pointer to it's memory (which is garbage now, and will hurt you when you use it).

您对内存使用的担忧很奇怪 - 引用不占用内存,对象可以。但是,如果要使用它,则无法释放对象。初学者的简单规则是:对于对象,尽可能使用强引用。当您有理由不使用强引用时,请使用weak(通常用于委托和数据源)。对于原始类型(int,float,CGRect,...),使用assign,因为它们不是对象。

Your concerns about "memory use" are weird - references don't take memory, object do. But you can't deallocate an object if you are going to use it. The simple rule for beginners is: for objects, use strong references whenever you can. When you have a reason not to use strong reference, use weak (usually for delegates and datasources). For primitive types (int, float, CGRect, ...) use assign, because they are not objects.

这篇关于弱和分配属性之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆