FIT3178 Chap.1 Swift, State and the iOS Runtime
Swift, State and the iOS Runtime
FIT3178 starts from Swift because every later iOS feature depends on choices made at the language boundary. A value type normally gives independent copied state; a class gives shared identity; an optional makes absence part of the type. These are not isolated definitions.
They determine whether an edit is local, whether two screens observe the same instance, whether a closure keeps an object alive and whether a missing value becomes a controlled branch or a crash.
When reading a small program, begin with a state ledger. Name each value, its declared type, the object or scope that owns it, and the events that can change it.
For structures, ask when a copy occurs and whether a nested reference still shares storage. For classes, draw the references rather than the variables alone. Two names can point to one instance, and the important question becomes who is permitted to mutate that instance and how an observer learns that it changed.
Optionals deserve the same discipline.
Optional binding, nil coalescing and guarded early exit communicate different decisions. Binding says subsequent work needs the value. A default says absence has an acceptable replacement. A guard says the operation cannot continue. Force unwrapping states a much stronger invariant: the value cannot be absent at that point.
If that invariant is not created and tested by the program, the exclamation mark is a postponed failure rather than confidence.
Closures add time to the ownership problem. A callback may execute after the function that created it and may capture a controller or model. A strong capture can extend lifetime; a weak capture can become nil; an unowned capture assumes the object outlives the closure.
Trace who stores the closure, when it runs and what cancellation means. This is especially important for network tasks, animations and notification handlers whose completion may arrive after navigation.
Worked reasoning: a profile editor shows an optional avatar and an editable display name. Keep the draft as a value owned by the editor.
Pass an immutable starting value into the screen, validate changes locally, and commit through one explicit callback. The image placeholder handles a nil avatar without hiding an invalid name.
If a shared reference is used instead, cancelling the screen may already have mutated the underlying profile; the architecture must decide whether that is intended.
For portfolio evidence, explain the choice and show the changed case. A code screenshot alone does not establish understanding.
State why the type fits ownership, demonstrate the nil or lifetime path, and identify what would break if the state were shared differently. The current portfolio is competency based, so reasoning should be reconstructable in discussion rather than polished only at submission time.
A useful ownership audit distinguishes the variable, the value and any referenced storage.
Copying a structure copies its value, but a property inside that structure may still refer to a shared object. Conversely, two constants can point to one mutable class instance even though neither variable can be reassigned. For every assignment, capture and callback, write what is copied, what is shared, and which lifetime assumption the code is making.
Then change one assumption and predict the visible effect before running the app.
Rehearse with a three-event trace: open the editor, begin an avatar request, then cancel before completion. At each event, record the draft value, profile value, request token and controller lifetime. The correct result is not merely no crash.
The original profile must retain its name, the dismissed interface must not receive presentation work, and the image result must either be cancelled or rejected as obsolete. This trace turns value semantics, optionals and captures into one observable contract.
A final Swift rehearsal should move in both directions.
Given code, infer the ownership contract and likely user-visible failure; given a product promise such as Cancel discards changes, choose value, reference and callback boundaries that make it true. Include a nested reference and a delayed closure so the answer cannot rely on the shallow slogan that structures copy and classes share. The goal is a trace that survives small implementation changes.
What this chapter covers
- 01
value semantics
- 02
reference semantics
- 03
optional
- 04
reason from Swift values and object lifetimes to predictable interface behaviour
- 05
Swift syntax is not an architecture; the relevant question is which object owns the state and how changes propagate.
Cancel a profile edit without leaking state
- 1Identify value and reference state.
- 1Name the draft owner.
- 1Trace the asynchronous capture.
- 1Define cancellation and commit.
- 1Test nil and late completion.
Key terms
- value semantics
- Copying a value creates independent state unless shared storage is introduced explicitly.
- reference semantics
- Multiple references can observe and mutate the same class instance.
- optional
- A type-safe representation of either a value or absence that must be handled deliberately.
Swift, State and the iOS Runtime FAQ
What is the key decision in Swift, State and the iOS Runtime?
Reason from swift values and object lifetimes to predictable interface behaviour.
Which failure path matters most?
Swift syntax is not an architecture; the relevant question is which object owns the state and how changes propagate.
Are the worked cases official portfolio tasks?
No. They are AskSia-authored practice aligned to the current teaching sequence.
What should be shown in discussion?
Show the state or responsibility trace, the changed condition and evidence that the repair works.
Assessment move
Rebuild the state or dependency map from memory, run the worked change, then explain the first assumption that fails.
Working through Swift, State and the iOS Runtime in FIT3178? Sia is AskSia’s AI Software Development tutor — ask any FIT3178 Swift, State and the iOS Runtime question and get a clear, step-by-step explanation grounded in how FIT3178 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.