Using Returned JSON Status in an if statement (Checking IAP Auto Renew Receipt )

浪尽此生 提交于 2019-12-13 07:16:28

问题


Im want to check the current subscription status of an Auto Renew In App purchase. Im getting the receipt data via JSON, im after some advice on how to use/query the returned data

if let parseJSON = json {
                        println("Recipt \(parseJSON)")

                    }

Returns

Recipt {
    environment = Sandbox;
    status = 21004;
}

Whilst I know this wouldn't compile, I want to to do is something along the lines of an if statement like:

if parseJSON contains status = 21004 {

                           //Do something

                        }

回答1:


You might consider something like

if parseJSON["status"] as? Int == 21004 {
    // do something
}

This works because as? Int will automatically convert a NSNumber to a Swift integer, and because there is a version of == that accepts optional arguments.

public func ==<T : Equatable>(lhs: T?, rhs: T?) -> Bool



回答2:


You can use SwiftyJSON. It will be:

let parseJSON = JSON(data: json)
if parseJSON["status"] == "21004" {
//do something
}

If it not working, try to write parseJSON["status"].stringValue



来源:https://stackoverflow.com/questions/32039421/using-returned-json-status-in-an-if-statement-checking-iap-auto-renew-receipt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!