Multiple variable assignment in Swift

24,455

Solution 1

You don't.

This is a language feature to prevent the standard unwanted side-effect of assignment returning a value, as described in the Swift book:

Unlike the assignment operator in C and Objective-C, the assignment operator in Swift does not itself return a value. The following statement is not valid:

   if x = y {
       // this is not valid, because x = y does not return a value
   }

This feature prevents the assignment operator (=) from being used by accident when the equal to operator (==) is actually intended. By making if x = y invalid, Swift helps you to avoid these kinds of errors in your code.

So, this helps prevent this extremely common error. While this kind of mistake can be mitigated for in other languages—for example, by using Yoda conditions—the Swift designers apparently decided that it was better to make certain at the language level that you couldn't shoot yourself in the foot. But it does mean that you can't use:

blah = blah2 = 3

If you're desperate to do the assignment on one line, you could use tuple syntax, but you'd still have to specifically assign each value:

(blah, blah2) = (3, 3)

...and I wouldn't recommend it. While it may feel inconvenient at first, just typing the whole thing out is the best way to go, in my opinion:

blah = 3
blah2 = 3

Solution 2

As the accepted answer says: You can get some tighter syntax, however you cannot assign from a to b to c without using multiple lines (for safety, probably). Here's an example of some more concise syntax to declare and assign multiple variables in one line:

var red, green, blue, alpha : CGFloat
(red, green, blue, alpha) = (0.0, 0.0, 0.0, 0.0)
ledColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
Share:
24,455

Related videos on Youtube

hamobi
Author by

hamobi

Web developer at work. Wannabe game developer at home =/ check out my corona sdk game Mega Hasan on the app store / play store check out my spritekit game Kill Sector on the app store

Updated on April 03, 2020

Comments

  • hamobi
    hamobi about 4 years

    How do I assign multiple variables in one line using Swift?

        var blah = 0
        var blah2 = 2
    
        blah = blah2 = 3  // Doesn't work???
    
  • dave
    dave about 8 years
    This is the kind of heavy-handed stuff that I feel like appears in "early / young" languages all the time. The gods of the language make it impossible to write code they subjectively find objectionable (this being one example, another being the necessity to prefix floats with 0 [try writing let x = .5 in swift]).
  • Kyle KIM
    Kyle KIM almost 8 years
    blah = 3; blah2 = 3;
  • anoo_radha
    anoo_radha over 6 years
    Could u please explain the above code. really i couldnt decipher it :)
  • Dan Rosenstark
    Dan Rosenstark over 6 years
    Did you understand the first two lines?
  • Dan Rosenstark
    Dan Rosenstark over 6 years
    @anoo_radha OK the answer is here: stackoverflow.com/questions/30541244/… Basically it's a way for the called method to modify the input parameter, so the method actually shoves the values into red etc.
  • anoo_radha
    anoo_radha over 6 years
    thanks @Dan Rosenstark for the reference to inout parameters
  • d4Rk
    d4Rk over 6 years
    Good to know, that this is possible. Even though it looks anything but nice to me ;-)
  • Doug Null
    Doug Null about 2 years
    dave: Agreed; this is not a feature, but a detraction from time-saving development efficiency, caught in the spirit of 'C' programming. 'gods' of new languages should consult the devils of prior languages for development efficiency wisdom.