Skip to content

Instantly share code, notes, and snippets.

@Pasanpr
Created March 8, 2017 20:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pasanpr/9cb737a04d0a96dd6b6123b44a1656e4 to your computer and use it in GitHub Desktop.
Save Pasanpr/9cb737a04d0a96dd6b6123b44a1656e4 to your computer and use it in GitHub Desktop.
enum PlistError: Error {
case invalidResource
case parsingFailure
}
class PlistLoader {
static func array(fromFile name: String, ofType type: String) throws -> [[String: String]] {
guard let path = Bundle.main.path(forResource: name, ofType: type) else {
throw PlistError.invalidResource
}
guard let array = NSArray(contentsOfFile: path) as? [[String: String]] else {
throw PlistError.parsingFailure
}
return array
}
}
class ContactsSource {
static var contacts: [Contact] {
let data = try! PlistLoader.array(fromFile: "ContactsDB", ofType: "plist")
return data.flatMap { Contact(dictionary: $0) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment