简体   繁体   English

Swift 2-创建数组数组

[英]Swift 2 - Create a array of arrays

Im trying to make an array with multiple arrays inside of it. 我正在尝试制作一个包含多个数组的数组。 Im using CloudKit to get the data. 我正在使用CloudKit来获取数据。

import UIKit
import CloudKit

var questionsCount = 0
var questionsArray = [String]()

class hvadvilduhelstViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    //firstfield.setTitle("Klik her for at starte!", forState: .Normal)
    //secondfield.setTitle("Klik her for at starte!", forState: .Normal)
    firstfield.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
    firstfield.titleLabel?.textAlignment = NSTextAlignment.Center
    secondfield.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
    secondfield.titleLabel?.textAlignment = NSTextAlignment.Center

    let container = CKContainer.defaultContainer()
    let publicData = container.publicCloudDatabase

    let query = CKQuery(recordType: "Questions", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
    publicData.performQuery(query, inZoneWithID: nil) { results, error in
        if error == nil { // There is no error
            for entry in results! {
                let firstOne = [entry["Question1"] as! String]
                let secondOne = firstOne + [entry["Question2"] as! String]
                let thirdOne = secondOne + [String(entry["Question1Rating"] as! Int)]
                let fourthOne = thirdOne + [String(entry["Question2Rating"] as! Int)]
                let fithOne = fourthOne + [String(entry["Reports"] as! Int)]
                questionsArray = questionsArray + fithOne
                print(questionsArray)

            }

        }
        else {
            print(error)
        }
    }

}

Using previous code I am getting this in the console output: 使用以前的代码,我在控制台输出中得到了这个:

["Dette er en test1", "Dette er en test2", "0", "0", "0", "test2", "test2", "0", "0", "0"]

instead of this (which is the output i want): 而不是这个(这是我想要的输出):

[["Dette er en test1", "Dette er en test2", "0", "0", "0"], ["test2", "test2", "0", "0", "0"]]

I simple can't figure out how to do this. 我简直不知道该怎么做。 My plan was to get a lot of records and put them inside of this single, huge array (to make it easy to use the 'value') Is there an easier/better way to do this? 我的计划是获取大量记录,并将它们放入单个庞大的数组中(以使其易于使用“值”)。是否有更简单/更好的方法来做到这一点?

Sorry for my english, not my native language. 对不起,我的英语不是我的母语。 Thanks for your help! 谢谢你的帮助!

When you have a problem like this, make a Playground and experiment and do a little thinking. 当您遇到这样的问题时,请创建一个Playground并进行实验,并进行一些思考。 Here is what you are doing, in essence: 本质上,这是您在做什么:

var arr = [String]()
for _ in (1...3) {
    let first = ["Mannie"]
    let second = first + ["Moe"]
    let third = second + ["Jack"]
    arr = arr + third
}
arr // ["Mannie", "Moe", "Jack", "Mannie", "Moe", "Jack", "Mannie", "Moe", "Jack"]

That isn't what you want, so don't do that. 那不是您想要的,所以不要那样做。 First, as your question title says, you want an array of arrays. 首先,正如问题标题所说,您需要一个数组数组。 Well then, you don't want to end up with a [String] ; 那么,您不想以[String]结尾; you just told us that you want a [[String]] (an array of arrays of strings)! 您刚刚告诉我们,您想要[[String]] (字符串数组的数组)! So first make that change: 因此,首先进行更改:

var arr = [[String]]()

Now, when you build your array and insert it into your array of arrays, use the append method (instead of the + operator): 现在,当您构建数组并将其插入到数组数组中时,请使用append方法(而不是+运算符):

arr.append(third)

Here's the result: 结果如下:

var arr = [[String]]()
for _ in (1...3) {
    let first = ["Mannie"]
    let second = first + ["Moe"]
    let third = second + ["Jack"]
    arr.append(third)
}
arr // [["Mannie", "Moe", "Jack"], ["Mannie", "Moe", "Jack"], ["Mannie", "Moe", "Jack"]]

Now go ye and do likewise in your real code. 现在去吧,在您的真实代码中也这样做。

Try this instead: 尝试以下方法:

import UIKit
import CloudKit

var questionsCount = 0
var questionsArray = [[String]]()

class hvadvilduhelstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //firstfield.setTitle("Klik her for at starte!", forState: .Normal)
        //secondfield.setTitle("Klik her for at starte!", forState: .Normal)
        firstfield.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
        firstfield.titleLabel?.textAlignment = NSTextAlignment.Center
        secondfield.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
        secondfield.titleLabel?.textAlignment = NSTextAlignment.Center

        let container = CKContainer.defaultContainer()
        let publicData = container.publicCloudDatabase

        let query = CKQuery(recordType: "Questions", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
        publicData.performQuery(query, inZoneWithID: nil) { results, error in
            if error == nil { // There is no error
                for entry in results! {
                    let firstOne = entry["Question1"] as! String
                    let secondOne = entry["Question2"] as! String
                    let thirdOne = String(entry["Question1Rating"] as! Int)
                    let fourthOne = String(entry["Question2Rating"] as! Int)
                    let fifthOne = String(entry["Reports"] as! Int)
                    questionsArray.append([firstOne, secondOne, thirdOne, fourthOne, fifthOne])
                    print(questionsArray)
                }
            }
            else {
                print(error)
            }
        }
    }
}

Notably, questionsArray is now of type [[String]] , not just [String] , and for each entry , you want to append a new [String] 值得注意的是, questionsArray现在的类型为[[String]] ,而不仅仅是[String] ,并且对于每个entry ,您都希望添加一个新的[String]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM