Skip to content

Instantly share code, notes, and snippets.



Last active Aug 29, 2015
What would you like to do?
This doesn't feel heaps cleaner...
class MediaItem : MTLModel, MTLJSONSerializing {
var mediaFileName : String!
var mediaMD5String : String!
var mediaFileURL : String!
var mediaFileSize : Int!
class func documentsDirectory() -> String{
return NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
class func JSONKeyPathsByPropertyKey() -> [NSObject : AnyObject]! {
return ["mediaFileName" : "file_name", "mediaMD5String" : "md5_string", "mediaFileURL" : "media_file_url", "mediaFileSize" : "media_file_size"]
class func allMediaItems(filePath: String) -> [MediaItem]?{
let fileString = MediaItem.documentsDirectory().stringByAppendingPathComponent("mediaItems.json")
var error: NSError?
let jsonData = NSData(contentsOfFile: fileString)
if let scanData = jsonData{
let result = NSJSONSerialization.JSONObjectWithData(scanData, options: NSJSONReadingOptions.AllowFragments, error: &error) as [AnyObject]?
if let actualError = error{
println("There was a massive error with this error: \(error?.localizedDescription)")
}else if let totalArray = result{
var serializationError: NSError?
let serializeResults = MTLJSONAdapter.modelsOfClass(MediaItem.self, fromJSONArray: totalArray, error: &serializationError) as [MediaItem]?
if let anError = serializationError {
println("There was a serialization error: \(anError.localizedDescription)")
}else if let resultArray = serializeResults {
return resultArray
println("Nothing returned but an empty array")
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.