Menu
CoddyTech

ARC and Weak References

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

Go garbage collection manages reachable allocations and can reclaim unreachable cycles. An ordinary Go pointer keeps its object reachable; it is not a Swift weak reference. Resetting one pointer to nil does not make every alias weak or invalidate the still-referenced object.

Swift ARC tracks strong class ownership, and a cycle of strong references can retain unused objects. Weak references do not retain and become nil when the instance deallocates. Safe unowned references also do not retain but assume the instance remains valid whenever accessed.

The Go observer and keeper both reference the same session, with KeepAlive making the keeper lifetime explicit through the observation. The Swift observer is weak but its strong keeper preserves the session. Matching output does not equate these ownership mechanisms.

Go: complete program

session.go

package main
type Session struct { Name string }

main.go

package main
import (
    "fmt"
    "runtime"
)

func main() {
owner := &Session{Name: "canvas"}
    keeper := owner
    observer := owner
    owner = nil
    fmt.Println(observer.Name)
    runtime.KeepAlive(keeper)
    _ = owner
}

Swift: complete program

Session.swift

import Foundation
final class Session {
    let name: String
    init(name: String) { self.name = name }
}

main.swift

import Foundation
var owner: Session? = Session(name: "canvas")
let keeper = owner!
weak var observer: Session? = owner
owner = nil
withExtendedLifetime(keeper) { print(observer?.name ?? "gone") }

Go defer provides scope-bound operations independently of garbage collection. Use explicit resource cleanup when the timing matters, and choose Swift weak relationships from lifetime requirements.

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 Go Developers

Practice on your own: Swift playground