简体   繁体   中英

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.

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. 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] ; you just told us that you want a [[String]] (an array of arrays of strings)! 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):

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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