0

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!

1
  • Your questionsArray could 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. Commented Feb 15, 2016 at 23:56

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.