0

I have been trying so hard to loop in NSArray parse it and then add it to NSMutableArray. the issue is not with parsing i wanted to loop over the whole NSArray.

Here is my code:

 if let dictionarys : NSArray = dictionary.valueForKey("response") as? NSArray {

            var categoryNum = 0
            var currency = "0"

            if let category: NSArray = dictionarys.valueForKey("category") as? NSArray {
                if let catId = category.valueForKey("categoryID") as? NSArray {
                   categoryNum = catId[0] as! Int
                }
            }
            if let currency1: NSArray = dictionarys.valueForKey("currency") as? NSArray {
                if let currency2 = currency1[0] as? String {
                    currency = currency2
                }
            }

serviceObject = ServicesObject(categoryId: categoryNum,currency: currency)
CategoryItems.addObject(serviceObject)


}
self.tableView.reloadData()

i want to do something like this :

            for (var i = 0;i<dictionarys.count ; i++){

     var categoryNum = 0
                var currency = "0"

                if let category: NSArray = dictionarys.valueForKey("category") as? NSArray {
                    if let catId = category.valueForKey("categoryID") as? NSArray {
                       categoryNum = catId[0] as! Int
                    }
                }
                if let currency1: NSArray = dictionarys.valueForKey("currency") as? NSArray {
                    if let currency2 = currency1[0] as? String {
                        currency = currency2
                    }
                }

    serviceObject = ServicesObject(categoryId: categoryNum,currency: currency)
    CategoryItems.addObject(serviceObject)

}

a sample of what I'm trying to parse i did : print(dictionarys)

(
        {
        category =         {
            category = "<null>";
            categoryID = 66;
        };
        currency = 2;

    },{
        category =         {
            category = "<null>";
            categoryID = 66;
        };
        currency = 2;

    },{
        category =         {
            category = "<null>";
            categoryID = 66;
        };
        currency = 2;

    }
)
Nata Mio
  • 2,168
  • 3
  • 22
  • 46
  • You stated that `dictionarys : NSArray`, you might want a dictionary instead. – zc246 Jan 28 '16 at 14:28
  • are you looking for `foreach`? – Tobi Nary Jan 28 '16 at 14:28
  • the response i get it as NSArray and because i am using singleton class for http calls, the method call parameter is nsdictionary so when i get the response as NSArray i place it in NSDictionary only to send it for controller delegate. and then parse it over there. so its an NSArray, its just a name. – Nata Mio Jan 28 '16 at 14:32
  • If you just want to loop through, check [this](http://stackoverflow.com/a/992913/2710486) – zc246 Jan 28 '16 at 14:37
  • 2
    `for (var i = 0;i – Eric Aya Jan 28 '16 at 14:40
  • You should post the structure of the objects in your collection so that we can understand how you are trying to parse it. –  Jan 28 '16 at 14:40
  • @zcui93 Bleh. This is a very old answer for a different programming language. Not an ideal link. :) – Eric Aya Jan 28 '16 at 14:42
  • 1
    Look at the [book](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID107), `for item in array {print(item)}` – zc246 Jan 28 '16 at 14:45
  • I wanted to say that really i know how to loop in a simple examples in foreach or for loop , but this one i couldn't figure it out ! @EricD. i do use for each but it was only to show what i am asking for. – Nata Mio Jan 28 '16 at 14:54

3 Answers3

1

I've taken your JSON sample and made this very simple example for you to get the idea of a proper Swift loop, this is probably what you need to adapt for your actual code base and data.

Given that "dictionarys" is an NSArray of NSDictionaries:

for dict in dictionarys {
    if let curr = dict["currency"] as? Int {
        print("Currency: \(curr)")
    }
    if let category = dict["category"] as? NSDictionary {
        if let ID = category["categoryID"] as? Int {
            print("ID: \(ID)")
        }
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
1

I made your example a bit more Swifty and added some mocked classes to get it all to work in a simple Playground:

import Foundation

class ServicesObject {
  var categoryId: Int
  var currency: String
  init(categoryId: Int, currency: String) {
    self.categoryId = categoryId
    self.currency = currency
  }
}

class CategoryItems {
  static func addObject(item: ServicesObject) {
    print("Added categoryID \(item.categoryId), currency \(item.currency)")
  }
}

let dictionary: NSArray = [
  ["response":
    ["category":
      ["category":"<null>",
        "categoryID":66],
     "currency": "2"]],
  ["response":
    ["category":
      ["category":"<null>",
        "categoryID":67],
    "currency": "3"]],
  ["response":
    ["category":
      ["category":"<null>",
        "categoryID":68],
    "currency": "4"]]]

if let responses = dictionary.valueForKey("response") as? NSArray {
  for response in responses where response is NSDictionary { // loop over all responses
    var categoryNums = 0
    if let category = response["category"] as? NSDictionary {
        categoryNums = category["categoryID"] as? Int ?? 0
    }
    var currency = response["currency"] as? String ?? "0"
    let serviceObject = ServicesObject(categoryId: categoryNums, currency: currency)
    CategoryItems.addObject(serviceObject) // add all responses
  }
}

There's some fancier stuff in there such as the nil coalescing operator (??) but hopefully it will work as an example.

0

You can initialize array with another array if you trying to do that try this.

var nsmutablearrayVariableName = NSMutableArray.init(array: nsarrayVariableName)
turushan
  • 690
  • 7
  • 25
  • i didn't ask to initialize another array , i want to loop in my NSArray values and add each time the object in Another NSMutableArray. so this is not an answer. – Nata Mio Jan 28 '16 at 14:35
  • this method does exactly that, add each value to another array with initializing. – turushan Jan 29 '16 at 19:57