Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Eugene Butusov on 06/04/16.
- // Copyright (c) 2016 VedideV. All rights reserved.
- //
- import Foundation
- class RestedObject : NSObject {
- class func conversions() -> [(from: String, to: String, convertor: (AnyObject) -> AnyObject)] {
- return [
- (from: "id", to: "id", convertor: { (source) -> AnyObject in return String(source as! NSNumber) })
- ]
- }
- class func serializers() -> [(from: String, to: String, convertor: (AnyObject) -> AnyObject)] {
- return [
- (from: "id", to: "id", convertor: { (source) -> AnyObject in
- let numberFormatter: NSNumberFormatter = NSNumberFormatter()
- return numberFormatter.numberFromString(source as! String)!
- })
- ]
- }
- var id: String = ""
- override init() {
- super.init()
- }
- required init(_ json: NSDictionary) {
- super.init()
- for conversion in self.dynamicType.conversions() {
- if let sourceVal = json[conversion.from] as AnyObject! {
- if !(sourceVal is NSNull) {
- self.setValue(conversion.convertor(sourceVal), forKey: conversion.to)
- }
- }
- }
- }
- internal var json: NSDictionary {
- let result: NSMutableDictionary = NSMutableDictionary()
- for conversion in self.dynamicType.serializers() {
- if let sourceVal = self.valueForKey(conversion.from) {
- result.setValue(conversion.convertor(sourceVal), forKey: conversion.to)
- }
- }
- return result
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement