Skip to content

Instantly share code, notes, and snippets.

@ryancumley
Created December 28, 2014 19:02
Show Gist options
  • Save ryancumley/25b5a26ca8c6e3d09ac1 to your computer and use it in GitHub Desktop.
Save ryancumley/25b5a26ca8c6e3d09ac1 to your computer and use it in GitHub Desktop.
Mapping JSON using Swift syntactic sugar in a 'Machine Translation' style from ObjC
//MARK: FlickrFeedItem and support
class FlickrFeedItem: NSObject {
var title: String?
var link: String?
var media: [String : String]?
var dateTaken: NSDate?
var photoDescription: String?
var published: NSDate?
var author: String?
var authorID: String?
var tags: [String]?
var extractedHeightValue: Int?
var extractedWidthValue: Int?
class func feedItemByMappingInput(inputDictionary input: [String : AnyObject]) -> FlickrFeedItem {
var retVal = FlickrFeedItem()
retVal.title = input["title"]? as? String
retVal.link = input["link"]? as? String
retVal.media = input["media"]? as? [String : String]
if let candidate = input["date_taken"]? as? String { retVal.dateTaken = foundationDateFromFlickrFormatDateString(dateString: candidate) }
retVal.photoDescription = input["description"]? as? String
if let candidate = input["published"]? as? String { retVal.published = foundationDateFromFlickrFormatDateString(dateString: candidate) }
retVal.author = input["author"]? as? String
retVal.authorID = input["author_id"]? as? String
if let candidate = input["tags"]? as? String { retVal.tags = structuredTagsFrom(tagString: candidate) }
if let sourceDescription = retVal.photoDescription {
if let heightWidthTuple = extractHeightAndWidthFromFlickrDescription(descriptionString: sourceDescription) {
retVal.extractedHeightValue = heightWidthTuple.height
retVal.extractedWidthValue = heightWidthTuple.width
}
}
return retVal
}
}
//MARK: helper conversion functions
func mediaMDictionaryFromNestedInput(inputDictionary input: [String : String]) -> [String : String] {
//not implemented yet
return ["Fake" : "Not Real"]
}
func foundationDateFromFlickrFormatDateString(dateString date: String) -> NSDate {
//not implemented yet
return NSDate.distantFuture() as NSDate
}
func structuredTagsFrom(tagString tags: String) -> [String] {
//not implemented yet
return ["fake"]
}
func extractHeightAndWidthFromFlickrDescription(descriptionString input: String) -> (height: Int, width: Int)? {
//not implemented yet
return (10, 20)
}
//MARK:-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment