2016-04-11 33 views
-2

發送數據到API調用時有一個小問題,我有一個Array,它包含單個數組中的緯度和經度值,但是我的API不接受Array格式,因爲數組中的數據在字符串格式,但我的API只接受Double值如何在Swift中將Double值添加到NSMutableArray中

這裏我的示例代碼:

for (var i=0 ; i<startLocationArray.count;i++) { 
        let dictLatLong = startLocationArray[i] as! NSArray 
        print("dictLatLong==\(dictLatLong)") 

        for dict in dictLatLong 
        { 
         let arr: NSArray = dict as! NSArray 
         for subdict in arr 
         { 
          let arrLatLong = NSMutableArray() 
          let str = String(subdict.valueForKey("lng")! as! Double)+", "+String(subdict.valueForKey("lat")! as! Double) 
          arrLatLong.addObject(str) 
          self.outputArray.addObject(arrLatLong) 
         } 

        } 

我的輸出是:

(
    "80.2622228, 13.125962" 
), 
    (
    "80.2617606, 13.1259684" 
), 
    (
    "80.2617484, 13.1229377" 
), 
    (
    "80.2592969, 13.1229812" 
), 
    (
    "80.2594669, 13.118439" 
) 

我的預期outpu t是:

(
    80.2622228, 13.125962 
), 
    (
    80.2617606, 13.1259684 
), 
    (
    80.2617484, 13.1229377 
), 
    (
    80.2592969, 13.1229812 
), 
    (
    80.2594669, 13.118439 
) 

從數組中刪除雙引號或如何將雙值添加到NsmutableArray中?

+1

你似乎是力展開自選不少。別。你只是要求一個崩潰。請學習[如何正確處理它們](http://stackoverflow.com/a/36360605/2976878)。 – Hamish

回答

1

你應該將你的字符串轉換爲double,然後再將它添加到你的數組中。

... 
let yourDoubleValue = Double(str) 
arrLatLong.addObject(yourDoubleValue) 

因爲你沒有正確地得到你的值,所以你應該嘗試一下。像這樣:

var arrLatLong : [Double] = [] 
let strings = [["80.2622228,13.125962"], 
       ["80.2617606,13.1259684"], 
       ["80.2617484,13.1229377" ], 
       ["80.2592969,13.1229812"], 
       ["80.2594669,13.118439" ]] 

     for element in strings { 
      let twoValueString = element.joinWithSeparator(",") 
      let valueArray = twoValueString.componentsSeparatedByString(",") 

      for point in valueArray { 
       if let value = Double(point) { 
        arrLatLong.append(value) 
       } 
      } 
     } 
     print(arrLatLong) 
    } 
+0

我做了這個,但我的應用程序崩潰 – user3458924

+0

可能是因爲你的力量解開。檢查是否發生了崩潰。 – LoVo

+0

我的錯誤:致命錯誤:意外地發現零,同時展開一個可選值「arrLatLong.addObject(yourDoubleValue!)」此行顯示在higlited紅色 – user3458924

0

如果你想要雙倍的數據,那麼你爲什麼要存儲爲字符串。您可以直接將數字存儲爲雙精度。

eg. 

let arr : NSMutableArray = NSMutableArray(); 
arr.addObject(NSNumber.init(double: 82.5621)); 
print("arr = \(arr)"); 
0

如果你的api接受double爲什麼你要在數組中添加字符串。 試試這個

for dict in dictLatLong 
  { 
    let arr: NSArray = dict as! NSArray 
     for subdict in arr 
      { 

let arrLatLong = NSMutableArray() 
    if let value = subdict.valueForKey("lng") as? NSString, value1 = subdict.valueForKey("lat") as? NSString { 
     let val = value.doubleValue 
     let val1 = value1.doubleValue 
     arrLatLong.addObject(val) 
     arrLatLong.addObject(val1) 
     print(arrLatLong) 
     self.outputArray.addObject(arrLatLong) 
     print(self.outputArray) 
    } 
    } 
+0

無法使用類型爲(任何對象)的參數列表類型'Double'調用初始化程序,我得到此錯誤 – user3458924

+0

已更新的答案。 – Sahil

+0

outputArray打印空值 – user3458924

0

我發現它

let str1 = String(subdict.valueForKey("lng")!) 
let str2 = String(subdict.valueForKey("lat")!) 

let floatLng = Double(str1) 
let floatLat = Double(str2) 

var arrLatLong1 : [Double] = [] 

arrLatLong1.append(floatLng!) 
arrLatLong1.append(floatLat!) 

self.outputArray.addObject(arrLatLong1) 
相關問題