Skip to content

Commit

Permalink
Reading words from json file
Browse files Browse the repository at this point in the history
  • Loading branch information
yassram committed May 14, 2019
1 parent 4e5ee52 commit 8716ada
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions WordSearch/Data/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["hello", "my", "name", "is", "yassir", "Swift", "Kotlin", "ObjectiveC", "Variable", "Java", "Mobile"]
11 changes: 9 additions & 2 deletions WordSearch/DataAccess/WordsDataAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import Foundation

class WordsDataAccess {
static func getWords(index _: Int, completion: @escaping ([String]) -> Void) {
if 1 == 1 {
completion(["hello", "my", "name", "is", "yassir", "Swift", "Kotlin", "ObjectiveC", "Variable", "Java", "Mobile"])
if let url = Bundle.main.url(forResource: "data", withExtension: "json") {
do {
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let words = try decoder.decode([String].self, from: data)
completion(words)
} catch {
print("Error reading Json input data")
}
}
}
}

0 comments on commit 8716ada

Please sign in to comment.