Skip to content

Instantly share code, notes, and snippets.

@loucimj
Last active July 26, 2017 12:09
Show Gist options
  • Save loucimj/c49d072f2f0056797592cbbbf8b8cc84 to your computer and use it in GitHub Desktop.
Save loucimj/c49d072f2f0056797592cbbbf8b8cc84 to your computer and use it in GitHub Desktop.
import Unbox
import Foundation
extension Unboxable {
func safeUnbox(unboxer: Unboxer,key :String )->String{
do{
return try unboxer.unbox(key: key) as String
}catch{
return ""
}
}
func safeUnbox(unboxer: Unboxer,key :String )->Int{
do{
return try unboxer.unbox(key: key) as Int
}catch{
return 0
}
}
func safeUnbox(unboxer: Unboxer,key :String )->Bool{
do{
return try unboxer.unbox(key: key) as Bool
}catch{
return false
}
}
func safeUnbox(unboxer: Unboxer,key :String )->Float{
do{
return try unboxer.unbox(key: key) as Float
}catch{
return 0.0
}
}
func safeUnbox(unboxer: Unboxer,key :String )->PostType{
do{
return try unboxer.unbox(key: key) as PostType
}catch{
return PostType.photo
}
}
func safeUnbox(unboxer: Unboxer,key :String )->InteractionType{
do{
return try unboxer.unbox(key: key) as InteractionType
}catch{
return InteractionType.like
}
}
func safeUnbox(unboxer: Unboxer,key :String )->UserType{
do{
return try unboxer.unbox(key: key) as UserType
}catch{
return UserType.other
}
}
func safeUnbox(unboxer: Unboxer, key :String) -> HashagType {
do {
return try unboxer.unbox(key: key) as HashagType
} catch {
return HashagType.hashtag
}
}
func safeUnbox(unboxer: Unboxer, key :String) -> LinkPostType {
do {
return try unboxer.unbox(key: key) as LinkPostType
} catch {
return LinkPostType.same
}
}
func safeUnbox(unboxer: Unboxer, key :String) -> Double {
do {
return try unboxer.unbox(key: key) as Double
} catch {
return 0.0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment