I'd like to store an array of weak references in Swift. The array itself should not be a weak reference - its elements should be. I think Cocoa NSPointerArray
offers a non-typesafe version of this.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- “Zero out” sensitive String data in Swift
- back button text does not change
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
Since
NSPointerArray
already handles most of this automatically, I solved the problem by making a type-safe wrapper for it, which avoids a lot of the boilerplate in other answers:Example usage:
It's more work up front, but the usage in the rest of your code is much cleaner IMO. If you want to make it more array-like, you can even implement subscripting, make it a
SequenceType
, etc. (but my project only needsappend
andforEach
so I don't have the exact code on hand).You can use the NSHashTable with weakObjectsHashTable.
NSHashTable.weakObjectsHashTable()
For Swift 3:
NSHashTable.weakObjects()
NSHashTable Class Reference
The existing example of the WeakContainer is helpful, but it doesn't really help one use weak references in existing swift containers such as Lists and Dictionaries.
If you want to use List methods such as contains, then the WeakContainer will need to implement Equatable. So I added the code to allow the WeakContainer to be equatable.
In case you wanted to use the WeakContainer in dictionaries, I also made it hashable so it can be used as dictionary keys.
I also renamed it to WeakObject to stress that this is only for class types and to differentiate it from the WeakContainer examples:
This allows you to do some cool stuff like use a Dictionary of weak references:
You could create wrapper around
Array
. Or use this library https://github.com/NickRybalko/WeakPointerArraylet array = WeakPointerArray<AnyObject>()
It is type safe.This is not my solution. I found it on the Apple Developer Forums.
@GoZoner has a good answer, but it crashes the Swift compiler.
Here's a version of a weak-object container doesn't crash the current released compiler.
You can then create an array of these containers:
I had the same idea to create weak container with generics.
As result I created wrapper for
NSHashTable
:Usage:
It's not the best solution, because
WeakSet
can be initialized with any type, and if this type doesn't conform toAnyObject
protocol then app will crash with detailed reason. But I don't see any better solution right now.Original solution was to define
WeakSet
in this way:But in this case
WeakSet
can't be initialized with protocol:Currently above code can't be compiled (Swift 2.1, Xcode 7.1).
That's why I dropped conforming to
AnyObject
and added additional guards withfatalError()
assertions.