Post

Replies

Boosts

Views

Activity

Using an excel document as a database/dictionary in Swift
Very new to all this but I'm trying to make an excel document work as almost a dictionary where in the first column becomes the key for the answer in the next column. I found something on a different forum that I couldn't understand but it also seems more complex than what I'm trying to do. I guess I have 3 questions. Does a database have to be on the cloud or does it just refer to a literally If i have 200 terms i want to link to 200 phrases do all of those have to be actually in the code or can the code just be told to read an excel or excel type document and setup a dictionary accordingly. Does this need to be a dictionary or a database and what is the difference? (The only other small point is whether dictionaries can have one key linked to many answers) The code I previously found is linked here var theDatabase = YourDatabase() // whatever is your database model // read the file as one big string var fileContents:String do { fileContents = try String(contentsOfFile: destinationPath, encoding: String.Encoding.utf8) } catch let error { print("Error reading file: (error.localizedDescription)") fileContents = "" } guard fileContents.count>0 else { return theDatabase } // split out records (separated by returns) let records = fileContents.split { $0 == "\r" } // first record is field names, i.e., column titles let fieldNames = findFields(String(records[0])) // all remaining records are data, so match each with field names of record 0 for k in 1..<records.count { let values = findFields(String(records[k])) var dictionary = [String:String ])() // the first close bracket immediately above was needed to post on forum not in actual code for n in 0..<values.count { dictionary[fieldNames[n]] = values[n] } theDatabase.append(dictionary) // append would be a method in YourDatabase } credit - DelawareMathGuy https://developer.apple.com/forums/thread/124621 Any help would be much appreciated
5
0
3k
Mar ’22