Latest / CyberSecurity Summary / iOS 11 Programming Fundamentals with Swift: Swift, Xcode, and Cocoa Basics
Transcript
- 0:00Speaker 1: So if you're coming to Swift from like C or
- 0:03Speaker 1: objective C, you probably think you have a pretty good
- 0:06Speaker 1: handle on what the number one is.
- 0:07Speaker 2: Right. It's just a basic building block exactly.
- 0:09Speaker 1: You think of it as a scaler, you know, a primitive,
- 0:12Speaker 1: dumb piece of data just kind of sitting there in memory.
- 0:14Speaker 2: Yeah, just raw bites.
- 0:16Speaker 1: Right. But if you're in Swift and you type the
- 0:18Speaker 1: number one and then you just add a dot and
- 0:20Speaker 1: call a method on it, like you know, say hello, yeah,
- 0:23Speaker 1: it actually works.
- 0:24Speaker 2: Which is pretty mind bending the first time you see it.
- 0:26Speaker 1: It really is. The compiler doesn't throw an error and
- 0:29Speaker 1: the console just outputs of greeting. So today, in this
- 0:32Speaker 1: deep dive, we are taking on the underlying architecture of Swift.
- 0:37Speaker 2: Yeah, we're really getting into the mechanics of it all.
- 0:40Speaker 1: We are. We're using some great excerpts from Matt Neuberg's
- 0:43Speaker 1: book iOS eleven Programming Fundamentals with Swift, and our mission
- 0:47Speaker 1: here is to sort of decode how this language actually
- 0:50Speaker 1: operates under the hood. We want to move past, you know,
- 0:53Speaker 1: just the basic syntax and really understand the core mechanisms
- 0:57Speaker 1: so you can start to anticipate how the compiler actually thinks.
- 1:01Speaker 2: Yeah, because we're really looking for the why behind Swift's
- 1:04Speaker 2: incredibly strict rules. I mean, at first lance, the compiler
- 1:08Speaker 2: can feel honestly kind of adversarial.
- 1:10Speaker 1: Or really just constantly yelling at you.
- 1:12Speaker 2: Right, constantly throwing warnings and errors. But once you start
- 1:16Speaker 2: to understand the philosophy of the language's blueprint, it's kind
- 1:19Speaker 2: of like looking at a schematic for a really complex machine.
- 1:23Speaker 2: You realize those constraints are actually.
- 1:25Speaker 1: Tools, tools to help you, not hurt you exactly.
- 1:29Speaker 2: They're designed to give you this incredible architectural elegance.
- 1:32Speaker 1: Okay, let's unpack this. Let's start with that friction I
- 1:35Speaker 1: mentioned at the top, the idea that in Swift, literally
- 1:38Speaker 1: everything is an object everything right. There are no primitive
- 1:41Speaker 1: scalers so Aboolian an integer. They aren't just raw bites
- 1:46Speaker 1: like we said before. They are like fully formed objects
- 1:49Speaker 1: with their own behaviors.
- 1:50Speaker 2: Yeah. And the ultimate acid test for whether something is
- 1:53Speaker 2: an object in Swift is well, whether you can send
- 1:55Speaker 2: it a message right right, or if you can modify
- 1:58Speaker 2: its behavior by defining new message on it. So in Swift,
- 2:02Speaker 2: that number one is actually an instance of a struct
- 2:05Speaker 2: which is wild. It is and Swift builds this entire
- 2:08Speaker 2: foundation on basically three specific flavors of object types. You've
- 2:12Speaker 2: got your classes, your structs, and your enoms.
- 2:15Speaker 1: Now, enoms are a really interesting inclusion there, I think,
- 2:18Speaker 1: because you know, in older languages, an enom is usually
- 2:21Speaker 1: just a flat list of integers under the hood.
- 2:23Speaker 2: Oh yeah, just simple industry mappings, right.
- 2:26Speaker 1: But in Swift, they are full fledged objects. They can
- 2:29Speaker 1: have properties, they can have methods, they can even maintain state.
- 2:32Speaker 2: And that distinction is so crucial to wrap your head
- 2:35Speaker 2: around because whether it's a class, a struct, or an enom,
- 2:39Speaker 2: they all share this fundamental ability to receive messages and
- 2:44Speaker 2: encapsulate state. The memory management differences between them those become
- 2:48Speaker 2: really critical later on, but at their architectural core, they
- 2:51Speaker 2: are all just objects serving as devices for maintaining state.
- 2:55Speaker 1: Right. And Newberg uses this classic shoebox analogy to explain variables,
- 2:59Speaker 1: which I love. But what makes Swift's implementation so fascinating
- 3:02Speaker 1: isn't the box itself. No, it's the rules around the
- 3:05Speaker 1: box exactly. It's how the compiler acts as this like
- 3:08Speaker 1: hyper strict warehouse manager.
- 3:10Speaker 2: Yeah, I like that comparison.
- 3:11Speaker 1: So you have your instance, your object, and you place
- 3:14Speaker 1: it in a variable that provides the object with a
- 3:17Speaker 1: scope in a lifetime right right.
- 3:18Speaker 2: As long as the scope exists, the object is kept
- 3:20Speaker 2: alive in memory.
- 3:22Speaker 1: But Swift forces you to declare your intent immediately. You
- 3:26Speaker 1: have to choose between two very different domains right out
- 3:29Speaker 1: of the gate, var for a variable and let for constant, And.
- 3:34Speaker 2: This forces the developer to make a really declarative choice
- 3:37Speaker 2: about mutation, and you have to do it before the
- 3:40Speaker 2: code ever even runs. Right. So, a var is basically
- 3:42Speaker 2: an open shoe box. You can swap the contents out whenever.
- 3:45Speaker 1: You want, toss a new pair of shoes in there, exactly.
- 3:48Speaker 2: But a let is a box that is glued shut.
- 3:51Speaker 2: Once it's initialized. The memory address it points to or
- 3:54Speaker 2: the value it holds, it just cannot be reassigned.
- 3:57Speaker 1: It's locked in.
- 3:58Speaker 2: It's totally locked in. And the mechanism, and that the
- 4:00Speaker 2: reason is so strict, is purely about memory efficiency and
- 4:03Speaker 2: thread safety.
- 4:04Speaker 1: Because if the compiler knows a value is immutable, right,
- 4:07Speaker 1: if it knows it's a let, it doesn't have to
- 4:10Speaker 1: build in all that overhead to track potential changes, exactly.
- 4:14Speaker 2: It doesn't have to watch it constantly, right.
- 4:16Speaker 1: It can just optimize how that data is stored. In fact,
- 4:19Speaker 1: if you declare a var but you never actually change
- 4:22Speaker 1: its value, the Swift compiler will actively audit your code
- 4:26Speaker 1: and throw warning.
- 4:27Speaker 2: Yeah, it'll actually suggest that you downgrade it to alet.
- 4:30Speaker 1: It's like, hey, you're wasting resources here.
- 4:32Speaker 2: Pretty much, the compiler is performing this massive static analysis
- 4:35Speaker 2: of your entire code base. It maps out every single
- 4:39Speaker 2: read and write operation.
- 4:41Speaker 1: Wow.
- 4:41Speaker 2: So when it sees that a state doesn't change, enforcing
- 4:45Speaker 2: immutability allows it to make assumptions, and those assumptions speed
- 4:49Speaker 2: up execution and honestly eliminate entire categories of bugs like
- 4:52Speaker 2: waste conditions exactly, race conditions where two threads try to
- 4:56Speaker 2: modify the same variable simultaneously. If it's a let, that's
- 5:00Speaker 2: literally impossible.
- 5:01Speaker 1: That makes so much sense. So the compiler is optimizing
- 5:04Speaker 1: how data is stored. But you know, memory safety isn't
- 5:08Speaker 1: just about how data is stored. It's also about where
- 5:10Speaker 1: it lives and who has access to.
- 5:12Speaker 2: It, right, the location and the visibility.
- 5:14Speaker 1: Yeah, because if we have like thousands of these initialized
- 5:17Speaker 1: shoe boxes floating around in our app, we need to
- 5:20Speaker 1: prevent naming collisions, and that brings us to name spaces
- 5:23Speaker 1: and encapsulation.
- 5:24Speaker 2: Yeah. So namespaces in Swift act as these isolated memory domains.
- 5:28Speaker 2: They organize the codebase. Right, So if you have a
- 5:32Speaker 2: class called manny and a class called mo, they can
- 5:35Speaker 2: actually both contain a nested class called class.
- 5:37Speaker 1: And they don't fight with each other.
- 5:39Speaker 2: Nope, the compiler doesn't get confused at all because the
- 5:41Speaker 2: name space requires explicit routing. You have to say many
- 5:45Speaker 2: class versus mo class ah okay, and even importing an
- 5:49Speaker 2: external framework like say Cucko's Foundation that establishes its own
- 5:53Speaker 2: module level name space.
- 5:55Speaker 1: With The book points out a really vital distinction here,
- 5:57Speaker 1: which I think people miss. A name space organizes things,
- 6:01Speaker 1: but it does not hide them, right.
- 6:03Speaker 2: Organization is not privacy.
- 6:04Speaker 1: Exactly by default, properties and methods inside these namespaces are
- 6:09Speaker 1: internally public to the module. So if you want to
- 6:12Speaker 1: actually protect the state of your object, say you want
- 6:15Speaker 1: to prevent another developer from directly overwriting like a critical
- 6:18Speaker 1: configuration string, you have to explicitly use the private keyword.
- 6:22Speaker 2: Yeah. Encapsulation requires deliberate action on your part. By marking
- 6:27Speaker 2: a property private, you restrict its visibility strictly to the
- 6:31Speaker 2: enclosing declaration.
- 6:32Speaker 1: You're basically putting up a brick wall, you really are.
- 6:34Speaker 2: You're telling the compiler do not allow any external code
- 6:38Speaker 2: to even acknowledge this memory exists.
- 6:40Speaker 1: Wow.
- 6:41Speaker 2: And that forces other objects to interact with your data
- 6:44Speaker 2: solely through the methods you expose, rather than just reaching
- 6:47Speaker 2: in and manipulating the raw state directly.
- 6:50Speaker 1: Which perfectly bridges us to the logic itself. Because state,
- 6:54Speaker 1: you know, just sitting there, isolated in memory. It doesn't
- 6:57Speaker 1: actually execute anything.
- 6:58Speaker 2: Right, it's just inert.
- 7:00Speaker 1: We need an engine to process that state, which means
- 7:03Speaker 1: we need functions. But Newberg frames functions in Swift not
- 7:07Speaker 1: just as blocks of code, but as these incredibly rigid
- 7:09Speaker 1: processing contracts.
- 7:11Speaker 2: Yeah. The structure of a function in Swift is defined
- 7:13Speaker 2: by what we call its function.
- 7:15Speaker 1: Signature, the signature. Okay, right.
- 7:17Speaker 2: When you declare a function, you aren't just giving it
- 7:20Speaker 2: a name. You are defining the exact type of data
- 7:23Speaker 2: it accepts and the exact type of data it returns.
- 7:26Speaker 2: It's very explicit, extremely explicit. For instance, if you have
- 7:30Speaker 2: a function that takes two integers and returns a string,
- 7:34Speaker 2: its signature is literally evaluated by the compiler as int
- 7:38Speaker 2: int with an arrow pointing to string.
- 7:40Speaker 1: I really want to look at this idea of the
- 7:42Speaker 1: signature as like an ironclad cryptographic handshake.
- 7:46Speaker 2: That's a good way to put it right.
- 7:48Speaker 1: Because if the signature demands two integers and you try
- 7:51Speaker 1: to pass it a double and a float, the compiler
- 7:54Speaker 1: intercepts it at build time.
- 7:55Speaker 2: Yep, it refuses to even compile the binary.
- 7:58Speaker 1: Which is a massive shift if you're coming from say,
- 8:01Speaker 1: dynamic languages like JavaScript or Python, and those languages, the
- 8:04Speaker 1: code might just try to run anyway and then crash
- 8:06Speaker 1: right in the user's hands.
- 8:07Speaker 2: And that's exactly what Swift wants to avoid. The compiler's
- 8:10Speaker 2: primary directive is to audit for correctness before execution.
- 8:13Speaker 1: Before the user ever sees it exactly.
- 8:16Speaker 2: That's strict type checking ensures that the data passing between
- 8:20Speaker 2: all your different modules aligns perfectly with those established contracts.
- 8:23Speaker 1: Okay, but what if there is no output?
- 8:25Speaker 2: What are you any like?
- 8:26Speaker 1: What if I build a machine that doesn't spit anything
- 8:28Speaker 1: out at all? Say I'm writing a function that just
- 8:31Speaker 1: saves a payload to disk or prints a log to
- 8:34Speaker 1: the console, it doesn't actually return a new piece of
- 8:37Speaker 1: data to the system. Is it still a machine does
- 8:40Speaker 1: the contract.
- 8:40Speaker 2: Still absolutely, The contract is still absolute. It just uses
- 8:44Speaker 2: a void return type.
- 8:45Speaker 1: Ah void.
- 8:46Speaker 2: Right. The function executes its logic what we call side effects.
- 8:50Speaker 2: Those are actions performed outside its local scope, like system
- 8:53Speaker 2: io or updating a UI element, but its signature explicitly
- 8:57Speaker 2: states it hands nothing back. You can write the arrow
- 9:01Speaker 2: pointing to the word void, You can use an empty
- 9:03Speaker 2: pair of parentheses, or you can even just omit the
- 9:06Speaker 2: return declaration entirely.
- 9:08Speaker 1: And the compiler understands all of those.
- 9:09Speaker 2: Yeah, the compiler registers all of those as a strict
- 9:12Speaker 2: void contract.
- 9:13Speaker 1: Okay. So on the flip side of that, what if
- 9:15Speaker 1: a function does return a value but I simply don't
- 9:18Speaker 1: care about it. Give me an example.
- 9:20Speaker 2: So say I call a system API that updates a
- 9:23Speaker 2: database and it returns a boolean true or false for
- 9:26Speaker 2: success or failure. But I just want the side effect
- 9:30Speaker 2: of the update. I don't care about the boolean. If
- 9:32Speaker 2: I don't assign that booleon to a variable, the compiler
- 9:36Speaker 2: actually yells at me with a warning.
- 9:38Speaker 1: Right, Because the compiler assumes that if a function took
- 9:41Speaker 1: the time to return data, that data is probably necessary
- 9:45Speaker 1: for the program's flow.
- 9:47Speaker 2: It thinks I made a mistake exactly.
- 9:49Speaker 1: Ignoring it is flagged as a potential logic error.
- 9:52Speaker 2: But you can bypass that right by assigning the result
- 9:55Speaker 2: to an underscore.
- 9:56Speaker 1: Yes, the underscore is so useful.
- 9:58Speaker 2: I love the underscore. It acts as a black hole,
- 10:01Speaker 2: a variable without a name. You just assigned the output
- 10:03Speaker 2: to the underscore, which satisfies the compiler's requirement that the
- 10:07Speaker 2: data was caught.
- 10:08Speaker 1: Right, the contract is fulfilled.
- 10:09Speaker 2: But it's just immediately discarded from memory.
- 10:12Speaker 1: Yeah, and alternatively, the developer who actually wrote the function
- 10:16Speaker 1: can proactively market with the at discardable result attribute.
- 10:20Speaker 2: Oh right, so they can warn the compiler ahead of time. Exactly.
- 10:23Speaker 2: It signals to the compiler that the return value is
- 10:25Speaker 2: purely optional, so it won't yell at you for ignoring it.
- 10:29Speaker 1: Now, this whole level of strictness regarding inputs and outputs,
- 10:32Speaker 1: it really dictates how we actually invoke these functions. And
- 10:36Speaker 1: to use this highly strict machine, you have to pull
- 10:39Speaker 1: the right levers, which means knowing exactly what to call
- 10:44Speaker 1: the interluts you're feeding it. Because the compiler is so
- 10:47Speaker 1: focused on type safety. Swift implements a very unique philosophy
- 10:51Speaker 1: regarding parameter naming.
- 10:52Speaker 2: Yeah. This is to ensure clarity at the call site,
- 10:55Speaker 2: and honestly, it's one of the features that makes Swift
- 10:57Speaker 2: code read so beautifully.
- 10:59Speaker 1: It really don't. It separates the internal name of a
- 11:01Speaker 1: parameter from the external name.
- 11:03Speaker 2: Right, because inside the function you want short mathematical variable
- 11:07Speaker 2: names like wnh.
- 11:08Speaker 1: Yeah, you don't want to type out huge names over
- 11:11Speaker 1: and over. But when you are calling the function from
- 11:13Speaker 1: the outside, you wanted to read like a plain English
- 11:16Speaker 1: sentence exactly.
- 11:17Speaker 2: So by default, Swift actually externalizes the internal parameter name,
- 11:21Speaker 2: but you have the power to define a completely specific
- 11:24Speaker 2: external label.
- 11:25Speaker 1: So if you are writing a function to draw shape.
- 11:28Speaker 2: Right, internally, the variables might just be wnh, but you
- 11:31Speaker 2: define external labels, so the call site reads draw shape
- 11:35Speaker 2: with one two hundred.
- 11:38Speaker 1: It's so clean. And the mechanism behind this dual naming
- 11:41Speaker 1: system is heavily tied to bridging the gap with Cocoa
- 11:45Speaker 1: and objective c APIs.
- 11:46Speaker 2: Isn't it, Oh, heavily. Objective c was notorious for its
- 11:49Speaker 2: incredibly long, highly descriptive method.
- 11:52Speaker 1: Names, those massive sentences.
- 11:54Speaker 2: Yeah, So Swift uses these external parameters to interface cleanly
- 11:58Speaker 2: with those legacy frameworks. It maintains that descriptive verbosity on
- 12:02Speaker 2: the outside while keeping the internal Swift logic super concise.
- 12:06Speaker 1: What's fascinating here is how it bridges that gap between
- 12:09Speaker 1: machine logic and human readability. And you can even take
- 12:13Speaker 1: that a step further, right, you can completely suppress the
- 12:16Speaker 1: external name using an underscore.
- 12:17Speaker 2: Yep, the underscore returns if the context is overwhelmingly obvious,
- 12:21Speaker 2: like if you're writing print hello, Forcing the vilaper to
- 12:24Speaker 2: type print message hello is just redundant. It's annoying, right,
- 12:27Speaker 2: So the underscore just eliminates that required label entirely.
- 12:30Speaker 1: And this focus on precise signatures and naming it leads
- 12:34Speaker 1: to this really powerful architectural feature called overloading.
- 12:37Speaker 2: Yes, overloading is huge in Swift because.
- 12:41Speaker 1: In older languages like Objective C, if you tried to
- 12:44Speaker 1: name two methods the exact same thing, the compiler would
- 12:47Speaker 1: just throw a duplicate declaration error and refuse to build butt.
- 12:51Speaker 1: In Swift, it's totally legal, it is.
- 12:53Speaker 2: Overloading allows you to define multiple functions with the exact
- 12:57Speaker 2: same name, as long as their signatures meaning they're im
- 13:00Speaker 2: or their outputs are different.
- 13:01Speaker 1: Okay. To visualize how the compiler handles this, think of
- 13:04Speaker 1: having two friends named.
- 13:05Speaker 2: Alex Okay, two Alex's yeah.
- 13:08Speaker 1: One is a mechanic, the other's a baker. If I
- 13:10Speaker 1: just yell Alex yeah and throw a wrench across the room,
- 13:13Speaker 1: the mechanic steps up.
- 13:14Speaker 2: To catch it, right because of the wrench exactly.
- 13:16Speaker 1: But if I yell Alex yeah and throw a bag
- 13:19Speaker 1: of flour, the baker steps up. The context the specific
- 13:23Speaker 1: type of input I am providing dictates exactly which function
- 13:26Speaker 1: answers the call.
- 13:27Speaker 2: That's a great analogy. Under the hood, the compiler actually
- 13:29Speaker 2: uses a process called name mangling.
- 13:31Speaker 1: Name mangling that sounds aggressive, it's.
- 13:33Speaker 2: Just a technical term. It takes the function name and
- 13:36Speaker 2: combines it with the type signatures to generate these completely
- 13:40Speaker 2: unique identifiers in the compiled binary. So say what strength
- 13:45Speaker 2: and say what int might look like the exact same
- 13:49Speaker 2: function name to us human developers, but to the compiler
- 13:52Speaker 2: they map to entirely different memory addresses based on the
- 13:55Speaker 2: types you feed them.
- 13:57Speaker 1: That's brilliant. It also gives the caller a lot of flexibility,
- 14:00Speaker 1: you know, through default parameters.
- 14:02Speaker 2: Oh yeah, default parameters are great.
- 14:04Speaker 1: You can declare a function where a parameter defaults to
- 14:06Speaker 1: a specific value like times in one if the caller
- 14:11Speaker 1: omits that parameter entirely, the compiler just silently inserts the
- 14:15Speaker 1: default value.
- 14:16Speaker 2: Or you can use variatic parameters right where you.
- 14:18Speaker 1: Add three dots after a type like string.
- 14:20Speaker 2: Exactly Suddenly, the caller can pass an arbitrary number of strings,
- 14:24Speaker 2: just as many as they want, and the compiler automatically
- 14:26Speaker 2: packages them all into an array for the internal logic
- 14:29Speaker 2: to process.
- 14:31Speaker 1: So it creates this incredibly robust front end for the API.
- 14:35Speaker 1: The developer calling the function has all this breathing room,
- 14:38Speaker 1: but the internal contract remains strictly enforced by the compiler.
- 14:42Speaker 2: Yep, you got the best of both worlds.
- 14:43Speaker 1: Okay, But here's where I always kind of hit a
- 14:46Speaker 1: wall when dealing with Swift's strictness. Normally, when you toss
- 14:50Speaker 1: that bag of flour to Alex the baker, you're just
- 14:52Speaker 1: handing over a copy. If I pass a variable into
- 14:56Speaker 1: a function, by default, Swift treats them parameter as a
- 15:01Speaker 1: let constant. The function can read the data, but it
- 15:05Speaker 1: cannot mutate the original variable.
- 15:06Speaker 2: Right, it's glued shut, just like we talked about it,
- 15:08Speaker 2: all right, it's gluted shut.
- 15:09Speaker 1: So if I need a utility function to permanently alter
- 15:12Speaker 1: a piece of state that I hand it. How do
- 15:14Speaker 1: I get around that restriction? Because this feels like we're
- 15:17Speaker 1: entering the danger zone.
- 15:18Speaker 2: We are definitely entering the danger zone. This touches on
- 15:21Speaker 2: the fundamental difference in how Swift handles memory for value
- 15:24Speaker 2: types versus reference types. So with value types, which include
- 15:27Speaker 2: things like strucks, enoms, strings, and integers, the default behavior
- 15:32Speaker 2: is to pass a copy of the data. The function
- 15:35Speaker 2: receives a totally isolated clone sitting in a completely different
- 15:39Speaker 2: memory space on the stack.
- 15:41Speaker 1: So modifying the clone inside the function does absolutely nothing
- 15:45Speaker 1: to the original variable sitting in the outer scope.
- 15:47Speaker 2: Exactly, you're just playing with a copy. To breach that wall,
- 15:49Speaker 2: you have to explicitly declare the parameter with the inout
- 15:53Speaker 2: keyword in out right. This changes the whole mechanism. Instead
- 15:57Speaker 2: of passing a copy, you are officially authorized the function
- 16:00Speaker 2: to modify the original value.
- 16:02Speaker 1: Here's where it gets really interesting to me. Swift forces
- 16:06Speaker 1: you to acknowledge this danger right at the call site.
- 16:10Speaker 2: Doesn't let you hide it.
- 16:11Speaker 1: No, when you invoke a function that takes an in
- 16:13Speaker 1: out parameter, you can't just pass the variable normally. You
- 16:17Speaker 1: have to propen the variable name with an ampersand the
- 16:20Speaker 1: inter symbol yep, and that ampersand is like a giant
- 16:23Speaker 1: flashing red warning light in your code. You are explicitly
- 16:26Speaker 1: telling the compiler and yourself I know I am mutating
- 16:29Speaker 1: state outside this function's local scope.
- 16:32Speaker 2: And under the hood. The mechanism Swift uses for inout
- 16:35Speaker 2: is generally called call by value result.
- 16:38Speaker 1: What does that mean exactly?
- 16:40Speaker 2: It basically means it copies the value in, mutates it
- 16:43Speaker 2: locally inside the function, and then copies it back over
- 16:46Speaker 2: the original memory address when the function returns.
- 16:49Speaker 1: Oh wow.
- 16:49Speaker 2: Yeah. But keep in mind, with reference types like class instances,
- 16:54Speaker 2: this paradigm shifts entirely.
- 16:57Speaker 1: Right because classes are allocated on the heap right exactly.
- 16:59Speaker 1: So if I pass an instance of a dog class
- 17:02Speaker 1: into a function, I'm not passing a copy of the dog.
- 17:04Speaker 2: No, you are passing a reference. You're passing a pointer
- 17:07Speaker 2: to the exact location in memory where that specific instance lives.
- 17:11Speaker 1: Because you are sharing the reference itself, the function can
- 17:14Speaker 1: just directly modify the properties of that class instance.
- 17:17Speaker 2: Right. Yeah, you don't need the in out keyword, you
- 17:19Speaker 2: don't need the ampersand the memory is inherently shared across scopes.
- 17:23Speaker 1: That is a massive architectural distinction. Value types demand explicit
- 17:28Speaker 1: authorization to mutate via inout, while reference types are implicitly
- 17:33Speaker 1: mutable across scopes.
- 17:35Speaker 2: Right. And this concept of direct memory mutation becomes even
- 17:38Speaker 2: more explicit when you start interfacing with lower level cocoapis.
- 17:42Speaker 1: Oh yeah, because Coco heavily leverages legacy C and objective
- 17:46Speaker 1: C code. Right, So you frequently encounter APIs that require
- 17:50Speaker 1: direct manipulation of memory addresses.
- 17:53Speaker 2: Yeah, and swift safely bridges this via types like unsafe
- 17:57Speaker 2: mutable pointer.
- 17:58Speaker 1: I just love that the word unsafe is literally baked
- 18:00Speaker 1: right into the type name.
- 18:01Speaker 2: It's very on the nose, it is.
- 18:03Speaker 1: Newberg gives this great example of extracting the red, green, blue,
- 18:07Speaker 1: and alpha values from a UI color object. The API
- 18:10Speaker 1: function itself just returns a basic boolean for success.
- 18:14Speaker 2: Right true or false.
- 18:15Speaker 1: So to actually extract the real color data, you have
- 18:18Speaker 1: to create four empty variables and pass their raw memory
- 18:21Speaker 1: addresses into the function using those ampersands, and then the
- 18:25Speaker 1: legacy C code bypasses Swift safety checks reaches directly into
- 18:29Speaker 1: those addresses and just overwrites the memory with the floating
- 18:32Speaker 1: point color data.
- 18:33Speaker 2: The philosophy here is paramount. Though Swift doesn't forbid you
- 18:37Speaker 2: from executing dangerous low level memory operations like that.
- 18:40Speaker 1: It just wants you to be aware exactly.
- 18:42Speaker 2: It simply refuses to let you do it by accident.
- 18:45Speaker 2: By forcing you to use the ampersand or to interact
- 18:48Speaker 2: with types explicitly labeled unsafe, the compiler ensures you are
- 18:52Speaker 2: taking absolute responsibility for that memory mutation.
- 18:56Speaker 1: You can't blame the compiler if it crashes.
- 18:58Speaker 2: Nope, that one is on you.
- 19:00Speaker 1: So we've seen how functions strictly manage data, whether that's
- 19:03Speaker 1: copying value types, sharing reference types, or directly mutating memory.
- 19:08Speaker 1: But the most mind bending part of Swift, for me
- 19:10Speaker 1: at least, is when the data you are passing around
- 19:14Speaker 1: isn't a string or an object, it's the logic itself.
- 19:17Speaker 2: Oh yes, treating functions as first class citizens.
- 19:20Speaker 1: Function inception pretty much.
- 19:23Speaker 2: Because a function has a type defined by its signature,
- 19:25Speaker 2: the compiler actually allows you to use a function anywhere
- 19:28Speaker 2: you would use a standard value.
- 19:29Speaker 1: So you can assign a function to a variable yes, or.
- 19:32Speaker 2: You can pass it as an argument into another function entirely.
- 19:35Speaker 1: That's wild. Newberg highlights the Droy principle here don't repeat
- 19:39Speaker 1: yourself using local functions. So if you are writing this
- 19:42Speaker 1: massive calculation block and you realize you need to check
- 19:46Speaker 1: if a specific path is valid in like three different loops, yeah,
- 19:50Speaker 1: you don't copy and paste the logic.
- 19:52Speaker 2: Now you declare a tiny function completely nested inside.
- 19:56Speaker 1: Your main function, just living right in there.
- 19:58Speaker 2: Yep. And by declaring it locally you prevent polluting the
- 20:02Speaker 2: global name space. The helper logic is encapsulated exactly where
- 20:06Speaker 2: it is utilized, which keeps the overall architecture incredibly clean.
- 20:11Speaker 2: But the true power unlocks when you start passing these
- 20:14Speaker 2: functions as arguments to abstract away boilerplate code.
- 20:18Speaker 1: The image of cell sized drawing context example is perfect
- 20:21Speaker 1: for this. Oh that's a classic example, right, because when
- 20:23Speaker 1: you draw an image in code, you always have to
- 20:25Speaker 1: execute the exact same sandwich of operations. You open a context,
- 20:29Speaker 1: you can figure the memory, you extract the image, and
- 20:32Speaker 1: you close the context.
- 20:32Speaker 2: Yeah, the bread of the sandwich is always identical, exactly.
- 20:35Speaker 1: Only the drawing instructions in the middle the meat of
- 20:37Speaker 1: the sandwich change. So you write a utility function that
- 20:40Speaker 1: handles all the bread, and it accepts a function as
- 20:43Speaker 1: a parameter you pass in your unique drawing logic, and
- 20:47Speaker 1: the utility function executes it right in the middle of
- 20:49Speaker 1: the context.
- 20:50Speaker 2: And Swift streamlines this paradigm even further through anonymous functions,
- 20:55Speaker 2: which are commonly known.
- 20:56Speaker 1: As closures right closures.
- 20:58Speaker 2: If you are passing logic into an API to be
- 21:00Speaker 2: executed immediately or upon completion, you just don't need the
- 21:04Speaker 2: overhead of formally naming and declaring a function elsewhere in
- 21:08Speaker 2: your file.
- 21:09Speaker 1: You just write the logic in line exactly.
- 21:12Speaker 2: You strip away the name, you remove the funk keyword,
- 21:15Speaker 2: and you place the signature inside curly braces followed by
- 21:19Speaker 2: the keyword.
- 21:19Speaker 1: In It acts as this like on the fly pop
- 21:22Speaker 1: up function.
- 21:23Speaker 2: Yeah, you see this constantly with something like uiview dot animate,
- 21:27Speaker 2: where you pass one block of logic for the animation
- 21:29Speaker 2: and another block for what happens when it completes.
- 21:32Speaker 1: But the syntax compression goes much further than that, doesn't it.
- 21:35Speaker 2: It really does. Because Swift relies on strict static typing,
- 21:38Speaker 2: the compiler actually has immense contextual awareness. It often already
- 21:43Speaker 2: knows exactly what the signature of that closure must be
- 21:45Speaker 2: based on the API you are.
- 21:47Speaker 1: Calling, and this is where Swift's strict typing feels a
- 21:50Speaker 1: bit like magic. If the compiler knows the API requires
- 21:54Speaker 1: a completion handler that takes a boolean and returns nothing,
- 21:57Speaker 1: you can start dropping syntax. You drop the return type, right,
- 22:00Speaker 1: you drop the parentheses.
- 22:01Speaker 2: We drop the in keywer.
- 22:03Speaker 1: You can even drop the parameter names entirely and just
- 22:06Speaker 1: refer to the incoming boollion as zero dollars. Yep.
- 22:08Speaker 2: You compress what would normally be a multi line function
- 22:12Speaker 2: declaration into a single, highly readable statement like print zero dollars.
- 22:18Speaker 1: Okay, but wait, let me push back on this extreme
- 22:20Speaker 1: shorehand for a second. Sure, if we strip away the
- 22:23Speaker 1: name and the return type and the parameter labels and
- 22:26Speaker 1: we just use a dollar sign in a zero, doesn't
- 22:28Speaker 1: that completely shatter the strict cryptographic contract we just spent
- 22:32Speaker 1: twenty minutes praising. How does the compiler guarantee safety if
- 22:36Speaker 1: the code is entirely abstracted like that, Well.
- 22:38Speaker 2: This raises an important point. The strictness hasn't been shattered
- 22:41Speaker 2: at all, It has just been inferred.
- 22:43Speaker 1: Inferred, Okay.
- 22:44Speaker 2: The compiler analyzes the receiving functions required signature. So if
- 22:49Speaker 2: uiview dot anime strictly demands a closure of type bool
- 22:52Speaker 2: returning void. The compiler maps zero dollars directly to that
- 22:56Speaker 2: booleon requirement.
- 22:57Speaker 1: I see.
- 22:58Speaker 2: So if you try to call a string method on
- 23:00Speaker 2: zero dollars, the compiler will immediately throw an error because
- 23:03Speaker 2: it mathematically proved zero dollars is a boollion based on
- 23:06Speaker 2: the surrounding context. Wow, the type safety is absolute. The
- 23:09Speaker 2: compiler just saves you the keystrokes of reiterating it.
- 23:12Speaker 1: It's the ultimate payoff of the architecture. Because you established
- 23:16Speaker 1: the strict contracts upfront, the compiler trusts the flow of
- 23:19Speaker 1: data enough to let you write incredibly concise code exactly.
- 23:23Speaker 1: So what does this all mean when we step back
- 23:25Speaker 1: and look at the blueprint. Swift is fundamentally a language
- 23:28Speaker 1: of encapsulated state, those let and var shoe boxes, rigorously
- 23:32Speaker 1: managed in memory, and ironclad logic contracts acting upon that state.
- 23:37Speaker 1: But by embracing the compiler strict type checking rather than
- 23:41Speaker 1: fighting it, it allows you to strip away the messy
- 23:43Speaker 1: boilerplate and write beautiful, concise logic where the compiler acts
- 23:48Speaker 1: as your silent auditor.
- 23:49Speaker 2: That's a great summary. It demands absolute precision regarding your
- 23:53Speaker 2: inputs and outputs, and in return it grants you memory
- 23:56Speaker 2: safety and really profound structural flexibility.
- 24:00Speaker 1: Which leaves me with one final architectural puzzle for you
- 24:03Speaker 1: all to chew on. We started by establishing it in swift.
- 24:06Speaker 1: Absolutely everything is an object, a number, a straying obstruct everything.
- 24:11Speaker 1: But we just detailed how functions are treated exactly like values.
- 24:15Speaker 1: They could be passed around in variables injected into APIs.
- 24:19Speaker 1: So if everything is an object and functions are first
- 24:22Speaker 1: class citizens, does that imply a function itself or a
- 24:25Speaker 1: closure is technically an object. Because think about it. Closures
- 24:30Speaker 1: can capture and store references to variables from their surrounding context,
- 24:34Speaker 1: which means they are actively maintaining state.
- 24:36Speaker 2: Yeah, they are holding onto data.
- 24:38Speaker 1: Imagine building an entire app architecture where your closures aren't
- 24:41Speaker 1: just isolated logic blocks, but dynamic objects. They pass themselves
- 24:45Speaker 1: around in endless loops of behavior, roaming through your application,
- 24:48Speaker 1: retaining state and executing long after the original function that
- 24:52Speaker 1: created them has died. It fundamentally changes how you view
- 24:56Speaker 1: the life cycle of your code, and that is a
- 24:58Speaker 1: mechanism for you to mull over and ext flore in
- 25:00Speaker 1: your own codebase,