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
- "Failed Initializers" on the Swift Blog.
- "Declarations" in the "Rapid Programming Language".
+16
source to share