UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Is there a way to create an alias for a property?

Forums > Swift

I run into this situation constantly: there is some property that I need to access frequently, and accessing that property requires an obnoxious amount of syntax, so my code ends up being 10x bulkier (and less readable) than in needs to be.

For example, let's say there is a property, called "property1", that can be accessed using the following syntax:

"exampleDictionary[exampleKey].exampleObject.exampleArray[exampleIndex].property1"

...so any time I want to access this property I have to write this extremely long syntax. It would be so so great if there were I way to define an alias for this property, something like this:

propertyalias prop = exampleDictionary[exampleKey].exampleObject.exampleArray[exampleIndex].property1

that way if I wanted to multiply property1 by 2, for example, instead of having to write all this:

exampleDictionary[exampleKey].exampleObject.exampleArray[exampleIndex].property1 = exampleDictionary[exampleKey].exampleObject.exampleArray[exampleIndex].property * 2

I could just write this:

prop = prop * 2

which is WAY more readable.

Is there a way to do this?

2      

You can create a computed property:

var property1: TypeOfPropertyOne {
  exampleDictionary[exampleKey].exampleObject.exampleArray[exampleIndex].property1  ?? DefaultValueIfNil
}

3      

@Hatsushira has it right: a computed property is what you need.

There is one additional thing you need, however. Computed properties are usually read-only. In order to do something like this:

prop = prop * 2

you will need to provide a setter for the computed property.

So:

var prop: TypeOfProperty {
    get {
        //this is basically what a default computed property is
        exampleDictionary[exampleKey].exampleObject.exampleArray[exampleIndex].property1  ?? DefaultValueIfNil
    }
    set {
        //NOTE: this is an example only, I didn't bother accounting for the optionals and such
        exampleDictionary[exampleKey].exampleObject.exampleArray[exampleIndex].property1  =
            exampleDictionary[exampleKey].exampleObject.exampleArray[exampleIndex].property1 * 2
    }
}

3      

@Hatsushira, @roosterboy, you guys are awesome, thank you. All of my code is about to become way easier to work on. I have been using computed properties for a while but I never realized they were the solution to this problem too. Thank you for showing me that I can use "set" with a computed property that was the key!

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.