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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Break Up Swift Concurrency
  • Security Is a Platform Property, Not a Pipeline Step
  • An XGBoost Property Valuation Postmortem: Leakage, Overfitting, and SHAP Surprises
  • Swift: Master of Decoding Messy JSON

Trending

  • One Database, Two Models: Building AI-Friendly Java Applications With MySQL JSON Duality Views
  • Taming Parameter Sniffing in SQL Server 2022 With PSP Optimization
  • Evals in CI: How to Catch Agent Regressions Before Production Does
  • Why Spark Executors Leave Cores Idle on YARN: A Packing Model for Executor Sizing

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Updated Apr. 24, 19 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
16.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in
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

  • How to Break Up Swift Concurrency
  • Security Is a Platform Property, Not a Pipeline Step
  • An XGBoost Property Valuation Postmortem: Leakage, Overfitting, and SHAP Surprises
  • Swift: Master of Decoding Messy JSON

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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