I'm tackling my first In-App Purchase set-up in my first Swift project. I'm trying to get an in-app bundle string from my UserDefaults, rather than bake it in as a fixed string, but I'm getting an error "fatal error: unexpectedly found nil while unwrapping an Optional value"
I'm calling my variable from the UserDefaults as follows:
var bundleIdentity : AnyObject!
NSUserDefaults.standardUserDefaults().synchronize()
var userDefaults = NSUserDefaults.standardUserDefaults()
if let myBundle: AnyObject? = userDefaults.objectForKey("bundle") {
bundleIdentity = myBundle as String
}
Now, I try to use it in my "getProductInfo" func:
func getProductInfo(){
if SKPaymentQueue.canMakePayments(){
let productID:NSSet = NSSet(object:self.bundleIdentity!)
let request:SKProductsRequest = SKProductsRequest(productIdentifiers: productID)
request.delegate = self
request.start()
}
}
I know the "bundleIdentity" is coming in, as I can printLn it as a string but I'm confused why I can't use it in the NSSet (object:self.bundleIdentity!) and get the error.
Any help would be much appreciated as I'm still a newbie to Swift!
Thanks
You have declared
bundleIdentity
as optional (using?
), and then you have told the machine that it definitely has a value (using!
). It is throwing a temper tantrum because you lied to it.