-1

In C++ we can use a member initialization list you initialize class member like:

object::object():_a(0), _b(0){}

Is there a way to do this same thing in swift(2.0)?

matt
  • 515,959
  • 87
  • 875
  • 1,141
E. Spiroux
  • 430
  • 6
  • 15
  • this syntax is the "initialization list" but i don't know how to recreate it in swift – floppy12 Feb 09 '16 at 15:19
  • 2
    There's no such syntax in Swift; it's not needed. Initialise in the declaration or in your initialiser. (And those are not declarations, they're initialisations.) – molbdnilo Feb 09 '16 at 15:28
  • 1
    What is "this same thing"? C++ is C++. Swift is Swift. They have nothing to do with each other. If you want to use Swift, use _Swift_. The question is pointless and meaningless. Just think about what you want to do _in Swift_. – matt Feb 09 '16 at 16:01

1 Answers1

1

For swift to require you to init class member means that the class member is an optionnal, you have to supply proper init in the class init. For example :

class PhAuthenticationToken : NSObject {

    var authenticationToken : NSString
    var expiry : NSDate?
    var permissions : NSString

    init(token : NSString, secondsToExpiry seconds : NSTimeInterval, permissions perms : NSString) {

        self.authenticationToken = token
        if (seconds != 0) {
            self.expiry = NSDate(timeIntervalSinceNow: seconds)
        }
        self.permissions = perms
    }
}

You should check the official documentation : https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html

thanksd
  • 54,176
  • 22
  • 157
  • 150
thibaut noah
  • 1,474
  • 2
  • 11
  • 39