fatal error: unexpectedly found nil while unwrapping an Optional value in swift(SKProduct)

fatal error: unexpectedly found nil while unwrapping an Optional value in swift(SKProduct)

import UIKit
import SpriteKit
import StoreKit

extension SKProduct {
func localizedPrice() -> String {
    let formatter = NSNumberFormatter()
    formatter.numberStyle = .CurrencyStyle
    formatter.locale = self.priceLocale
    return formatter.stringFromNumber(self.price)!
}
}


class BuyCoin: SKScene {

var coin200a = SKProduct()

override func didMoveToView(view: SKView) {
NSLog("The price of this product is \(coin200a.localizedPrice())")
}
}

我做错了什么? PS: 我正在尝试获取应用内购买产品的本地价格。

如果 SKProduct 配置不正确,SKProductDOCS price 可能是 nil

var price: NSDecimalNumber! { get } // suppose crash happens here

API 要求用户使用所有正确的值配置 SKProduct 否则当您尝试解包 price 实际上是 nil[=20= 时它会崩溃]


用于测试:

我会这样写:

if self.price != nil{ // we don't unwrap price (fetch optional)
   return formatter.stringFromNumber(self.price!)!
} 
else{
  return ""
}