-1

i have small problem while sending data to API Calls, i have an Array its contains latitude and longitude values in single array, but my API not accepting the Array format because the data in array is in String Format but my API accept only Double Values

here my sample code:

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

                    }

my OutPut is :

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

my expected output is :

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

remove Double Quotes from Array or how to Add Double Values into NsmutableArray ?

user3458924
  • 105
  • 5
  • 18
  • 1
    You seem to be force unwrapping optionals quite a bit. Don't. You're just asking for a crash. Please learn [how to properly deal with them](http://stackoverflow.com/a/36360605/2976878). – Hamish Apr 11 '16 at 06:45

4 Answers4

1

You should convert your string to double before adding it to your array.

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

Because you are not getting your values correctly in teh first place, you should try sth. like this:

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)
    }
LoVo
  • 1,856
  • 19
  • 21
  • Probably because of your force unwrapping. Check were the crash exactly happens. – LoVo Apr 11 '16 at 06:37
  • my error : fatal error: unexpectedly found nil while unwrapping an Optional value "arrLatLong.addObject(yourDoubleValue!)" this line showing in higlited Red – user3458924 Apr 11 '16 at 06:39
  • So your str couldn`t be converted to Double. – LoVo Apr 11 '16 at 06:41
  • Check if the str is correctly set. I don`t see any keys for "lng" or "lat" in the output. But you use them to access the subdictionary values. – LoVo Apr 11 '16 at 06:46
  • bro i getting value from Google Map Api for ROUTES – user3458924 Apr 11 '16 at 06:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108781/discussion-between-lovo-and-user3458924). – LoVo Apr 11 '16 at 06:50
1

i found it

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)
user3458924
  • 105
  • 5
  • 18
0

If you want data in double then why are you storing as string. You can directly store number as double.

eg. 

let arr : NSMutableArray = NSMutableArray();
arr.addObject(NSNumber.init(double: 82.5621));
print("arr = \(arr)");
Mahendra
  • 8,448
  • 3
  • 33
  • 56
0

if your api accepting double why are you adding string in array. try this

 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)
    }
  }
Sahil
  • 9,096
  • 3
  • 25
  • 29