在NSArray中循环并将Object添加到NSMutableArray

时间:2016-01-28 14:22:18

标签: ios swift loops

我一直在努力在NSArray中循环解析它然后将它添加到NSMutableArray。问题不在于解析我想循环遍及整个NSArray。

这是我的代码:

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

我想做这样的事情:

            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)

}

我正在尝试解析的一个示例:print(dictionarys)

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

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

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

    }
)

3 个答案:

答案 0 :(得分:1)

我已经采用了您的JSON示例,并为您提供了一个非常简单的示例,让您了解正确的Swift循环,这可能是您需要适应实际代码库和数据的。

鉴于&#34; dictionarys&#34;是NSDictionaries的NSArray:

sort: {}

答案 1 :(得分:1)

我让你的例子更加Swifty并添加了一些模拟类来让它在一个简单的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
  }
}

那里有一些比较漂亮的东西,例如nil coalescing operator (??),但希望它可以作为一个例子。

答案 2 :(得分:0)

如果您尝试这样做,可以使用其他数组初始化数组。

var nsmutablearrayVariableName = NSMutableArray.init(array: nsarrayVariableName)