Hamdouchi Interactive
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk

Unlocking the Power of Swift:
A Tech Blog Series

Welcome to our tech blog series dedicated to exploring the world of Swift and its related topics. Our goal is to provide valuable insights and in-depth analysis on the latest advancements in the Swift programming language, including design patterns and data structures. Stay tuned for our upcoming articles and join the discussion on the exciting world of Swift programming!

Explore all Swift Topics

Self vs. self: Understanding the Distinction in Swift

7/17/2023

0 Comments

 
Picture
In Swift, the difference between Self and self is an important one to understand.

Self is a special type in Swift that refers to the type of the current instance within a class or struct. It is often used as a return type for methods, as it allows the method to return an instance of the class or struct that it is being called on. For example:

    
Here, the introduce() method returns an instance of the Person class. This can be useful for method chaining, where multiple methods can be called on the same instance in a single line of code.

On the other hand, self is a special keyword in Swift that refers to the current instance of a class or struct within its own context. It is often used to disambiguate between a local variable and a property of the class or struct. For example:

    
In this example, self is used to refer to the name property of the Person class, rather than a local variable named ‘name’ that may have been defined within the introduce(to:) method.

It's important to note that Self and self have different meanings in Swift, and using the wrong one can result in compile-time errors. Always be sure to use Self when referring to the type of the current instance, and self when referring to the current instance itself.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Picture

    Mohamed Hamdouchi


    Author

    Lead iOS Engineer.
    I help develop Design System Libraries.
    Creative Thinker.
    Featured on the App Store with 4M+ downloads.


    Archives

    November 2023
    October 2023
    August 2023
    July 2023
    June 2023
    May 2023
    April 2023
    March 2023
    February 2023
    January 2023
    December 2022


    Categories

    All
    Abstract Factory
    Animation
    Array
    Associatetype
    Avl
    Behavioral
    Bridge
    Builder
    Case
    Chaining
    Chain Of Responsibility
    Class
    Closure
    Coalescing
    Coercion
    Command
    Composite
    Computed Property
    Conditional Conformance
    Crash
    Creational
    Data Structure
    Decorator
    Default Case
    Design Pattern
    Dictionary
    Enum
    Extension
    Facade
    Factory
    Factory Method
    Flyweight
    Function
    Generics
    Graphs
    Guard
    Hash Table
    Heap
    Initialization
    Interpreter
    Ios
    Iterator
    Linked List
    Lowercase
    Mapping
    Mediator
    Memento
    Observer
    Optional
    Pattern Matching
    Protocol
    Prototype
    Proxy
    Quadratic Probing
    Queue
    Search
    Self
    Sequence
    Singleton
    Stack
    State
    Strategy
    Strikethrough
    Struct
    Structural
    Subscript
    Swift
    Template Method
    Text
    Tree
    Tries
    Try
    UIKit
    Uilabel
    UITableView
    Uppercase
    Visitior

​​COPYRIGHT © 2009 HAMDOUCHI INTERACTIVE, LLC. ​ALL RIGHTS RESERVED
  • Welcome
  • Mobile
  • Tech Blog
    • Swift
    • SwiftUI
  • Let's Talk