From Long Functions to Modular Code - Refactoring the User Account Creation Flow
Ⅰ.IntroductionIn my recent iOS project ChatApp , I refactored the account creation functionality. The original code was overly long and lacked modularity, making it difficult to maintain and extend. By refactoring the code, I improved its readability, maintainability, and reduced potential memory leak risks. In this blog, I will share a comparison of the code before and after the refactor, as well as the advantages of the refactor and the lessons learned. Here is the whole code before the re...
Taking more medicine doesn’t make you a doctor
“Taking more medicine doesn’t make you a doctor.” This simple sentence carries a quiet yet profound truth. On the surface, it appears obvious: one does not become a medical professional by virtue of being a patient. Swallowing pills, no matter how many, imparts no real understanding of anatomy, pathology, or healing. But the deeper implication of the phrase reaches far beyond medicine. It gently mocks a widespread illusion—that passive accumulation of experiences, repeated motions, or expo...
Persist
If you want to gain something, you have to give something in return. And if you choose to give, you must also learn to persist. If you truly feel it’s too hard, then give up — but if you choose to give up, don’t complain. That’s just how life is. The world is, in its own way, fair. Everyone must rely on their own effort to decide what kind of life they want to live.
Understanding Protocol-Delegate Pattern in Swift with Real Examples
Table of Contents Ⅰ. Introduction Ⅱ. What is a Protocol? Ⅲ. Why Use Protocols? Ⅳ. What is a Delegate? Ⅴ. How to Use the Protocol-Delegate Pattern in Swift Ⅵ. Protocol-Delegate Pattern in UIKit with Real Examples Example 1: RingViewDelegate – Custom View Delegation Example 2: WorkoutDelegate – View Controller Communication Ⅶ. Why Use the Protocol-Delegate Pattern in Swift? Ⅷ. Summary Ⅸ. References Ⅰ.IntroductionOne of the most powerful and elegant patterns in iOS development is the protocol...
When and Why to Use @escaping in Swift - A Practical Guide
Ⅰ.IntroductionIn my last article, Escaping vs Non-Escaping Closures in Swift, we discussed the difference between escaping and non-escaping closures in Swift. Today, we’ll take a deeper look at escaping closures through real-world examples. Core Concept Sentence:A non-escaping closure executes during the function execution, while an escaping closure executes after the function has returned. Ⅱ.Non-Escaping ClosuresLifecycle The closure is passed as a parameter to a function The closure is exec...
Escaping vs Non-Escaping Closures in Swift
Ⅰ.What is an Escaping Closure?Definition:An escaping closure is a closure that is passed into a function as a parameter, but may be called after the function has already returned.In other words, the closure “escapes” the function’s scope. When declaring a function that takes a closure parameter, you use the @escaping keyword to indicate that the closure is allowed to escape the function body. This means the closure “escapes” the function’s body. Ⅱ. Why Do We Need Escaping Closures? (Example: ...
Understanding Typealias in Swift
Ⅰ.IntroductionHave you ever encountered a situation where your function’s parameters — especially closure parameters — become overwhelmingly long and cluttered? For example: 123func handle(success: ((Int) -> Int)?, failure: ((Error) -> Void)?, progress: ((Double) -> Void)?) { } We can use typealias to helps you simplify complex types. Like this: 12345typealias Success = (Int) -> Inttypealias Failure = (Error) -> Voidtypealias Progress = (Double) ...
Understanding () -> Void in Swift
Ⅰ.What is () -> Void in Swift?It represents a closure that takes no parameters and returns no value.Let’s break it down: Part Meaning () No input parameters -> Indicates a return type Void No return value (Void means nothing) Ⅱ.In plain language:() -> Void means: “A block of code that takes nothing in, returns nothing out — it just runs.” Example:12345let sayHello: () -> Void = { print("Hello!")}sayHello() // Output: Hello! This sayHello is a c...
Trailing Closures in Swift
Trailing Closures in SwiftⅠ.IntroductionTrailing closures are one of Swift’s most distinctive and frequently used features, especially in asynchronous callbacks, animations, and data processing. Let’s dive deep into this essential concept. Ⅱ.What is a Trailing Closure?A Trailing Closure is a special syntax in Swift where,if the final parameter of a function is a closure, you can write that closure outside the parentheses of the function call. This makes your code cleaner and more readable. A...
Functions as First-Class Citizens
Functions as First-Class CitizensI. What Are “First-Class Citizens”?First-Class Citizens refer to entities (typically data types) in a programming language that can be used like variables. They must satisfy three key characteristics: Characteristic Meaning Assignable Can be assigned to variables or constants Passable as parameters Can be passed as arguments to other functions Returnable Can be returned from other functions In Swift, functions are first-class citizens! II. Demon...