Latest / CyberSecurity Summary / iOS 10 Programming Fundamentals with Swift: Swift, Xcode, and Cocoa Basics
Transcript
- 0:00Speaker 1: You know, if you were building iOS apps before before
- 0:04Speaker 1: twenty fourteen, imagine your development environment was sort of like
- 0:07Speaker 1: living in a grand historic mansion.
- 0:10Speaker 2: Oh yeah, a very very old mansion.
- 0:13Speaker 1: Right, and this house had been in the family for decades.
- 0:15Speaker 1: It had these amazing foundations, but over the years people
- 0:18Speaker 1: had just you know, bolted on these weird editions patches
- 0:22Speaker 1: everywhere exactly like there was a room you can only
- 0:25Speaker 1: access by climbing out a window, or the plumbing is
- 0:29Speaker 1: a total maze, and you constantly had to remember which
- 0:32Speaker 1: lights which shorted out the microwave.
- 0:34Speaker 2: That is an incredibly accurate description of the objective C
- 0:37Speaker 2: ecosystem back then.
- 0:39Speaker 1: It really was for a long time. That is exactly
- 0:41Speaker 1: what programming with objective C felt like. I mean, you
- 0:43Speaker 1: had bracket syntax everywhere, header files you had to manage.
- 0:46Speaker 2: Manually, oh of the header file.
- 0:48Speaker 1: Yeah, just so much historical baggage.
- 0:50Speaker 2: It was what you call a venerable legacy language. I mean,
- 0:52Speaker 2: it gave developers immense power and these really dynamic run
- 0:55Speaker 2: time capabilities, but it was absolutely full of tricky syntac
- 1:00Speaker 2: and well just traps for the unwary.
- 1:04Speaker 1: Yeah, you had to tread lightly.
- 1:05Speaker 2: You really did you just accepted that managing pointers manually
- 1:09Speaker 2: and dealing with super verbose method calls was you know,
- 1:13Speaker 2: the price of admission for building on an Apple platform.
- 1:15Speaker 1: And then at the WWDC keynote in twenty fourteen, Apple
- 1:19Speaker 1: just sent a massive shockwave through the entire developer community.
- 1:23Speaker 1: It was huge. They'd just announced like a new framework
- 1:27Speaker 1: or a minor syntax update for the old house. They
- 1:29Speaker 1: announced a total teardown, a complete blank slate, exactly, a clean, modern,
- 1:35Speaker 1: ground up rebuild that prioritized absolute clarity and safety. And
- 1:40Speaker 1: they called it Swift.
- 1:41Speaker 2: The audible gasp in the room when that slide hit
- 1:44Speaker 2: the screen is like legendary in the tech world.
- 1:47Speaker 1: I can imagine.
- 1:48Speaker 2: Nobody outside of a very small core team at Apple
- 1:50Speaker 2: saw it coming. I mean, developers were just completely accustomed
- 1:53Speaker 2: to the warts.
- 1:54Speaker 1: And the weight of objectivec They just figured they were
- 1:56Speaker 1: stuck with it forever.
- 1:57Speaker 2: Yeah, pretty much so. The idea that Apple had been
- 2:00Speaker 2: secretly developing this modern replacement for years was fundamentally paradigm shifting.
- 2:05Speaker 1: Okay, let's unpack this today. We are taking a deep
- 2:08Speaker 1: dive into the foundational architecture that clean rebuild.
- 2:11Speaker 2: It's going to be a fun one.
- 2:12Speaker 1: It really is. We are using Matt Nuberg's incredibly comprehensive
- 2:16Speaker 1: guide iOS ten programming fundamentals with Swift. Our mission here
- 2:21Speaker 1: is to give you a shortcut to understanding the matrix
- 2:24Speaker 1: behind modern iOS apps.
- 2:26Speaker 2: The real core rules.
- 2:28Speaker 1: Right. We're going to decode the underlying rules of Swift,
- 2:31Speaker 1: specifically looking at how objects, variable scopes, and functions operate.
- 2:36Speaker 1: And we're going to do it without drowning you in
- 2:38Speaker 1: dense code.
- 2:39Speaker 2: Nobody wants to hear raw code read aloud anyway, No,
- 2:42Speaker 2: definitely not.
- 2:43Speaker 1: Yeah, but if you want to understand how software architecture
- 2:46Speaker 1: actually dictates performance and safety, you are in the right place.
- 2:50Speaker 2: And Newbird's text is honestly the perfect roadmap for this.
- 2:52Speaker 2: He doesn't just show you how to write an app.
- 2:54Speaker 2: He deconstructs the actual philosophy of the language. Swift is
- 2:58Speaker 2: designed to be highly readable, documenting almost English like in.
- 3:02Speaker 1: Its method calls super friendly on the surface.
- 3:04Speaker 2: Exactly, But beneath that friendly modern surface is a very strict,
- 3:09Speaker 2: highly optimized engine. It has a rigid compiler that just
- 3:12Speaker 2: refuses to let you make the kinds of careless mistakes
- 3:15Speaker 2: objective c used to permit.
- 3:16Speaker 1: It won't let you get away with anything, not a chance.
- 3:19Speaker 1: So to understand that engine, we really have to start
- 3:22Speaker 1: with Swift's most radical foundational rule, and it's a structural pivot.
- 3:28Speaker 1: Swift is a purely object oriented language. Everything is an object,
- 3:32Speaker 1: right like, there are no exceptions.
- 3:34Speaker 2: And to fully grasp why that matters, you kind of
- 3:36Speaker 2: have to realize how different it is from older C
- 3:39Speaker 2: based languages.
- 3:40Speaker 1: Okay, how so, well, in C or.
- 3:43Speaker 2: Objective C you have primitive or scaler built in beta types,
- 3:48Speaker 2: things like simple integers or booleons.
- 3:50Speaker 1: Its raw data exactly.
- 3:52Speaker 2: They aren't objects. They're just raw pieces of data sitting
- 3:54Speaker 2: in memory. They don't have methods, they don't inherit from anything.
- 3:58Speaker 2: But in Swift there are no scalers, none at all. None.
- 4:01Speaker 2: Everything right down to a simple integer or a true
- 4:04Speaker 2: false boolean is treated as an object, which.
- 4:06Speaker 1: Means we are dealing strictly with three main flavors of
- 4:09Speaker 1: object types, right classes, strucks, and enoms.
- 4:13Speaker 2: That's the trio.
- 4:14Speaker 1: And here is where the architecture gets really interesting. Because
- 4:17Speaker 1: even a basic number like the digit one is a
- 4:20Speaker 1: struct behind the scenes, it's an object. And because everything
- 4:24Speaker 1: is an object, you can send messages to literally anything
- 4:27Speaker 1: using dot notation exactly.
- 4:29Speaker 2: The syntax model is totally uniform across the entire code base.
- 4:33Speaker 2: It's always the object than a dot, then the message
- 4:36Speaker 2: or method you're calling.
- 4:37Speaker 1: So the number one isn't just a raw digit sitting
- 4:39Speaker 1: in memory. You can write one dot description to immediately
- 4:43Speaker 1: get a string representation of it just like that. But
- 4:46Speaker 1: Newberg points out something even cooler. You can extend base types.
- 4:50Speaker 1: You could write an extension for the integer type in
- 4:52Speaker 1: your project so that you could literally type one dot
- 4:56Speaker 1: say hello, and the program would output hello, I'm one.
- 5:00Speaker 2: Yeah, you are modifying the fundamental behavior of numbers in
- 5:03Speaker 2: your app.
- 5:03Speaker 1: That's wild.
- 5:04Speaker 2: What's fascinating here is that this uniform structure creates extreme predictability.
- 5:10Speaker 2: In Swift, every noun in the language is an object,
- 5:13Speaker 2: and every verb is a message sent to that object.
- 5:15Speaker 1: Mounds and verbs. That's a great way to look at it, right.
- 5:19Speaker 2: There are no loose primitives floating around that require like
- 5:22Speaker 2: special handling. It forces a very clean mental model for
- 5:26Speaker 2: you as a developer. You always know exactly how entities interact,
- 5:30Speaker 2: whether it's a massive network controller or just the number
- 5:34Speaker 2: forty two.
- 5:34Speaker 1: Wait, I want to push back on that uniform model
- 5:36Speaker 1: for a second, though.
- 5:37Speaker 2: Okay, lay it on me.
- 5:38Speaker 1: If the number one is an object, yeah, and all
- 5:41Speaker 1: verbs are messages sent via dot notation. What about simple math?
- 5:47Speaker 1: What is the plus sign in one plus two? There's
- 5:49Speaker 1: no dot notation there. It looks exactly like c or
- 5:52Speaker 1: you know, third grade math class.
- 5:54Speaker 2: Yeah, that is where the compiler does some heavy lifting
- 5:56Speaker 2: to keep things readable. The plus sign is actually just
- 5:59Speaker 2: syntactic show. It's something called operator overloading.
- 6:02Speaker 1: Wait. Really, so the plus sign is literally just a
- 6:04Speaker 1: disguised method being called on the.
- 6:06Speaker 2: Object precisely under the hood. Operators like the plus sign,
- 6:10Speaker 2: the minus sign, or equality checks, they are just special
- 6:13Speaker 2: syntax for sending a message.
- 6:14Speaker 1: That's crazy.
- 6:15Speaker 2: When you write one plus two, you are literally sending
- 6:18Speaker 2: a message to the struct representing one, saying, execute your
- 6:22Speaker 2: internal addition method, pass in this other object the two
- 6:26Speaker 2: as the parameter, and return the result. Okay, Wow, The
- 6:30Speaker 2: language's internal logic remains perfectly consistent. It's all objects and messages,
- 6:35Speaker 2: but it hides that messaging behind familiar math symbols, so
- 6:38Speaker 2: your algorithms remain readable.
- 6:40Speaker 1: It doesn't look like a mess of dots in parentheses exactly.
- 6:43Speaker 2: And the LVM compiler optimizes this down to raw CPU
- 6:47Speaker 2: instructions anyway, so you don't take a performance hit for
- 6:49Speaker 2: the object oriented abstraction.
- 6:51Speaker 1: That is a massive architectural shift. It keeps the domain
- 6:54Speaker 1: model pure without sacrificing performance. Yeah okay, so if everything
- 6:58Speaker 1: in Swift is an object type, it's helpful to think
- 7:01Speaker 1: of those types as blueprints. Good analogy, But a blueprint
- 7:04Speaker 1: of a house isn't a house. Yeah right, you can't
- 7:06Speaker 1: live in it doesn't use memory. We need to move
- 7:08Speaker 1: from the abstract blueprint to concrete.
- 7:11Speaker 2: Reality, which brings us to the concept of instantiation. Declaring
- 7:15Speaker 2: an object type like a class or a struct just
- 7:18Speaker 2: tells the system what kind of thing could exist. It
- 7:20Speaker 2: defines the properties and methods. But to actually get one,
- 7:24Speaker 2: to allocate memory for it and use it in your app,
- 7:26Speaker 2: you have to instantiate it.
- 7:28Speaker 1: Newberg uses a classic example of a dog class. The
- 7:31Speaker 1: blueprint says, here is what a dog is. It has
- 7:34Speaker 1: a name property, and a bark method.
- 7:37Speaker 2: Basic dog stuff exactly.
- 7:39Speaker 1: But when you call dog with parentheses in your code,
- 7:43Speaker 1: you are telling that blueprint instantiate yourself. You're allocating memory
- 7:47Speaker 1: on the heap or the stack and bringing a specific, living,
- 7:51Speaker 1: breathing digital dog into existence.
- 7:53Speaker 2: And the primary reason we need these concrete instances is
- 7:56Speaker 2: to maintain what programmers call state state. Yeah, the dog
- 8:00Speaker 2: blueprint dictates that all dogs share the same behaviors, but
- 8:03Speaker 2: the instance is what holds the unique.
- 8:05Speaker 1: Data, right, the specific details.
- 8:07Speaker 2: That is what allows your first instance to hold the
- 8:09Speaker 2: name Fido its memory space, while your second instance holds
- 8:12Speaker 2: the name Rover. They share the logic, but their state
- 8:15Speaker 2: is entirely isolated.
- 8:17Speaker 1: And that isolated state is protected by a core principle
- 8:19Speaker 1: called encapsulation.
- 8:21Speaker 2: Yes, crucial concept.
- 8:23Speaker 1: The object hides its internal memory and mechanics from the
- 8:26Speaker 1: rest of the app. Newberg uses the example of a
- 8:29Speaker 1: stack data structure, like a stack of plates. You can
- 8:32Speaker 1: send it a push message to add an item or
- 8:35Speaker 1: a pop message to remove one, but the outside world,
- 8:38Speaker 1: the rest of your app, has absolutely no idea how
- 8:42Speaker 1: that stack is storing the items. Internally.
- 8:44Speaker 2: It could be in array, it could be a linked list.
- 8:47Speaker 1: Exactly, but the caller doesn't know and honestly doesn't care.
- 8:50Speaker 2: And that is a massive advantage for you when you're
- 8:53Speaker 2: maintaining a large codebase. Because the internal workings are hidden,
- 8:56Speaker 2: you can completely refactor how the stack stores data behind
- 9:00Speaker 2: the scenes.
- 9:00Speaker 1: You can totally get the inside of it right.
- 9:02Speaker 2: Maybe you find a much more memory efficient way to
- 9:05Speaker 2: handle the internal data. You can rewrite the inside of
- 9:07Speaker 2: that class, and you are guaranteed not to break the
- 9:10Speaker 2: rest of the application because the outside world only ever
- 9:13Speaker 2: interacts with the public API the push and pop methods.
- 9:17Speaker 1: It makes me think of like a high end restaurant kitchen.
- 9:20Speaker 2: Oh, I like that.
- 9:21Speaker 1: An object is basically the kitchen, the public API, the
- 9:25Speaker 1: methods you're allowed to call is the menu and the waiter.
- 9:29Speaker 1: You can order a meal that's sending a message, but
- 9:31Speaker 1: you are absolutely not allowed to walk through the swinging
- 9:34Speaker 1: doors into the kitchen, shove the executive chef aside, and
- 9:38Speaker 1: secretly swap out the salt for sugar.
- 9:41Speaker 2: That would end badly, very badly.
- 9:43Speaker 1: That private state the ingredients is strictly off limits.
- 9:47Speaker 2: Which brings us to access control, specifically the private keyword.
- 9:51Speaker 2: In Swift, you can explicitly lock the doors of your
- 9:55Speaker 2: object's namespace.
- 9:56Speaker 1: Just throw a dead ballt on it.
- 9:57Speaker 2: Pretty much. If your dog instance has a property for
- 10:01Speaker 2: say internal bark volume, you can market private. The dog
- 10:04Speaker 2: object can use it internally to calculate how loud to
- 10:07Speaker 2: bark based on its state, but no other part of
- 10:10Speaker 2: the code base can reach in and manipulate that integer directly.
- 10:13Speaker 2: It protects the integrity of the object's state.
- 10:16Speaker 1: Okay, so we spun up a concrete instance like Fido
- 10:19Speaker 1: the dog, we've protected his internal state using encapsulation and
- 10:22Speaker 1: private access control. But if we are spinning up thousands
- 10:26Speaker 1: of these instances in a complex app, how does iOS
- 10:29Speaker 1: track them without just drowning in memory leaks. We need
- 10:33Speaker 1: a way to reference them.
- 10:34Speaker 2: And this brings us to variables and scope.
- 10:37Speaker 1: Right.
- 10:37Speaker 2: Variables are essentially the named pointers we give to objects
- 10:41Speaker 2: or values so we can refer to them in our code.
- 10:43Speaker 2: They are the handles that let us grab onto the
- 10:46Speaker 2: memory we've allocated.
- 10:47Speaker 1: But in Swift, variables aren't just loosely created whenever you
- 10:50Speaker 1: feel like it. You have to declare them explicitly, and
- 10:54Speaker 1: you are forced to make a very specific architectural choice
- 10:57Speaker 1: right at the moment of creation, using one of two
- 11:01Speaker 1: keywords right.
- 11:02Speaker 2: Let or var let or var. This is one of
- 11:04Speaker 2: the most fundamental paradigm shifts. If you're coming to Swift
- 11:07Speaker 2: from an older language. If you declare your variable using let,
- 11:11Speaker 2: you are creating a constant.
- 11:12Speaker 1: It's locked thin.
- 11:13Speaker 2: The pointer is completely locked. The object or a value
- 11:16Speaker 2: you assigned to that variable can never be reassigned. But
- 11:19Speaker 2: if you use var for variable, you are explicitly allowing
- 11:23Speaker 2: that pointer to be reassigned to a new object later
- 11:26Speaker 2: in the execution.
- 11:27Speaker 1: And the Swift compiler is like completely obsessed with this distinction.
- 11:30Speaker 2: Oh, totally obsessed.
- 11:31Speaker 1: If you declare a variable with var, but the compiler's
- 11:34Speaker 1: static analysis notices you never actually reassign it anywhere in
- 11:38Speaker 1: your code, it will literally throw a warning just nagging
- 11:42Speaker 1: you to change it to let.
- 11:43Speaker 2: It's very boss.
- 11:44Speaker 1: It demands strictness. But why why does the compiler care
- 11:48Speaker 1: so much about whether I use let or var? If
- 11:51Speaker 1: the app works find either way.
- 11:53Speaker 2: It comes down to memory optimization and thread safety. When
- 11:56Speaker 2: the compiler knows a value is immutable, meaning it will
- 12:00Speaker 2: absolutely never change because it's marked with let, it doesn't
- 12:03Speaker 2: have to generate extra machine code to monitor that memory
- 12:06Speaker 2: space for changes. Like it doesn't have to worry about
- 12:09Speaker 2: two different background threads trying to change the variable at
- 12:11Speaker 2: the exact same millisecond and crashing the app. It can
- 12:14Speaker 2: store the data more compactly and access it faster at runtime.
- 12:18Speaker 2: It's pushing you to write CURD that is inherently safer
- 12:21Speaker 2: and highly optimized by default.
- 12:23Speaker 1: Here's where it gets really interesting. Let's talk about the
- 12:26Speaker 1: life span of these pointers. Variables have a scope and
- 12:30Speaker 1: a lifetime. In Swift, scope is usually dictated by structural
- 12:34Speaker 1: curly braces. So if I initialize a new dog instance
- 12:38Speaker 1: and assign it to a variable, but I do all
- 12:40Speaker 1: of that inside a simple if statement block, what happens
- 12:44Speaker 1: when the compiler hits the closing curly brace of that
- 12:47Speaker 1: if statement the end of the line. Do the pointer
- 12:50Speaker 1: and the memory just vanish?
- 12:51Speaker 2: They absolutely do. Newberg describes it perfectly. They vanish in
- 12:54Speaker 2: a puff of smoke that.
- 12:56Speaker 1: Feels almost ruthless compared to languages with looser memory management.
- 12:59Speaker 2: It is strict, but it's the bedrock of Swift's automatic
- 13:03Speaker 2: reference counting or ARC. The variable only lives as long
- 13:06Speaker 2: as the structural scope the curly braces surrounding it, So.
- 13:10Speaker 1: The second it steps outside those braces, once.
- 13:12Speaker 2: The program's execution steps out of those braces, the variable
- 13:15Speaker 2: goes out of scope. If that variable was the only
- 13:17Speaker 2: thing holding onto your dog instance, ARC immediately frees up.
- 13:21Speaker 1: That memory just deletes it.
- 13:23Speaker 2: It forces you to be very deliberate about the hierarchy
- 13:25Speaker 2: of your data, ensuring objects live exactly as long as
- 13:28Speaker 2: they are needed and not a millisecond longer, which prevents
- 13:31Speaker 2: memory blow.
- 13:32Speaker 1: Okay, so we have our instances, we store them securely
- 13:35Speaker 1: with strong memory constraints using variables, and we understand their lifetimes.
- 13:39Speaker 1: But an app isn't just static data.
- 13:41Speaker 2: No, it has to do things right.
- 13:43Speaker 1: It has to calculate, update the UI, fetch JSON from
- 13:46Speaker 1: a server. That executional logic happens exclusively inside functions.
- 13:51Speaker 2: Functions are the execution blocks of Swift. All your operational code,
- 13:56Speaker 2: the actual logic of your application, must live inside a
- 13:59Speaker 2: function or a method.
- 14:01Speaker 1: Newberg breaks down the anatomy of function into distinct parts.
- 14:04Speaker 1: He describes it kind of like an elementary school math
- 14:07Speaker 1: textbook machine. Right.
- 14:08Speaker 2: I love that imagery.
- 14:09Speaker 1: You have a hopper at the top for input the parameters.
- 14:12Speaker 1: You have gears in the middle where the body of
- 14:13Speaker 1: the function executes, and a tube at the bottom for
- 14:16Speaker 1: the output the return value.
- 14:17Speaker 2: And what governs that entire process is the function signature.
- 14:21Speaker 2: Think of it as a strict, unbreakable memory contract, a
- 14:25Speaker 2: contract yeah. For example, a signature that looks like int
- 14:28Speaker 2: int arrow int dictates exactly what types enter the function,
- 14:32Speaker 2: in this case two integers and exactly what type is
- 14:35Speaker 2: repern one integer, and Swift.
- 14:38Speaker 1: Puts a massive premium on readability here. It automatically externalizes
- 14:43Speaker 1: parameter names.
- 14:44Speaker 2: Yes, this is such a great feature.
- 14:46Speaker 1: So when you call the function in your code, it
- 14:48Speaker 1: reads clearly. You don't just pass random digits like echo
- 14:52Speaker 1: parentheses two comma three. You write out echo string col
- 14:56Speaker 1: and high times colon three.
- 14:57Speaker 2: It acts as self documenting code.
- 15:00Speaker 1: So a developer reading it six months later instantly knows
- 15:03Speaker 1: what the parameters do exactly.
- 15:05Speaker 2: Though it's also important to note that sometimes there is
- 15:08Speaker 2: no output. A function might return absolutely nothing, which Swift
- 15:12Speaker 2: denotes as void or just empty parentheses.
- 15:15Speaker 1: Why would you want a function that returns nothing?
- 15:17Speaker 2: You invoke these functions purely for their side effects. Maybe
- 15:20Speaker 2: they trigger an animation on the screen or save a
- 15:23Speaker 2: file to the disk, but they don't hand a piece
- 15:25Speaker 2: of data back to the caller out of that bottom too.
- 15:28Speaker 1: Got it? Now I want to ask about the rigidity
- 15:30Speaker 1: of these contracts. Why is Swift so incredibly strict about
- 15:33Speaker 1: these function signatures like if I pass a float instead
- 15:36Speaker 1: of an integer, it just flat out refuses to compile.
- 15:40Speaker 2: If we connect this to the bigger picture, it goes
- 15:41Speaker 2: right back to the flaws of objective C.
- 15:44Speaker 1: The old mansion.
- 15:44Speaker 2: The old mansion. In objective C, message passing was dynamic.
- 15:49Speaker 2: You could accidentally pass the wrong type of object to
- 15:51Speaker 2: a method. The compiler might just give you a mild
- 15:54Speaker 2: warning and let you build the app anyway.
- 15:56Speaker 1: That sounds dangerous, it was.
- 15:59Speaker 2: Because when the user runs it and the method tries
- 16:01Speaker 2: to operate on a text string when it actually expected
- 16:03Speaker 2: a number, it throws an unrecognized selector exception.
- 16:07Speaker 1: And the app hard crashes in the user's hands exactly.
- 16:10Speaker 2: Swift's strict static typing prevents this entirely. If the signature
- 16:15Speaker 2: demands an integer and you try to pass a string,
- 16:18Speaker 2: it simply will not build. It shifts the burden of
- 16:22Speaker 2: catching type errors from the user's run time back to
- 16:25Speaker 2: the developer's compile.
- 16:26Speaker 1: Time, which completely eliminates whole categories of crashes.
- 16:29Speaker 2: It's a lifesaver.
- 16:30Speaker 1: But let me ask a logic question about chaining these
- 16:33Speaker 1: execution blocks together. If a function requires an integer as
- 16:36Speaker 1: an input and another function returns an integer as an output.
- 16:40Speaker 1: Can I just shove a function inside a function like
- 16:43Speaker 1: nesting them? Yeah? Newberg has this example some and in
- 16:47Speaker 1: the parentheses four comma some parentheses five comma six foot
- 16:52Speaker 1: Does the compiler evaluate that seamlessly?
- 16:55Speaker 2: Yes, and it's completely reliant on that type safety we
- 16:58Speaker 2: just discussed. The compiler of evaluates the innermost function. First,
- 17:02Speaker 2: it runs the sum of five and six, verifies that
- 17:05Speaker 2: it returns an integer, in this case eleven, and then
- 17:08Speaker 2: drops that integer directly into the parameter of the outer
- 17:11Speaker 2: function alongside the four clean Because the compiler can mathematically
- 17:15Speaker 2: guarantee the types match the contract at every single step,
- 17:18Speaker 2: it allows for infinite safe composition of functions.
- 17:22Speaker 1: Strict type signatures, scope limits, constants that cannot mutate. But
- 17:29Speaker 1: just as you get comfortable with all these rigid boundaries,
- 17:31Speaker 1: Swift reveals it has some incredibly powerful mechanisms to subvert
- 17:34Speaker 1: these very rules when necessary.
- 17:36Speaker 2: This is where Swift goes from being merely a safe
- 17:39Speaker 2: language to a highly flexible, expressive one.
- 17:42Speaker 1: Let's look at modifiable parameters. We established earlier that when
- 17:45Speaker 1: you pass a value into a function's parameters, it's treated
- 17:48Speaker 1: as a constant inside that function.
- 17:50Speaker 2: Right, It is a let variable.
- 17:53Speaker 1: By default, the function can't change the original data you
- 17:55Speaker 1: passed in. But by using the keyword in out in
- 17:59Speaker 1: the function signature and passing a variable's memory address using
- 18:02Speaker 1: an ampersand symbol, a function can permanently alter a variable
- 18:06Speaker 1: that lives entirely outside of its own scope.
- 18:09Speaker 2: Yes, the amber sand is a critical piece of syntax here.
- 18:12Speaker 2: When you use it at the call site, it acts
- 18:14Speaker 2: as a mutation warning, like a red flag. Exactly. It
- 18:17Speaker 2: tells anyone reading the code, be careful. This function isn't
- 18:20Speaker 2: just reading this variable. It's going to mutate its underlying
- 18:24Speaker 2: memory directly. Newberg uses an example of a removed character function.
- 18:28Speaker 2: Oh right, You pass a string variable with an amber san.
- 18:32Speaker 2: The function processes, it strips out specific characters, and when
- 18:36Speaker 2: the function completes, your original string in the outer scope
- 18:39Speaker 2: is permanently modified.
- 18:40Speaker 1: It reaches right out of its own curly braces to
- 18:42Speaker 1: mutate external state. That's powerful, But the most mind bending
- 18:47Speaker 1: architectural feature has to be the concept of functions as
- 18:51Speaker 1: first class citizens.
- 18:52Speaker 2: This is a huge paradigm shift, especially for developers use
- 18:55Speaker 2: to languages where methods are just static blocks of execution.
- 19:00Speaker 2: In Swift, a function is not just an action. A
- 19:03Speaker 2: function is actually a value itself.
- 19:05Speaker 1: Meaning a function has a type, just like an integer
- 19:07Speaker 1: or a string has a type. Yes, the function's signature
- 19:10Speaker 1: is its type. And because it is treated as a
- 19:13Speaker 1: value in memory, you can assign an entire function to
- 19:16Speaker 1: a variable pointer. Or even crazier, you can pass a
- 19:20Speaker 1: function directly into the parameters of another function.
- 19:22Speaker 2: A classic example in everyday iOS development is uiview dot animate.
- 19:27Speaker 2: When you want to transition a button across the screen,
- 19:29Speaker 2: you don't just call a method and hope it works.
- 19:31Speaker 2: You pass an anonymous function, a block of executable code
- 19:35Speaker 2: often called a closure that doesn't even have a name,
- 19:38Speaker 2: directly into the animation methods parameters Wow, and you can
- 19:41Speaker 2: pass a second closure to tell the operating system exactly
- 19:45Speaker 2: what code to execute after the animation completely finishes. This
- 19:49Speaker 2: raises an important question, though, why would we ever want
- 19:52Speaker 2: to pass a function into another function. Isn't that just
- 19:55Speaker 2: adding unnecessary complexity to the code base.
- 19:57Speaker 1: Well, based on Newberg's text, it's actually about drastically reducing
- 20:02Speaker 1: complexity and solving the problem of boilerplate code. He use
- 20:06Speaker 1: is the example of drawing graphics contexts. If you want
- 20:09Speaker 1: to draw three different images. The steps to open a
- 20:12Speaker 1: drawing context, allocate the memory, extract the final image, and
- 20:16Speaker 1: close the context are perfectly identical every single.
- 20:19Speaker 2: Time, just repetitive, dizzy work exactly.
- 20:22Speaker 1: The only thing that changes is the specific lines, circles,
- 20:25Speaker 1: or colors you draw in the middle.
- 20:27Speaker 2: If you didn't have first class functions, you would have
- 20:29Speaker 2: to copy and paste that identical setup and tear down
- 20:32Speaker 2: code everywhere, which violates the dry principle don't repeat yourself.
- 20:36Speaker 2: But by passing a function as a parameter, you write
- 20:39Speaker 2: the repetitive context management once in a utility function. Then
- 20:43Speaker 2: you simply pass the unique drawing instructions in as a
- 20:46Speaker 2: closure parameter. The utility function sets up the canvas, executes
- 20:50Speaker 2: your custom closure, and cleans up the memory. It abstracts
- 20:54Speaker 2: away all the repetitive work.
- 20:56Speaker 1: So what does this all mean? Let me try an
- 20:57Speaker 1: analogy here, go for it. Passing a function as a
- 21:00Speaker 1: value into another function is exactly like handing a coworker
- 21:04Speaker 1: a sealed envelope with a list of instructions. You hand
- 21:07Speaker 1: them the envelope and say, don't read this right now,
- 21:10Speaker 1: don't execute these steps, just hold on to it. But
- 21:13Speaker 1: when the database finally finishes loading, open this envelope and
- 21:17Speaker 1: execute exactly what is written inside.
- 21:19Speaker 2: That is a brilliant way to conceptualize it. You are
- 21:22Speaker 2: passing the potential for.
- 21:23Speaker 1: Execution, the potential. I like it that.
- 21:25Speaker 2: You are packaging up a block of logic, capturing the
- 21:28Speaker 2: state it needs, and handing it off to be executed
- 21:30Speaker 2: at a later time or under specific asynchronous conditions by
- 21:34Speaker 2: whatever system you pass the envelope to. It is the
- 21:37Speaker 2: absolute backbone of asynchronous programming in Swift.
- 21:41Speaker 1: It's just an incredibly elegant way to handle execution flow.
- 21:44Speaker 1: We've covered a massive amount of architectural ground today.
- 21:47Speaker 2: We really have.
- 21:48Speaker 1: We started with a shocking pivot at WWDC, tearing down
- 21:51Speaker 1: the fragile dynamic house of objective C to build the
- 21:54Speaker 1: strictly typed compiled foundation. We explored the radical unifying idea
- 21:59Speaker 1: that absolutely everything, even a simple number, is an object
- 22:03Speaker 1: capable of receiving messages.
- 22:04Speaker 2: We looked at how abstract blueprints are allocated in memory
- 22:07Speaker 2: as concrete instances managing their own private state through strict
- 22:11Speaker 2: encapsulation right, and we decoded how scope dictates exactly when
- 22:16Speaker 2: that memory vanishes in a puff of smoke.
- 22:18Speaker 1: And Finally, we looked at how the ironclad contracts of
- 22:21Speaker 1: function signatures not only prevent run time crashes, but can
- 22:25Speaker 1: actually be leveraged to pass executable logic around your app
- 22:30Speaker 1: like sealed envelopes of instructions.
- 22:32Speaker 2: All ties together.
- 22:33Speaker 1: It does the next time you were using an app
- 22:36Speaker 1: and you cap a button that triggers a flawless, smooth animation.
- 22:40Speaker 1: I want you to visualize what's happening under the hood.
- 22:43Speaker 1: Think about the hidden instances managing their protected state, the
- 22:46Speaker 1: strict scope boundaries keeping the memory footprint perfectly optimized, and
- 22:50Speaker 1: those anonymous closures passing logic behind the scenes to make
- 22:53Speaker 1: the interface feel like magic.
- 22:55Speaker 2: It genuinely gives you a profound appreciation for the matrix
- 22:58Speaker 2: of rules and compiler opizations operating silently in your pocket
- 23:02Speaker 2: billions of times a second.
- 23:03Speaker 1: It really does. But before we wrap up, I want
- 23:06Speaker 1: to leave you with one final fascinating puzzle to mole over.
- 23:09Speaker 1: Straight from Newberg's text. We learned earlier that everything in
- 23:13Speaker 1: Swift is an object, and that includes strings, massive blocks.
- 23:17Speaker 2: Of texts right text strings.
- 23:19Speaker 1: But in Swift strings are specifically built as value types,
- 23:23Speaker 1: meaning they are strucks, not classes, and the defining rule
- 23:26Speaker 1: for value types is that they are not shared by
- 23:28Speaker 1: a single pointer. They are effectively copied entirely every single
- 23:33Speaker 1: time they are assigned to a new variable or passed
- 23:35Speaker 1: into a function.
- 23:36Speaker 2: That's right. Unlike classes, which are reference types that just
- 23:39Speaker 2: pass around appointed to the exact same instance in memory,
- 23:42Speaker 2: structs technically demand a brand new copy of themselves every
- 23:46Speaker 2: time they cross a scope boundary.
- 23:48Speaker 1: So think about the architectural implications of that. If a string,
- 23:51Speaker 1: say a massive ten page article of text in a
- 23:54Speaker 1: news app, is technically being copied over and over and
- 23:57Speaker 1: over again every single time gets passed around the complex
- 24:00Speaker 1: labyrinth of functions in an iOS app, how does your
- 24:03Speaker 1: iPhone not run out of memory instantly?
- 24:05Speaker 2: That sounds like it should, right exactly.
- 24:08Speaker 1: How does the processor survive the overhead of all that
- 24:11Speaker 1: constant memory duplication. It's a brilliant question about how the
- 24:16Speaker 1: Swift compiler secretly manages memory under the hood with a
- 24:19Speaker 1: trick called copy on write, A very clever trick, and
- 24:22Speaker 1: it just goes to show how much more depth there
- 24:24Speaker 1: is to explore when you dig into the differences between
- 24:26Speaker 1: value types and reference types on your own. Until next time,
- 24:30Speaker 1: enjoy exploring the clean modern architecture of Swift