在数组中组织多种类型的数据swift 2

时间:2016-02-26 23:03:34

标签: arrays swift swift2

我是iOS开发的新手。我正在尝试执行以下操作: 存储标题,允许在其下添加任意数量的子类别。每个子类别都需要附加2个整数。我需要能够使用,编辑,删除和添加新标题,子类别和整数。

“标题”:
    “子类别”:int,int
    “子类别”:int,int

“标题”:
    “子类别”:int,int
“子类别”:int,int
“子类别”:int,int
    “子类别”:int,int

我已经尝试了几次结构和数组。 例如:

    struct everyThing {
        var titles = ["Title 1", "Title 2", "Title 3"]
        var subcategories = ["subcat1", "subcat2", "subcat3", "subcat4", "subcat5"]
        var integers: [Double] = [5.0,10.0,7.0,15,3,6,7,8,12,14,13,15]
    }

    struct grouping1{
        var title = everyThing().titles[0]
        var subcategories = everyThing().subcategories[0..<2]
        var integers = everyThing().workRest[0..<2]
    }
    struct grouping2{
        var title = everyThing().titles[1]
        var subcategories = everyThing().integers[2..<4]
        var integers = everyThing().integers[2..<4]
    }

无法跟踪和缩放,因为可以在特定标题下添加任意数量的子类别。

有关组织此数据的最佳方法的任何想法吗?如果这太模糊,请告诉我。

2 个答案:

答案 0 :(得分:1)

您可以使用dictionary [String : [String : (Int, Int)]]

let dictionary: [String : [String : (Int, Int)]] = [
  "Title1" : [
    "subcat1" : (5, 10),
    "subcat2" : (7, 15)
  ],
  "Title2" : [
    "subcat3" : (3, 6),
    "subcat4" : (7, 8),
    "subcat5" : (12, 14)
  ]
]

要在类别和子类别下获取整数元组(Int, Int),可以使用

let tuple: (Int, Int) = dictionary[title]![subcategory]!

但是,这使用!强制解包。相反,更安全的方法是不会导致应用程序崩溃

let tuple: (Int, Int)? = dictionary[title]?[subcategory]

然后,要获取元组中的值,您可以使用

let val1: Int? = tuple?.0
let val2: Int? = tuple?.1

要在值不存在时设置值 0 而不是nil,您可以使用??运算符

let val1: Int = tuple?.0 ?? 0
let val2: Int = tuple?.1 ?? 0

如果您想遍历所有值,可以使用

完成
for title in dictionary.keys{
  for subcategory in  dictionary[title]!.keys{
    //we can force unwrapping because we are sure the
    //value will not be nil, because we are looping
    //through the keys of the dictionary

    let value1: Int = dictionary[title]![subcategory]!.0
    let value2: Int = dictionary[title]![subcategory]!.1

    //use title, subcategory, value1, and value2 as you please
  }
}

设置值就像

一样简单
dictionary["newOrExistingTitle"]["newOrExistingSubcategory"] = (num1, num2)

例如

dictionary["Title1"]["subcat2"] = (8, 2)

答案 1 :(得分:0)

您需要2个模型值

struct SubCategory {
    let title: String
    let value0: Int
    let value1: Int
}

struct Category {
    var title: String
    private (set) var subcategories = [SubCategory]()

    init(title:String) {
        self.title = title
    }
}

让我们看看你现在能做些什么。

添加2个类别

var categories = [Category]()
let category0 = Category(title: "Category 0")
categories.append(category0)
let category1 = Category(title: "Category 1")
categories.append(category1)

// [Category(title: "Category 0", subcategories: []), Category(title: "Category 1", subcategories: [])]

添加子类别

var cat = categories[0]
cat.subcategories.append(SubCategory(title: "Sub0", value0: 1, value1: 2))
cat.subcategories.append(SubCategory(title: "Sub1", value0: 3, value1: 4))
categories[0] = cat

// [Category(title: "Category 0", subcategories: [SubCategory(title: "Sub0", value0: 1, value1: 2), SubCategory(title: "Sub1", value0: 3, value1: 4)]), Category(title: "Category 1", subcategories: [])]

更改类别标题

var cat = categories[0]
cat.title = "New title"
categories[0] = cat

// [Category(title: "New title", subcategories: []), Category(title: "Category 1", subcategories: [])]