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

标签 xcode swift error-handling fatal-error 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:我正在尝试获取应用内购买产品的本地价格。

最佳答案

来自DOCS如果 SKProduct 配置不正确,SKProductprice 可能为 nil

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

API 要求用户使用所有正确的值配置 SKProduct,否则当您尝试解开实际上为 nilprice 时,它会崩溃nil

<小时/>

用于测试:

我会写这样的内容:

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

关于xcode - fatal error : unexpectedly found nil while unwrapping an Optional value in swift(SKProduct),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28917116/

相关文章:

swift - Alamofire,使用未声明的类型

ios - 为 CocoaPods 的 pod 设置部署目标

ios - Xcode - ITMS 错误(90022、90023)

objective-c - Xcode 在没有时抛出随机错误

swift - UISlider 抛出 NSRangeException

ios - 没有 '..<' 候选人产生预期的上下文结果类型 'NSRange Swift 3

ios - 从 inputAccessoryView 到 tableView 的动画文本,如 iOS 11 消息应用程序

vb.net - 在VB中处理OpenFileDialog时出错

python - 如何在按下 Enter 后立即将 Kivy 输入文本分配给变量?

powershell - 错误处理 - PowerShell 脚本