Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. //
  2. // Created by Eugene Butusov on 06/04/16.
  3. // Copyright (c) 2016 VedideV. All rights reserved.
  4. //
  5.  
  6. import Foundation
  7.  
  8. class RestedObject : NSObject {
  9.  
  10. class func conversions() -> [(from: String, to: String, convertor: (AnyObject) -> AnyObject)] {
  11. return [
  12. (from: "id", to: "id", convertor: { (source) -> AnyObject in return String(source as! NSNumber) })
  13. ]
  14. }
  15.  
  16. class func serializers() -> [(from: String, to: String, convertor: (AnyObject) -> AnyObject)] {
  17. return [
  18. (from: "id", to: "id", convertor: { (source) -> AnyObject in
  19. let numberFormatter: NSNumberFormatter = NSNumberFormatter()
  20. return numberFormatter.numberFromString(source as! String)!
  21. })
  22. ]
  23. }
  24.  
  25. var id: String = ""
  26.  
  27. override init() {
  28. super.init()
  29. }
  30.  
  31. required init(_ json: NSDictionary) {
  32. super.init()
  33.  
  34. for conversion in self.dynamicType.conversions() {
  35. if let sourceVal = json[conversion.from] as AnyObject! {
  36. if !(sourceVal is NSNull) {
  37. self.setValue(conversion.convertor(sourceVal), forKey: conversion.to)
  38. }
  39. }
  40. }
  41. }
  42.  
  43. internal var json: NSDictionary {
  44. let result: NSMutableDictionary = NSMutableDictionary()
  45. for conversion in self.dynamicType.serializers() {
  46. if let sourceVal = self.valueForKey(conversion.from) {
  47. result.setValue(conversion.convertor(sourceVal), forKey: conversion.to)
  48. }
  49. }
  50. return result
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement