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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  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.

Yoshua Elmaryono user avatar by
Yoshua Elmaryono
·
Apr. 24, 19 · Code Snippet
Like (1)
Save
Tweet
Share
14.75K 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.

Popular on DZone

  • Deploying Java Serverless Functions as AWS Lambda
  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL
  • How To Check Docker Images for Vulnerabilities
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: