0
之後將我的項目迅速3我已經運行時錯誤,當我嘗試解析JSON對象的NSArray這是我的代碼解析JSON對象的NSArray斯威夫特3
let receipt: Data! = try? Data(contentsOf: receiptURL)
if receipt == nil {//NSData(contentsOfURL: receiptURL, options: nil, error: nil)!
// validateReceipt(false)
return
}
let base64Data: String = receipt.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0)) as String
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last?.path//
do{
let payload: Dictionary = ["receipt-data" : base64Data as String, "password": sharedSecret as String ]
let receiptData = try JSONSerialization.data(withJSONObject: payload, options: JSONSerialization.WritingOptions.prettyPrinted)
let request = NSMutableURLRequest(url: URL(string: serverURL)!, cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy, timeoutInterval: 10) // ur website
request.httpMethod = "POST"
request.httpBody = receiptData as! Data?
var response: URLResponse?
let resultData = try NSURLConnection.sendSynchronousRequest(request as URLRequest, returning: &response)
do{
let json = try JSONSerialization.jsonObject(with: resultData, options: .mutableLeaves) as? NSDictionary
let statusCode = (json?.object(forKey: "status") as! NSNumber).intValue
switch(statusCode){
case 21000, 21002, 21003, 21004, 21006:
return
// break
case 21005:
//Server is not available: Save recipt data and try again later
return
case 21007:
validateReceipt(true)
return
case 21008:
validateReceipt(false)
return
default:
break
}
//print("**********************************")
//print("json :: ", json)
//print("**********************************")
if let parseJSON = json {
let latest_receipt_info:NSArray = (parseJSON.object(forKey: "receipt") as AnyObject).object(forKey: "in_app") as! NSArray
//var ms = 0
//parse the json reponse and add the objects into array
for i in 0 ..< latest_receipt_info.count {
let object:[String: String] = latest_receipt_info.object(at: i) as! [String : String]
let strMS = object["expires_date_ms"]
let prodId = object["product_id"]
if(prodId == iAPItems["LifeTime"] || prodId == iAPItems["Remove_Ads"]){
latestObjectsForEachProduct[prodId!] = object
}else{
if let oldTransaction = latestObjectsForEachProduct[prodId!] {
let strOldMS = oldTransaction["expires_date_ms"]
print("oldTransaction :: ",prodId, " :: ",strOldMS)
if(strMS!.toDouble() > strOldMS!.toDouble()){
//latestObject = object
latestObjectsForEachProduct[prodId!] = object
//ms = str!.toInt()
}
}else{
latestObjectsForEachProduct[prodId!] = object
}
}
}
錯誤出現在這條線
let latest_receipt_info:NSArray = (parseJSON.object(forKey: "receipt") as AnyObject).object(forKey: "in_app") as! NSArray
我不知道我應該怎麼做,如果任何一個能幫助
注意,迅速上2工作
非常感謝替換此代碼,,它的工作就像一個想 –