Last active
December 13, 2016 17:47
-
-
Save robertmryan/73c19aaed3792723ba3e8c28960d94e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func open(_ filePath: String) throws -> NSArray { | |
let data = try Data(contentsOf: URL(fileURLWithPath: filePath)) | |
let object: Any? | |
if #available(OSX 10.11, *) { | |
object = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data as NSData) | |
} else { | |
let unarchiver = NSKeyedUnarchiver(forReadingWith: data) | |
object = unarchiver.decodeObject(forKey: "root") | |
} | |
guard let unarchivedObject = object else { | |
throw ArchiveError.unableToUnarchive | |
} | |
guard let array = unarchivedObject as? NSArray else { | |
throw ArchiveError.notArray | |
} | |
return array | |
} | |
enum ArchiveError: Error { | |
case unableToUnarchive | |
case notArray | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In further refinement, I'd (a) not have
try
in the name of the method; and (b) retire use ofNSException
(we're throwing errors, not exceptions).