Array of weak in Swift

Array of weak references in Swift.
Create a generic wrapper as:

class Weak<T: AnyObject> {
weak var value : T?
init (value: T) {
self.value = value
}
}

Add instances of this class to your array.

class Stuff {}
var weaky: [We…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sergey Leschev

Array of weak references in Swift.
Create a generic wrapper as:

class Weak<T: AnyObject> {
  weak var value : T?
  init (value: T) {
    self.value = value
  }
}

Add instances of this class to your array.

class Stuff {}
var weaky: [Weak<Stuff>] = 
  [Weak(value: Stuff()), Weak(value: Stuff())]

When defining Weak you can use either struct or class.
Also, to help with reaping array contents, you could do something along the lines of:

extension Array where Element:Weak<AnyObject> {
  mutating func reap () {
    self = self.filter { nil != $0.value }
  }
}

The use of AnyObject above should be replaced with T - but I don't think the current Swift language allows an extension defined as such.

Contacts
I have a clear focus on time-to-market and don't prioritize technical debt. And I took part in the Pre-Sale/RFX activity as a System Architect, assessment efforts for Mobile (iOS-Swift, Android-Kotlin), Frontend (React-TypeScript) and Backend (NodeJS-.NET-PHP-Kafka-SQL-NoSQL). And I also formed the work of Pre-Sale as a CTO from Opportunity to Proposal via knowledge transfer to Successful Delivery.

🛩️ #startups #management #cto #swift #typescript #database
📧 Email: sergey.leschev@gmail.com
👋 LinkedIn: https://www.linkedin.com/in/sergeyleschev/
👋 LeetCode: https://leetcode.com/sergeyleschev/
👋 Twitter: https://twitter.com/sergeyleschev
👋 Github: https://github.com/sergeyleschev
🌎 Website: https://sergeyleschev.github.io
🖨️ PDF Design Patterns: Download


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Sergey Leschev


Print Share Comment Cite Upload Translate Updates
APA

Sergey Leschev | Sciencx (2023-02-04T07:25:58+00:00) Array of weak in Swift. Retrieved from https://www.scien.cx/2023/02/04/array-of-weak-in-swift/

MLA
" » Array of weak in Swift." Sergey Leschev | Sciencx - Saturday February 4, 2023, https://www.scien.cx/2023/02/04/array-of-weak-in-swift/
HARVARD
Sergey Leschev | Sciencx Saturday February 4, 2023 » Array of weak in Swift., viewed ,<https://www.scien.cx/2023/02/04/array-of-weak-in-swift/>
VANCOUVER
Sergey Leschev | Sciencx - » Array of weak in Swift. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/04/array-of-weak-in-swift/
CHICAGO
" » Array of weak in Swift." Sergey Leschev | Sciencx - Accessed . https://www.scien.cx/2023/02/04/array-of-weak-in-swift/
IEEE
" » Array of weak in Swift." Sergey Leschev | Sciencx [Online]. Available: https://www.scien.cx/2023/02/04/array-of-weak-in-swift/. [Accessed: ]
rf:citation
» Array of weak in Swift | Sergey Leschev | Sciencx | https://www.scien.cx/2023/02/04/array-of-weak-in-swift/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.