Sometimes it's useful to keep a list of weak references to objects, especially to reduce the maintenance work needed to track when they are destroyed if you had kept a strong reference to them instead.
class C {}
This wouldn't work since they keep a strong reference:
class Watcher {
var objects = [C]()
}
So you can define a WeakRef
wrapper class:
class WeakRef<T: AnyObject> {
weak var object: T?
init(object: T) {
self.object = object
}
}
And this will work as expected:
class Watcher {
var objects = [WeakRef<C>]()
}
Have fun with it!
Your feedback is valuable: Do you want more nuggets like this? Yes or No
.
Here's a few related nuggets:
- iOS Dev Nugget 314 Checked Array Subscripting
- iOS Dev Nugget 312 Fix Distorted Launch Screens with Hotspot Bar
- iOS Dev Nugget 309 Rule of Thumb Regarding self.
- iOS Dev Nugget 308 Careful with early return statements in functions that return void
- iOS Dev Nugget 307 Array Out of Bounds
- iOS Dev Nugget 306 Underscores in Numeric Literals
- iOS Dev Nugget 303 Use Enums for States
- iOS Dev Nugget 301 Print App Document Path II
- iOS Dev Nugget 300 Pipe Operator
- iOS Dev Nugget 299 Currying
- iOS Dev Nugget 297 Defensive Checks for Asynchronous Calls
- iOS Dev Nugget 292 for-where
- iOS Dev Nugget 291 Implicit Member Expressions
- iOS Dev Nugget 289 Set Background Color for UIWebView to Match App While Still Loading Documents
- iOS Dev Nugget 288 Use the .custom UIButton type to Fix Flashing when Title is Changed
- iOS Dev Nugget 287 Modifying a Property Which is a Struct Triggers didSet
- iOS Dev Nugget 286 Use Auto Layout for Bottom Bar
- iOS Dev Nugget 284 Use Shell Aliases for Git commands
- iOS Dev Nugget 282 Update to My Git no-commit Pre-commit Hook
- iOS Dev Nugget 276 Adding an Indent Default Parameter When Logging Calls in Object Hierarchies
- iOS Dev Nugget 275 Implementing UIView draw(_:)
- iOS Dev Nugget 272 Namespacing using Enums
- iOS Dev Nugget 270 Using lazy properties to workaround initializing dependent non-optional properties
- iOS Dev Nugget 268 Using Blocks for Delegate Methods
- iOS Dev Nugget 265 value type cannot have a stored property that recursively contains it
- iOS Dev Nugget 264 Track how long a function takes to run and report progress
- iOS Dev Nugget 259 CAShapeLayer, UIBezierPath and UIRectCorner
- iOS Dev Nugget 257 Asserts
- iOS Dev Nugget 255 Symbolic Breakpoint to watch for View Controller Deallocation
- iOS Dev Nugget 252 A Little Trick to Write Loosely Coupled View Controllers
- iOS Dev Nugget 251 Retrieving the Current View Controller
- iOS Dev Nugget 248 In-app Native Routing
- iOS Dev Nugget 246 Mapping Swift and Obj-C Method Names
- iOS Dev Nugget 244 IteratorProtocol
- iOS Dev Nugget 241 Swift Enums with Labels
- iOS Dev Nugget 240 Print an Object's Unique Identifier
- iOS Dev Nugget 238 Keep Code in Method at the Same Level of Abstraction
- iOS Dev Nugget 234 #available and @available
- iOS Dev Nugget 233 Separating View Controllers and Their Views
- iOS Dev Nugget 222 Uniquely Identifying User Devices
- iOS Dev Nugget 215 dump Instead of print for Swift Structs
- iOS Dev Nugget 211 Higher Order Functions: map, filter, reduce
- iOS Dev Nugget 209 Swift Defer
- iOS Dev Nugget 207 Swift Nil-Coalescing Operator
- iOS Dev Nugget 206 Date doesRelativeDateFormatting
- iOS Dev Nugget 205 The Never Return Type in Swift
- iOS Dev Nugget 204 Swift @autoclosure
- iOS Dev Nugget 203 Swift Measurement
- iOS Dev Nugget 202 Swift Enumerations to Encode States
- iOS Dev Nugget 201 Swift Pattern Matching
- iOS Dev Nugget 200 Swift flatMap
- iOS Dev Nugget 199 Swift Talk Video Series
- iOS Dev Nugget 198 KZPlayground
- iOS Dev Nugget 197 App Initialization Code
- iOS Dev Nugget 196 Rename Refactoring for Swift Code
- iOS Dev Nugget 193 Result Enumeration
- iOS Dev Nugget 188 SwiftLint
- iOS Dev Nugget 187 Creating a Singleton Class in Swift
- iOS Dev Nugget 186 Injection for Xcode
- iOS Dev Nugget 182 Tracking the swift-evolution Git Repository
- iOS Dev Nugget 181 Swift Selectors
- iOS Dev Nugget 180 Low Power Mode
- iOS Dev Nugget 172 Print App Document Path
- iOS Dev Nugget 168 Using Blocks to Manage Contexts
- iOS Dev Nugget 158 Checking if Newer APIs Are Available In Older iOS Versions (II)
- iOS Dev Nugget 149 Timepiece, a Swift Library for Handling Dates
- iOS Dev Nugget 143 Don't Forget SQLite
- iOS Dev Nugget 142 Grand Central Dispatch
- iOS Dev Nugget 141 Great Swift Standard Library Examples Covered in Playground (Swift 2.0)
- iOS Dev Nugget 138 Interoperability: Using Enumerations Defined In Objective-C from Swift
- iOS Dev Nugget 137 Named Tuple Elements
- iOS Dev Nugget 136 Auto Layout with Cartography
- iOS Dev Nugget 135 CAShapeLayer, Animated Views and XCPShowView()
- iOS Dev Nugget 133 Dollar.swift
- iOS Dev Nugget 132 XCPCaptureValue in Swift Playgrounds
- iOS Dev Nugget 131 Running Asynchronous Code in Swift Playgrounds
- iOS Dev Nugget 129 Browsing the Swift Standard Library
- iOS Dev Nugget 124 performSelector:withObject:afterDelay: in Swift
- iOS Dev Nugget 123 Using dynamicType in Swift
- iOS Dev Nugget 116 method() is unavailable: use object construction Class()
.