当值为null时,Swift Query Request崩溃

时间:2018-01-04 17:38:47

标签: ios swift

检索查询结果

 "Adjusted_Lease_Value__c" = "0.0";
    "Amount_Financed__c" = "23520.64";
    "Assignment_Amount__c" = "19220.21";
    "Category__c" = 4;
    "Charge_Off_Amount__c" = "0.0";
    "Committed_Funds__c" = "19220.21";
    "Date_Assigned_Back_to_ACG__c" = "<null>"

我是如何检索它们的:

// Initial Access to Salesforce in order to query data
client.performLogin(accessUsername, password: accessPassword, fail:{ (fail) in

}) { (success) in

self.queryResult = self.client.query(getCasesSQL2)

for o: Any in self.queryResult.records() {

// This line fails
let test = (o as AnyObject).fieldValue("Date_Assigned_Back_to_ACG__c") as! String

// This works no problem
let AmountFinanced = ((o as AnyObject).fieldValue("Amount_Financed__c") as! String

}

如果查询结果为“null”,则会导致应用崩溃。我该怎么办?

1 个答案:

答案 0 :(得分:0)

如果它可能为零,则不要使用强制转换。

self.queryResult = self.client.query(getCasesSQL2)

for o: Any in self.queryResult.records() {

   let test = (o as AnyObject).fieldValue("Date_Assigned_Back_to_ACG__c") as? String

   let amountFinanced = ((o as AnyObject).fieldValue("Amount_Financed__c") as? String

}