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!
questionsArraycould never hold[["Dette er en test1", "Dette er en test2", "0", "0", "0"], ["test2", "test2", "0", "0", "0"]], because you have typed it as a[String], which is an array of strings. You would need a[[String]], an array of arrays of strings.