Created
December 15, 2016 21:23
-
-
Save ranmyfriend/08817bcb1d4c90bef652cf49d20fedfe to your computer and use it in GitHub Desktop.
Initialize Struct class using Generics and work for b->child1,child2. So if you access b you can get b.meta and b.response<home,login,signup>
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
/*-------------Initialize Struct class using Generics and bounding for b->child1,child2-----*/ | |
struct b<T> { | |
var resp: T? | |
var meta: metaResp? | |
init?(input:Dictionary<String,Any>,resp: T) { | |
self.resp = resp | |
let bin:[String:Any] = input["base"] as! [String : Any] | |
meta = metaResp.init(input: bin["meta"] as! Dictionary<String, Any>) | |
} | |
} | |
struct metaResp{ | |
let status:Bool? | |
let message:String? | |
let code:Int? | |
init(input:Dictionary<String,Any>) { | |
self.status = input["status"] as! Bool? | |
self.code = input["code"] as! Int? | |
self.message = input["message"] as! String? | |
} | |
} | |
struct loginResp{ | |
var email:String? | |
var name:String? | |
init(input:Dictionary<String,Any>) { | |
self.email = input["email"] as! String? | |
self.name = input["name"] as! String? | |
} | |
} | |
struct homeResp{ | |
var flips:[Int]? | |
var ownerId:Int? | |
init(input:Dictionary<String,Any>) { | |
self.flips = input["flips"] as! [Int]? | |
self.ownerId = input["ownerId"] as! Int? | |
} | |
} | |
let loginResponse:[String:Any] = ["base":["meta":["status":true,"code":200,"message":"login success"]],"response":["email":"a@a.com","name":"apple"]] | |
let homeResponse:[String:Any] = ["base":["meta":["status":true,"code":200,"message":"home success"]],"response":["flips":[1,2,3],"ownerId":100]] | |
if let l = b<loginResp>( | |
input:loginResponse, | |
resp:loginResp.init(input: loginResponse["response"] as! Dictionary<String, Any>)) { | |
print(l.resp?.name as Any) | |
print(l.meta?.message as Any) | |
} | |
if let r = b<homeResp>(input:homeResponse,resp:homeResp.init(input: homeResponse["response"] as! Dictionary<String, Any>)) { | |
print(r.meta?.message as Any) | |
print(r.resp?.flips as Any) | |
print(r.resp?.ownerId as Any) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment