Advertisement
Guest User

Untitled

a guest
Nov 11th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.86 KB | None | 0 0
  1. //
  2. //  MyJson.swift
  3. //  Quotter
  4. //
  5. //  Created by Alex Belov on 11/11/2018.
  6. //  Copyright © 2018 Alex Belov. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11.  
  12. struct quotesDB: Codable {
  13.     let listofQuotes: [String]
  14.     let theme: String
  15. }
  16.  
  17.  
  18. class JsonHelper {
  19.     private let file = "quotes.json"
  20.     private let filePath = Bundle.main.path(forResource: "quotes", ofType: "json")
  21.     private var jsonString = ""
  22.    
  23.    
  24.     func getDataBase() -> [quotesDB]{
  25.         do{
  26.             jsonString = try String(contentsOf: URL(fileURLWithPath: filePath!))
  27.            
  28.         } catch{
  29.             print("Error reading json")
  30.         }
  31.        
  32.         let data = jsonString.data(using: .utf8)!
  33.         let decoder = JSONDecoder()
  34.         return try! decoder.decode([quotesDB].self, from: data) // There's array of quotesDb structs
  35.     }
  36.    
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement