简体   繁体   中英

multidimensional arrays in swift inside a ViewController

I am relatively new to Swift and I guess the problem is more of a general problem regarding initialization.

I need to create a multidimensional array where I can access all values which are mostly strings in a UIViewController instance. See the following example with placeholders as Strings (which are supposed to be real words, but I tried to keep it simple until it works):

let firstArray = ["1","2","3"]
let secondArray = ["4","5","6"]
let thirdArray = [firstArray,secondArray]
let fourthArray = ["1","2","3"]
let fifthArray = ["4","5","6"]
let sixthArray = [fourthArray, fifthArray]
let seventhArray = [thirdArray, sixthArray]

in a Playground this runs fine and is accessible as i want it. But inside my ViewController instance the compiler returns the following error(s):

'myViewController.Type' does not have a member named firstArray

and the same for every time I try to place an array inside an array. Inside my .swift file it looks like this:

import UIKit

class tricktionaryViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    let firstArray = ["1","2","3"]
    let secondArray = ["4","5","6"]
    let thirdArray = [firstArray,secondArray]
    let fourthArray = ["1","2","3"]
    let fifthArray = ["4","5","6"]
    let sixthArray = [fourthArray, fifthArray]
    let seventhArray = [thirdArray, sixthArray]
    let textCellIdentifier = "TextCell"

//.... some functions following

}

I actually tried to create classes for the first level of arrays, but still the same problem. What am I doing wrong?

Since third array contains elements from stored properties, third array cant be a store property itself because its value its calculated based on stored properties values. So to make this work you have to declare third array ( and all other computed arrays) as immutable computed variables as follow

let firstArray = ["1","2","3"]
let secondArray = ["4","5","6"]
var thirdArray:[[String]] {
  return [firstArray,secondArray]
}

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