Reputation: 94
I am a beginner (student) programmer currently building a course-catalog-like app where I can view courses required for certain specializations within my school.
Currently, I made a seperate data.swift file to store all the data as global and made a bunch of huuuuge arrays. In the main tableview, I just load datas from my data.swift file.
data.swift
let data1:[[[[String]]]] = [some datas]
let data2:[[[[String]]]] = [some datas]
let sectionHeaders [[String]] = [some datas]
On my tableviewcontroller, I load them up by doing
cell = data1[choice1][choice2][choice3]
...etc
However, I know that this is NOT how it is supposed to be done, well at least xcode tells me "Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions" on my arrays =(, I just don't know any other ways =(
I've actually googled this a little bit and found that some people were recommending plist, some ppl saying sqlite, json, xml and such.
For storing and loading BIG local data, what is the best / most efficient and fastest method?
Thanks!!
Upvotes: 1
Views: 2382
Reputation: 437552
I know that your structure is more complicated, but I wanted to tackle the multiple arrays question. Consider something like:
let teachers = ["Larry Fine", "Curly Howard", "Moe Howard"]
let classNames = ["Calculus 101", "Religious Studies 102", "Teaching with Technology in Film and Media Studies 201"]
Rather than having multiple arrays, you might have a single structure, e.g.:
struct Course: Codable {
let teacher: String
let className: String
}
You could then represent that in a single JSON structure in a text file, say courses.json
, like so:
[ { "teacher" : "Larry Fine", "className" : "Calculus 101" }, { "teacher" : "Curly Howard", "className" : "Religious Studies 102" }, { "teacher" : "Moe Howard", "className" : "Teaching with Technology in Film and Media Studies 201" } ]
In Swift 4, you'd do something like:
var courses: [Course]?
And then to load a JSON file from your bundle and populate courses
:
let fileURL = Bundle.main.url(forResource: "courses", withExtension: "json")!
let data = try! Data(contentsOf: fileURL)
courses = try! JSONDecoder().decode([Course].self, from: data)
You then end up with a single array of courses which has advantages over the separate teachers
and courseNames
arrays. For example, if you want to sort the courses by course name, it makes sure that the course name and teacher name are sorted together.
Now, I know your structure is more complicated than this, so your JSON is likely to get more complicated, too, but I just wanted to illustrate the idea of (a) custom objects; and (b) using Swift 4's new Codable
protocol and JSONDecoder
to parse that JSON.
Upvotes: 3
Reputation: 519
You can use the UserDefaults class. It’s simple and easily accessible. Read the documentation about all the different ways you can retrieve values.
Save Data:
let defaults = UserDefaults.standard
defaults.set("Dewan Payroda", forKey: "Name")
Retrieve Data:
let data = defaults.object(forKey: "Name")
print(data)
Upvotes: 1