代码之家  ›  专栏  ›  技术社区  ›  husharoonie

值为null时Swift查询请求崩溃

  •  0
  • husharoonie  · 技术社区  · 8 年前

    检索到的查询结果

     "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
    
    }
    

    当查询结果为 “空” 它使应用程序崩溃。我该怎么办?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Pooja Gupta    8 年前

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

    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
    
    }