DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Swift: Master of Decoding Messy JSON
  • Unlocking the Power of Reflection in Mobile Development
  • Advanced Usage of Decodable in Swift: Handling Dynamic Keys
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation

Trending

  • Compliance Automated Standard Solution (COMPASS), Part 11: Compliance as Code, the OSCAL MCP Server Way
  • A Practical Blueprint for Deploying Agentic Solutions
  • Frame Buffer Hashing for Visual Regression on Embedded Devices
  • A Deep Dive into Tracing Agentic Workflows (Part 2)
  1. DZone
  2. Coding
  3. Frameworks
  4. Simplest Way to Add Stored Property to Swift Extension

Simplest Way to Add Stored Property to Swift Extension

If you're interested in honing your iOS development skills, check out this Swift code snippet.

By 
Yoshua Elmaryono user avatar
Yoshua Elmaryono
·
Updated Apr. 24, 19 · Code Snippet
Likes (1)
Comment
Save
Tweet
Share
16.6K Views

Join the DZone community and get the full member experience.

Join For Free
class ExampleClass {}

fileprivate var storedProperty_FILEPRIVATE: [ObjectIdentifier:Int] = [:]
fileprivate var storedProperty_DEFAULT: Int = 0
extension ExampleClass {
    var storedProperty: Int {
        get {return storedProperty_FILEPRIVATE[ObjectIdentifier(self)] ?? storedProperty_DEFAULT}
        set {storedProperty_FILEPRIVATE[ObjectIdentifier(self)] = newValue}
    }
}

let a = ExampleClass()
let b = ExampleClass()
print(a.storedProperty) //0
b.storedProperty = 2
a.storedProperty = 1
print(a.storedProperty) //1
print(b.storedProperty) //2

That’s it. Now you can keep the variable definition close to where it’s used (in the extension, instead of in the class definition).

You just add two fileprivate variables or constants and access it in the extension using the computed variable.

The fileprivate access control ensures the variable storedProperty_FILEPRIVATE and storedProperty_DEFAULT are not global.

Of course, you can modify it as per your requirements (e.g. make it read-only, do validation before setting, etc.)

Unfortunately, I haven't found a way to make sure that the property is initialized when an object is instantiated and so we must provide a default value (after the ?? nil coalescing operator) in case we haven't initialized the storedProperty of the object.

If you don't want to provide a default value, you can just do it like below (notice that the return type become Int? instead of Int)

extension ExampleClass {
    var storedProperty: Int? {
        get {return storedProperty_FILEPRIVATE[ObjectIdentifier(self)]}
        set {storedProperty_FILEPRIVATE[ObjectIdentifier(self)] = newValue}
    }
}
Property (programming) Swift (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Swift: Master of Decoding Messy JSON
  • Unlocking the Power of Reflection in Mobile Development
  • Advanced Usage of Decodable in Swift: Handling Dynamic Keys
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook