gpt4 book ai didi

json - 如何解决错误 : "Invalid type in JSON write (NSConcreteData)"

转载 作者:搜寻专家 更新时间:2023-10-31 19:30:12 25 4
gpt4 key购买 nike

我正在尝试编写一些代码来验证我的 iOS 应用程序上的订阅。我正在学习本教程:http://savvyapps.com/blog/how-setup-test-auto-renewable-subscription-ios-app/

它不在 Swift 2.0 中,所以我不得不转换一些代码,但我在处理这一行时遇到了问题:

let requestData = try! NSJSONSerialization.dataWithJSONObject(receiptDictionary, options: NSJSONWritingOptions.PrettyPrinted) as NSData!

当它碰到那一行时,它会打印出这个错误信息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteData)'

这是整个函数:

func validateReceipt() {
print("Validating")

if let receiptPath = NSBundle.mainBundle().appStoreReceiptURL?.path where NSFileManager.defaultManager().fileExistsAtPath(receiptPath) {
print("Loading Validation")
let receiptData = NSData(contentsOfURL:NSBundle.mainBundle().appStoreReceiptURL!)
print(receiptData)
let receiptDictionary = ["receipt-data" :
receiptData!.base64EncodedDataWithOptions([]), "password" : "placeholder"]
let requestData = try! NSJSONSerialization.dataWithJSONObject(receiptDictionary, options: NSJSONWritingOptions.PrettyPrinted) as NSData!

let storeURL = NSURL(string: "https://sandbox.itunes.apple.com/verifyReceipt")!
let storeRequest = NSMutableURLRequest(URL: storeURL)
storeRequest.HTTPMethod = "POST"
storeRequest.HTTPBody = requestData

let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())

session.dataTaskWithRequest(storeRequest, completionHandler: { (data, response, connection) -> Void in

if let jsonResponse: NSDictionary = try! NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as?
NSDictionary, let expirationDate: NSDate = self.formatExpirationDateFromResponse(jsonResponse) {
print(expirationDate)
//self.updateIAPExpirationDate(expirationDate)
}
})
}
}

我以前没有进行过应用内购买,所以对此我有点陌生。预先感谢您的帮助!

最佳答案

错误消息清楚地表明字典中的值之一是 JSON 不支持的 NSData 对象。

这似乎是一个代码完成问题

替换

base64EncodedDataWithOptions([])

base64EncodedStringWithOptions([])

如链接文章中所写。


两个旁注:

  • dataWithJSONObject 返回 NSData 无论如何,类型转换是无用的,永远不会是隐式解包可选。
  • 服务器当然不关心漂亮的 JSON 打印。

关于json - 如何解决错误 : "Invalid type in JSON write (NSConcreteData)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35668488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com