when i try to make transaction and its successful the response i get back is nill bellow is my code and error
func tranasctionSuccessful(flwRef: String?, responseData: [String : Any]?) {
let dataDic = responseData as NSDictionary
if(dataDic.get("status") == "success"){
self.customerVerified(flutterWaveToken: "")
}else{
self.customerVerified(flutterWaveToken: "")
}
}
here is the error x code gave me below
[String : Any]?' is not convertible to 'NSDictionary
CodePudding user response:
First of all, the error means that the way you change [String:Any]? which is optional value to Dictionary is wrong.
Secondly, in Swift you should prefer to use Dictionary instead of NSDictionary (you can read this in docs) and response value of "status" is type Any you should cast it into String to compare with "success"
The full code with check nil value will be like this
func tranasctionSuccessful(flwRef: String?, responseData: [String : Any]?) {
// check nil value
if (responseData == nil) {
return
}
let dataDic = responseData! as Dictionary // case optional value to non optional value
let status = dataDic["status"] as! String? // be sure that status response value is String
// check if not have status value
if (status == nil) {
return
}
if(status! == "success") {
self.customerVerified(flutterWaveToken: "")
} else {
self.customerVerified(flutterWaveToken: "")
}
}
If you be sure that response value is not nil && status is not nil you can shorten into this
func tranasctionSuccessful(flwRef: String?, responseData: [String : Any]?) {
let dataDic = responseData! as Dictionary
if(dataDic["status"] as! String == "success") {
self.customerVerified(flutterWaveToken: "")
} else {
self.customerVerified(flutterWaveToken: "")
}
}
More over: I think you should read about Dictionary and how to deal with response data from docs for further information. Apple provides us well document.