NSUUID with UUIDString initializer

I am trying to make NSUUID with a given value:

let uuidString        = "000000-0000-0000-0000-000000000000"
let beaconUUID:NSUUID = NSUUID(UUIDString: uuidString)

      

The second line contains the error message:

Additional argument "UUIDString" when called

Looking at the documentation it seems like it should work. Any ideas?

I added also as String

after uuidString

, same problem.

+3


source to share


3 answers


The error message is misleading. init?(UUIDString string: String)

is a failable initializer "and returns an optional parameter that needs to be expanded. So

let beaconUUID:NSUUID = NSUUID(UUIDString: uuidString)!

      

or simply

let beaconUUID = NSUUID(UUIDString: uuidString)!

      



works. If there is a chance the initialization will fail, use an optional binding:

if let beaconUUID =  NSUUID(UUIDString: uuidString) {
    // ...
} else {
    // failed
}

      

For more information see

+16


source


NSUUID(UUIDString: uuidString)

      

return an optional parameter so that you also declare your variable as optional.



let uuidString        = "000000-0000-0000-0000-000000000000"
let beaconUUID:NSUUID? = NSUUID(UUIDString: uuidString)

      

0


source


Actually, what is "!" in the fast? I know this does NOT mean, but how does this happen in this case? Anyone can explain this to me, I am new to Swift programming language.

0


source







All Articles