Why underscore and other syntax questions

Hello,


I am quite new to swift and therefore have some quite generall questions. I have the following code:


class Pokeman {
    private var _name: String!
    private var _pokedexId: Int!
   
    var name: String {
   
        return _name
   
    }
   
    var pokedexId: Int {
   
        return _pokedexId
   
    }
   
    init(name: String, pokedexId: Int) {
   
        self._name = name
        self._pokedexId = pokedexId
   
    }
}


My questions are why do you use "_"? Why use private in this case? And more generally why create two almost identical variables (only difference is the underscore and the use of private)?


Thanks in advance for the help!

That's an old practice in objC, to note backstore variable, and note the difference between _name and name in self.name = name ; but it's no more needed in Swift (could write self.name = name)


However, I sometimes use it in Swift to highlight that the var is really a local one.

Accepted Answer

Where did this code come from? It looks like an extremely literal translation of some Objective-C code, but this is not the way Swift code is normally written.


If the "name" and "pokedexId" properties are intended to be immutable both within class Pokeman and outside it, then you'd write something like this:


class Pokeman {
     let name: String
     let pokedexId: Int
     init (name: String, pokedexId: Int) {
          self.name = name
          self.pokedexId = pokedexId
     }
}


Or, if you want the properties to be mutable within the class, but immutable from outside it, you'd write:


class Pokeman {
     private(set) var name: String
     private(set) var pokedexId: Int
     init (name: String, pokedexId: Int) {
          self.name = name
          self.pokedexId = pokedexId
     }
}

It looks like an extremely literal translation of some Objective-C code, but this is not the way Swift code is normally written.

True. OTOH, I sometimes write code like this based on the nature of the values involved. For example, consider this code:

private var _session: URLSession? = nil
public var session: URLSession {
    if self._session == nil {
        let config = URLSessionConfiguration()
        self._session = URLSession(configuration: config)
    }
    return self._session!
}

where I want

session
to be non-optional but it needs to be backed by an optional value. And I use
_session
for that optional value because… well… history.

Note Some folks using

lazy
for this sort of thing but in my specific case I need to be able to invalidate the session, which set
_session
to nil.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

What you say is, of course, true, but the OP (a) is new to Swift, (b) didn't choose to use the underscore, (c) doesn't compute anything in the computed properties. Under those conditions, this is not normal Swift code.


With translated syntax, it's almost normal Obj-C code (it's old-school, though, because it doesn't use property synthesis). It's also almost a normal C# pattern, which is why I asked where it came from.

Great answer, thanks. The standard follow up question I guess is why should you have a immutable variable?

Actually, an immutable property, not a variable, if we're talking about instance members.


An immutable property can still be set in initial phase of an initializer. Its immutability doesn't prevent it being initialized to something, which could be a parameter passed in to the initializer, like a unique ID.

Why underscore and other syntax questions
 
 
Q