简体   繁体   中英

iOS Swift to Objective-C how to pass weak references to arrays?

I would like to understand how Swift and objective-c handle passing around nil references. I know that I cannot add nil to an Objective-C mutable array.

How nil is handled in the following scenario:

  • Swift class is a weak reference observer passed to Objective-C class
  • Objective-C saves the reference to Swift class in an array
  • Swift class is dealocated, it's weak reference turns to nil
  • Objective-C's now has an array with one of the elements pointing to nil

Does Objective-C class keep functioning, or does it crash when trying to iterate over the array and getting a nil reference?

Unless you are using NSPointerArray , when you add the object to an array the array will hold a strong reference to it, preventing the object from being released.

Only after the object is removed from the array, or the array itself is released will the object be eligible for deallocation.

This means that the array cannot be left holding a nil reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM