0

I am trying to update a string array with data from SQLite. The data I am trying to use is called gameGAMETABLEDATA, which is a string array. I am able to save a string array as gameGAMETABLEDATA in SQLite with an action, and when I println(gameGAMETABLEDATA), it returns:

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

So the value inside it is a string array. However, Xcode does not identify this value in SQLite as a string array, but rather as a string. I know so because when I try to change GameViewController.tableData to match gameGAMETABLEDATA[index] there is an error.

SavedGames.swift

var gameGAMETABLEDATA: [String] = []

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

    }

GameViewController.swift

var tableData: [String] = ["cricket1.png", "cricket1.png", "cricket1.png", "cricket1.png", "cricket1.png", "cricket1.png", "cricket1.png"]

What is going on? When I do GameViewController.tableData = [gameGAMETABLEDATA[index]] it doesn't work. Any help would be nice. Thank you.

3
  • Could it be because I am storing gameGAMETABLEDATA as a text? Do I need to store this as an array? SQLite code: let sql_stmt = "CREATE TABLE IF NOT EXISTS GAME (ID INTEGER PRIMARY KEY AUTOINCREMENT, PLAYERS TEXT, GAMETYPE TEXT, gameGAMETABLEDATA TEXT)" Commented May 13, 2015 at 1:01
  • I would reconsider using sqlite directly. Have you considered realm or something similar? Commented May 13, 2015 at 1:10
  • I have. This is a class project that requires SQL. Commented May 13, 2015 at 1:58

1 Answer 1

2

SQLite supports a limited number of column types, although:

Any column can still store any type of data.

https://www.sqlite.org/datatype3.html

SQLite doesn't support array columns, but text is rich (and BLOBs are richer). You can use this richness to serialize and deserialize almost anything.

In your case, it looks like you care about an array of strings that you control (referencing files you control). Assuming your files names all share the absence of a character in them, you can join them into a single string using such a delimiter (e.g., a comma), and serialize the result:

let files = ["cricket1.png", "cricket2.png" /* … */]
let text = files.joined(separator: ",")
// Save `text` to your database, as "cricket1.png,cricket2.png".

To deserialize from the database, e.g. in a text variable:

let files = text.split(separator: ",")
// Use `files` as above.
Sign up to request clarification or add additional context in comments.

5 Comments

When I did that I got: cricket1.png,cricket1.png,cricket1png,.... It is missing the array brackets [ ] and the quotes.. How do I deserialize to make it ["cricket1.png" "cricket1.png", "cricket1.png"]?
@JoshO'Connor The code above does just that. join gives you "cricket1.png,cricket1.png" and if you pass that exact same string to split (as text, above), you get ["cricket1.png", "cricket1.png"].
It outputted [[cricket2.png, cricket1.png, cricket1.png, cricket1.png, cricket1.png, cricket1.png, cricket1.png]]
When I passed it to split
For future usage: in Swift 4 the Syntax changed to let exampleArray = ["1","2","3"] let text = exampleArray.joined(separator: ",") // "1,2,3" let stringArrayFromText = text.split(separator: ",") // ["1","2","3"] let intArrayFromStringArray = stringArrayFromText.map{Int($0)!} // [1,2,3]

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.