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;

    }
)
9
  • You stated that dictionarys : NSArray, you might want a dictionary instead. Commented Jan 28, 2016 at 14:28
  • are you looking for foreach? Commented Jan 28, 2016 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. Commented Jan 28, 2016 at 14:32
  • 2
    for (var i = 0;i<dictionarys.count ; i++) Argh, don't do this in Swift, please, never never never. Read the Swift Guide and see how to make a proper loop instead. Commented Jan 28, 2016 at 14:40
  • 1
    Look at the book, for item in array {print(item)} Commented Jan 28, 2016 at 14:45

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Thank you very much , i will check it out ! :)
@Kenneth Bruno, gave me new information with new way of handling ! thanks for helping :)
0

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

var nsmutablearrayVariableName = NSMutableArray.init(array: nsarrayVariableName)

2 Comments

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.
this method does exactly that, add each value to another array with initializing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.