swift 弱引用_Swift中解释的弱引用和无主引用之间的区别

swift 弱引用

It’s a good practice to know how memory management works in Swift. Swift uses Automatic Reference Counting (ARC) to manage the app’s memory automatically without writing any additional code. It deallocates the memory of instances when it is no longer used. But sometimes we have to provide more information to avoid memory leaks and deadlocks.

了解内存管理如何在Swift中工作是一个好习惯。 Swift使用自动引用计数 (ARC)来自动管理应用程序的内存,而无需编写任何其他代码。 当不再使用实例时,它将分配实例的内存。 但是有时我们必须提供更多信息,以避免内存泄漏死锁

ARC only applies on instance of classes because classes are reference type. It didn’t applies on structures and enumerations because they are value type.

ARC仅适用于类的实例,因为类是引用类型。 它不适用于结构和枚举,因为它们是值类型。

By default, class instance creates a strong reference with their methods, constants, variables, etc. means it will not be deallocated until strong reference remains. Sometimes it causes memory leaks and deadlocks, and we can avoid this by declaring weak reference or unowned reference.

默认情况下,类实例使用其方法,常量,变量等创建一个强引用 。这意味着在保留强引用之前,它不会被释放。 有时它会导致内存泄漏死锁 ,我们可以通过声明弱引用或未拥有的引用来避免这种情况。

Difference between weak reference and unowned reference

弱引用和非引用之间的区别

The weak reference is an optional type, which means weak reference will set to nil once the instance it refers to frees from memory.

弱引用是可选类型,这意味着一旦弱引用引用的实例从内存中释放出来,它将设置为nil

On the other hand, unowned reference is a non-optional type, it never will be set to nil and always have some value.

另一方面, 无主引用是非可选类型,它永远不会设置为nil并始终具有一些值

Weak References

参考文献薄弱

You can declare weak reference with the weak keyword before variable or property. The following example will explain how to use weak references.

您可以在变量或属性之前使用weak关键字声明弱引用 。 下面的示例将说明如何使用弱引用

Let’s implement class Animal with property name type of String and zoo which is an optional type.

让我们使用属性name类型为String和zoo是可选类型的class Animal来实现class Animal类。

class Animal {
let name: String
var zoo: Zoo? init(name: String) {
self.name = name
} deinit {
print("\(name) is deinitialized.")
}
}

Now, define another class Zoo with a property location type of String and animal as a weak reference which is an optional type.

现在,使用属性位置类型为String定义另一个类Zoo 动物作为弱参考 ,这是可选类型。

class Zoo {
let location: String
weak var animal: Animal?

init(location: String) {
self.location = location
} deinit {
print("Zoo at \(location) is deinitialized.")
}
}

Define two variables tiger and logoaZoo of optional type and set to an instance. Then, link both instances together with the use of unwrap.

定义两个可选类型的变量tigerlogoaZoo并将其设置为实例 然后,使用展开将两个实例链接在一起。

var tiger: Animal? = Animal(name: "Amber")var lagoaZoo: Zoo? = Zoo(location: "11th St, Corona, NY 11368, United States")tiger!.zoo = lagoaZoo
lagoaZoo!.animal = tiger

When we set tiger variable to nil it breaks the strong reference to Animal instance and it deallocated from the memory.

当我们将Tiger变量设置为nil时,它将破坏对Animal实例的强引用,并且将其从内存中释放。

As there is no more strong reference to the Animal instance so animal property will also be set to nil.

由于不再强烈引用Animal实例,因此animal属性也将设置为nil。

tiger = nil

If we set logoaZoo to nil it breaks strong reference to Zoo instance and will also deallocated from the memory.

如果将logoaZoo设置为nil,它将破坏对Zoo实例的强引用,并且还将从内存中释放。

lagoaZoo = nil

Unowned References

无人参考

You can declare unowned reference with the unowned keyword before variable or property. The following example will explain how to use unowned references.

您可以在变量或属性之前使用无主关键字声明无主引用 。 以下示例将说明如何使用未拥有的引用

Define class Employee with the property name of type String and bank which is optional type.

定义类Employee ,其属性名称类型为String和bank (可选类型)。

class Employee {
let name: String
var bank: Bank? init(name: String) {
self.name = name
} deinit {
print("\(name) is deinitialized.")
}
}

Let’s define another class Bank with the property name of type String and employee which is unowned property (non-optional).

让我们定义另一个类Bank ,其属性名称类型为String,而employee则为非拥有属性(非可选)。

class Bank {
let name: String
unowned let employee: Employee init(name: String,employee: Employee) {
self.name = name
self.employee = employee
} deinit {
print("\(name) is deinitialized.")
}
}

Define variables ana of optional type and set to an instance. Then, assign Bank instance to Employee’s bank property.

定义可选类型的变量ana并将其设置为实例 然后,将银行实例分配给员工的银行财产。

var ana: Employee? = Employee(name: "Ana")
ana!.bank = Bank(name: "SPI Bank", employee: ana!)

When we set ana to nil, there is no strong reference with Employee instance and it will be deallocated.

当我们将ana设置为nil时, Employee实例没有强引用,它将被释放。

Because there is no strong reference with Bank instance it will also be deallocated from memory.

因为与Bank实例没有强引用,所以它也将从内存中释放。

ana = nil

Conclusion

结论

It’s useful to know how memory management works in Swift to break strong reference to instance and to avoid any memory leaks in the application.

了解内存管理如何在Swift中工作以打破对实例的强引用并避免应用程序中的任何内存泄漏非常有用。

To find newest tips and tricks about iOS development, Xcode and Swift please visit https://www.gurjit.co

要查找有关iOS开发,Xcode和Swift的最新提示和技巧,请访问https://www.gurjit.co

Thanks!

谢谢!

翻译自: https://levelup.gitconnected.com/difference-between-weak-and-unowned-references-explained-in-swift-24cb1d110650

swift 弱引用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值