0

I am programmatically presenting a view controller and I am changing the value of a string array with a string array value stored in my SQL (gameGAMETABLEDATA). I am stuck on this error "Cannot assign a value of type 'String' to value of type [String]". I am not sure why this is happening. When I println(gameGAMETABLEDATA[index]), I get a string array, but swift is telling me that it is not a string array. Any help would be greatly appreciated.

var gameGAMETABLEDATA: [String] = []


override func viewDidLoad() {
    super.viewDidLoad()

            let index = indexPath.row

            //Error below!!! "Cannot assign a value of type 'String' to value of type [String]"
            GameViewController.tableData = gameGAMETABLEDATA[index]

        }

println(gameGAMETABLEDATA)[index] returns:

[cricket3.png, cricket1.png, cricket1.png, cricket1.png, cricket1.png, cricket1.png, cricket1.png]

8
  • if tableData is String array and gameGAMETABLEDATA is String array then use direct resultController.tableData = gameGAMETABLEDATA[index] or describe more Commented May 12, 2015 at 5:57
  • 1
    Please, add OnePlayerCricket class code to question Commented May 12, 2015 at 5:57
  • Ive updated this and made it simpler to focus on the direct issue. Commented May 12, 2015 at 6:03
  • What is the output for ` println(gameGAMETABLEDATA[index])` ? Commented May 12, 2015 at 6:05
  • [cricket3.png, cricket1.png, cricket1.png, cricket1.png, cricket1.png, cricket1.png, cricket1.png] Commented May 12, 2015 at 6:09

3 Answers 3

1

Your array declaration seems wrong. Do like below.

var gameGAMETABLEDATA: [[String]] = []

override func viewDidLoad() {
    super.viewDidLoad()
    let index = indexPath.row

    //Error below!!! "Cannot assign a value of type 'String' to value of type [String]"
    GameViewController.tableData = gameGAMETABLEDATA[index]

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

2 Comments

Your gameGAMETABLEDATA should be an Array of array as per your code.
Did it help you in solving your problem?
0

gameGAMETABLEDATA declared as array of strings (e.g. ["game 0", "game 1"]) it is ok that gameGAMETABLEDATA[i] is String (following our example gameGAMETABLEDATA[0] == "game 0")

If you need to show all contents of Game Table Data just remove braces in this line: GameViewController.tableData = gameGAMETABLEDATA If you need some specific data for single game you should use other data structure. Like

let gamesData = 
[
 ["title of game 0", "top score of game 0"],
 ["title of game 1", "top score of game 1"]
]

This structure is array of arrays of strings and particular array of strings can be accessed by gamesData[0] — this expression returns ["title of game 0", "top score of game 0"]

Comments

0

You are passing a single string to your tableData property. You can pass the whole array:

GameViewController.tableData = gameGAMETABLEDATA

wrap the result in square brackets to nest the string in an array:

GameViewController.tableData = [gameGAMETABLEDATA[index]]

or define your gameGAMETABLEDATA property as a matrix:

gameGAMETABLEDATA: [[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.