blog.iankulin.com

Code

Day 23 - Views and Modifiers - Part 2

A short SwiftUI note in which I compare two ways to vary a view based on state: branching with if/else inside the body, which Paul Hudson warns makes SwiftUI destroy and recreate views when the state changes, versus using the ternary operator within a modifier so only the changed property updates. I walk through both code versions and reflect on why the efficiency difference exists...

Day 23 - Views and Modifiers - Part 1

I give an introduction to SwiftUI views, explaining that views are immutable structs with a body property that SwiftUI renders automatically rather than being drawn manually. I cover container views like HStack and the use of modifiers such as padding and frame, including how modifiers on a container apply to its children and why their order affects the result...

Challenge 1

I built a simple unit conversion app for Challenge 1 of Day 19 of 100 Days of SwiftUI, deliberately keeping it minimal without MVVM. New techniques I covered include limiting the keyboard to numbers and adding a toolbar to dismiss it. I also note that SwiftUI text fields don’t select existing text for overtyping, with no straightforward fix short of a third-party package, and I include full code plus a GitHub link...

Codewars / reduce

Trying out Codewars, I solve a beginner Swift kata about finding the integer that appears an odd number of times in an array, using a Set to track entries. The top community solution turns out to be a one-liner using reduce and XOR, prompting me to explain how reduce works, why XORing every element leaves the odd-occurring value, and to argue that the longer version is still easier to read...

Named Loops

Breaking out of nested loops to a specific outer loop has no direct support in C/C++, so the traditional workaround is converting for loops to while loops with a continue flag, which I demonstrate with an example that stops processing a string at a given character. Swift offers a cleaner alternative through labeled loops, where naming a loop lets a break statement exit directly to that level...

Checkpoint 9

My solution to Hacking with Swift’s Checkpoint 9: writing a single-line Swift function that returns a random element from an optional integer array, or a random number from 1 to 100 when the array is nil or empty. I show the challenge, an embedded video, and the finished one-liner...

Checkpoint 8

My solution to Hacking with Swift’s Checkpoint 8: a Building protocol requiring rooms, cost, and estate agent properties, with a protocol extension providing a method that prints a sales summary. My House and Office structs conform to the protocol, each adding its own extra property, and my sample instances show the method in use...

Protocols

Swift structs can’t use inheritance and classes are limited to a single superclass, so protocols fill the gap by letting structs, classes, and enums adopt shared properties and methods. I walk through a C++ multiple inheritance example and its Swift protocol equivalent, showing how a function can accept any type that conforms to a given protocol...

Checkpoint 7

My solution to Hacking with Swift’s Checkpoint 7, which calls for an animal class hierarchy with Animal, Dog, and Cat as superclasses and specific breeds as subclasses. My included Swift code implements the legs property, overridden speak() methods for each breed, and an isTame property set through initializers...

Simple MVVM

I explain the MVVM architectural pattern using a simple SwiftUI light bulb toggle app as an example. I walk through the Model, View Model, and View components, how they connect through ObservableObject and @Published/@StateObject, and why separating data from the interface matters. I’ve made the source code available on GitHub...

Memorise Assignment 1

My notes on completing the first assignment from Stanford’s CS193p SwiftUI lecture series, which involved minor changes to the app built in the lectures. My write-up covers an error that prevented me from initializing a @State property from an instance member, and some dissatisfaction with duplicated emoji arrays and SF Symbol alignment—issues I partly resolved after reading the assignment’s hints section and rewatching a lecture...

Checkpoint 6

My short Swift exercise solution covers structs and access control: a Car struct stores a model, seat count, and current gear. The gear is protected with private(set) access and can only be changed through my mutating gearUp and gearDown methods that stay within static min and max gear constants...

Checkpoint 5

My Swift solution to an array-processing exercise: filtering even numbers out of an array of lucky numbers, sorting the remaining values in ascending order, mapping each one to a formatted string, and printing the results one per line using filter, sorted, and map...

Learning Retention

I rewrote a Swift checkpoint exercise after the original unsaved code was lost, with the task being to count the unique elements in an array of strings. The solution casts the array to a Set, and I reflect on having encountered sets twice recently, in the 100 days course and a podcast episode, calling that kind of spaced exposure high-quality learning...

Checkpoint 4

My Swift solution to a coding challenge: compute the integer square root of a number between 1 and 10,000 without using the built-in sqrt() function. My implementation uses a brute-force loop and throws custom errors for out-of-bounds input or when no integer root exists, with do/catch error handling shown in the example usage...

Checkpoint 4 optimisation

A walkthrough of the Hacking with Swift Checkpoint 4 exercise, computing integer square roots in Swift. I first solve it with a brute-force loop, then show a binary search version that handles far larger input ranges with no perceptible delay, and reflect on the trade-offs between code simplicity and performance...

Checkpoint 3

This is my Swift implementation of the classic FizzBuzz exercise. My code loops from 1 to 100, using named booleans for divisibility by 3 and 5 to decide whether to print Fizz, Buzz, FizzBuzz, or the number itself...