Menu
CoddyTech

ARC and Weak References

Lesson 29 of 48 in Coddy's Swift for GDScript Developers course.

Godot RefCounted instances are released when their reference count reaches zero, while Nodes and other manually managed objects have different lifetime rules. Reference cycles can retain RefCounted objects. A weakref can observe an object without contributing a strong reference, and get_ref returns null after it is gone.

Swift ARC likewise manages strong class ownership and needs cycle-aware design. A weak reference becomes nil without retaining its object and is a mutable Optional. Safe unowned access assumes the instance still exists and traps if that assumption is violated.

The source helper observes a local RefCounted token while alive and returns only its WeakRef. The Swift helper returns a closure capturing its token weakly. Both later observations report that the strong local ownership has ended.

Complete GDScript comparison program:

extends SceneTree
class Token extends RefCounted:
    pass
func observe():
    var token = Token.new()
    var view = weakref(token)
    print(view.get_ref() != null)
    return view

func _init():
    var view = observe()
    print(view.get_ref() == null)
    quit()

Complete Swift comparison program, compiled from these files:

Token.swift

final class Token {}

Functions.swift

func observer() -> () -> Bool {
    let token = Token()
    weak var view = token
    print(view != nil)
    return { [weak token] in token == nil }
}

main.swift

let isGone = observer()
print(isGone())
challenge icon

Challenge

Easy

Implement Member with immutable name: String and init(name:). Implement Membership with weak optional member: Member? and init(member:). The membership must not keep the member alive. Input is one ASCII name of 1 through 20 letters. Create a member in an optional owner, create a membership, print its member name, clear the sole owner, and report whether the membership is now nil as name true.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Member.swift, Membership.swift, Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
var owner: Member? = Member(name: "Heldout"); let link = Membership(member: owner!); guard link.member === owner else { return false }; owner = nil; return link.member == nil
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Swift for GDScript Developers

Practice on your own: Swift playground