Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name convention for unwrapped value in Swift

Tags:

swift

When I unwrap a value in Swift, I'm always uncertain on how to name the variabile which will contain it:

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {

    if let unwrappedTouches = touches
    {
        ....
    }

}

Is there some popular-among-Swift-coders naming convention?

like image 339
Teejay Avatar asked Aug 24 '15 22:08

Teejay


1 Answers

You can assign the same name to an unwrapped variable as the optional.

Preferred:

var subview: UIView?
var volume: Double?

// later on...
if let subview = subview, let volume = volume {
  // do something with unwrapped subview and volume
}

Not preferred:

var optionalSubview: UIView?
var volume: Double?

if let unwrappedSubview = optionalSubview {
  if let realVolume = volume {
    // do something with unwrappedSubview and realVolume
  }
}

Taken from The Official raywenderlich.com Swift Style Guide. However these are just guidelines and other conventions may be just fine.

like image 58
Palle Avatar answered Oct 05 '22 00:10

Palle