Syntax link to sugar in Swift?

In c++

you can enter a link to the alias as follows:

StructType & alias = lengthyExpresionThatEvaluatesToStuctType;
alias.anAttribute = value; // modify "anAttribute" on the original struct

      

Is there a similar syntactic sugar for string (value) manipulation in Swift?

Update 1: For example: let's say that the structure is contained in a dictionary like [String: StructType] and that I like to change a few attributes in the myDict ["hello"] structure. I can make a temporary copy of this recording. Modify the copy and then copy the temporary structure back to the dictionary as follows:

var temp = myDict["hello"]!
temp.anAttribute = 1
temp.anotherAttribute = "hej"
myDict["hello"] = temp

      

However, if my function has multiple exit points, I would have to write myDict["hello"] = temp

before each exit point, and so it would be more convenient if I could just type and describe (link) for myDict["hello"]

like this:

var &  alias = myDict["hello"]! // how to do this in swift ???
alias.anAttribute = 1
alias.anotherAttribute = "hej"

      

Update 2: Before asking this question or closing it: Check out How To Quickly Build Type Valued Applications (from WWWDC15)! The value type is an important feature of Swift! As you probably know, Swift borrowed several features from c++

, and value types are the most important feature c++

(when c++

compared to Java and such languages). c++

Has some syntactic sugar when it comes to value types , and my questions are: does Swift

similar sugar have a similar sugar hidden in his language ?. I'm sure it Swift

will have, in the end ... Please don't close the vote on this question if you don't understand it!

I just read Deitel's book on Swift. While I'm not an expert (yet), I'm not really a novel. I try to use Swift as efficiently as possible!

+3


source to share


2 answers


Swift does not allow reference semantics to evaluate types, generally speaking, except when used as declared functions inout

. You can pass a struct reference to a function that runs on the inout version (I believe the quote is necessary that this is implemented as a copy-write, not a memory reference). You can also capture variables in nested functions for similar semantics. In both cases, you can return earlier from the mutating function while maintaining the appropriate assignment. Here is an example of a playground that I ran in Xcode 6.3.2 and Xcode 7-beta1:

//: Playground - noun: a place where people can play

import Foundation

var str = "Hello, playground"

struct Foo {
    var value: Int
}

var d = ["nine": Foo(value: 9), "ten": Foo(value: 10)]

func doStuff(key: String) {

    let myNewValue = Int(arc4random())

    func doMutation(inout temp: Foo) {
        temp.value = myNewValue
    }

    if d[key] != nil {
        doMutation(&d[key]!)
    }
}

doStuff("nine")
d // d["nine"] has changed... unless you're really lucky

// alternate approach without using inout
func doStuff2(key: String) {

    if var temp = d[key] {

        func updateValues() {
            temp.value = Int(arc4random())
        }

        updateValues()
        d[key] = temp
    }
}

doStuff2("ten")
d // d["ten"] has changed

      

You don't need to doMutation

nest the function in your outer function, I just did this to demonstrate that you can grab values ​​such as myNewValue

from the surrounding function, which can simplify implementation. updateValues

however, must be nested because it captures temp

.



Even though this works based on your sample code, I think using a class here (possibly a final class if you're concerned about performance) is really more idiomatically highly recommended by Swift.


You can, if you like, get the original pointer using a standard library function withUnsafeMutablePointer

. You can probably also add a value to an inner class that has only one member. There are also functional approaches that can mitigate the problem of early return.

+1


source


I'm not sure if I got your question, but hope it helps (typealias nameIWannaGive = String), String can be replaced with your structure as String is a struct type in swift



0


source







All Articles