I keep running into this error. I’ve tried numerous things, could someone tell me where I’ve gone wrong at? heres my code:
let receiptFileURL = Bundle.main.appStoreReceiptURL
let receiptData = try? Data(contentsOf: receiptFileURL!)
let recieptString = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
guard let jsonDict: [String: AnyObject] = ["receipt-data" : recieptString! as AnyObject, "password" : IAPProduct.apiID.rawValue as AnyObject] else {
DispatchQueue.main.async {
self.performSegue(withIdentifier: "mainVC", sender: nil)
}
}
2
Answers
Since you are initializing the data, the
["receipt-data" : recieptString! as AnyObject, "password" : IAPProduct.apiID.rawValue as AnyObject]
cannot be nil, and thus it makes no sense to wrap it inguard
. The only optional you have in this statement isrecieptString
, which you are force-unwrapping (can be causing crash ifrecieptString!
is nil.So change your guard statement to
and then initialize the dictionary without guard or force unwrap:
You are going to create a non-optional dictionary so the error
reminds you that you cannot optional bind a non-optional type. Remove the
guard
expression.By the way a JSON collection type does never contain
AnyObject
values, this avoid the ugly bridge cast along the way, and you can omit theoptions
parameter inbase64EncodedString