Skip to content

Commit

Permalink
* Se agrega el método toDictionary. * Se agrega documentación. * Se a…
Browse files Browse the repository at this point in the history
…ctualiza el archivo Readme. * Se actualiza el archivo .podspec.
  • Loading branch information
xrax committed Feb 24, 2016
1 parent 93ca4a8 commit de3f077
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 11 deletions.
Binary file modified .DS_Store
Binary file not shown.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
**/.DS_Store

### Xcode ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata

## Other
*.xccheckout
*.moved-aside
*.xcuserstate

6 changes: 3 additions & 3 deletions ParceSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Pod::Spec.new do |s|

s.name = "ParceSwift"
s.version = "0.0.3"
s.version = "0.0.4"
s.summary = "Parser - Transform from Dictionary to a desired object and the other way."
s.homepage = "https://github.com/sebastian989/ParceSwift"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = "Leonardo Armero Barbosa, Sebastián Gómez Osorio"
s.source = { :git => "https://github.com/sebastian989/ParceSwift.git", :tag => "0.0.3" }
s.source = { :git => "https://github.com/sebastian989/ParceSwift.git", :tag => "0.0.4" }
s.source_files = "ParseSwift", "ParceSwift/*.{swift}"
s.frameworks = 'UIKit'
s.ios.deployment_target = '9.0'
s.platform = :ios, '9.0'
s.requires_arc = false

end
end
Binary file modified ParceSwift/.DS_Store
Binary file not shown.
85 changes: 77 additions & 8 deletions ParceSwift/ParceSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
// Parseable.swift
// TestReflection
//
// Created by Sebastián Gómez on 20/02/16.
// Created by Sebastián Gómez and Leonardo Armero Barbosa on 20/02/16.
// Copyright © 2016 Sebastián Gómez. All rights reserved.
//

import Foundation

/**
NSObject extension to transform a Dictionary to and from object
*/
public extension NSObject {

/**
Fills the current NSObject with a Dictionary data.

- parameter json: Dictionary with data.
*/
public func fromDictionary(json: [String: AnyObject]) {

let propertyAndTypes = self.getPropertiesAndType()

for (label, type) in propertyAndTypes {
Expand All @@ -26,11 +34,13 @@ public extension NSObject {
}

if let intValue = propertyValue as? NSNumber {
self.setValue(intValue, forKey: label)
continue
if type == "String" {
self.setValue(String(intValue), forKey: label)
} else {
self.setValue(intValue, forKey: label)
}
}


// Array

if let arrayValue = propertyValue as? [String] {
Expand All @@ -55,13 +65,12 @@ public extension NSObject {
continue
}


// Dictionary

if type == "String, String" || type == "String, Int" || type == "String, Bool"
|| type == "Int, String" {
self.setValue(propertyValue, forKey: label)
continue
self.setValue(propertyValue, forKey: label)
continue
}

if let modelValue = propertyValue as? [String:AnyObject] {
Expand All @@ -73,6 +82,60 @@ public extension NSObject {
}
}

/**
Transforms the current NSObject to Dictionary.

- returns: Dictionary with data.
*/
public func toDictionary() -> [String: AnyObject] {
var dictionary = Dictionary<String, AnyObject>()

let propertyAndTypes = self.getPropertiesAndType()
for (label, _) in propertyAndTypes {

guard let propertyValue = self.valueForKey(label) else {
continue
}

if propertyValue is String
{
dictionary[label] = propertyValue as! String
}

else if propertyValue is NSNumber
{
dictionary[label] = propertyValue as! NSNumber
}

else if propertyValue is Array<String>
{
dictionary[label] = propertyValue
}

else if propertyValue is Array<AnyObject>
{
var array = Array<[String: AnyObject]>()

for item in (propertyValue as! Array<AnyObject>) {
array.append(item.toDictionary())
}

dictionary[label] = array
}
// AnyObject
else
{
dictionary[label] = propertyValue.toDictionary()
}
}
return dictionary
}

/**
Get a dictionary with label-value of properties of the current NSObject

- returns: Dictionary with label-value of properties.
*/
func getPropertiesAndType() -> [String : String] {
var propertiesAndType = [String: String]()
let aMirror = Mirror(reflecting: self)
Expand All @@ -84,6 +147,12 @@ public extension NSObject {
return propertiesAndType
}

/**
Get type of a class from their class name.

- parameter className: String with class name.
- returns: Class type ready to initializing.
*/
func swiftClassFromString(className: String) -> NSObject.Type {
let appName = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as! String
let path = appName + "." + className
Expand Down
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,62 @@ print(user.name!)
print(user.address!.street!)
```


### Create a dictionary from a model:

If your model and sub models extends of NSObject:

```
class User: NSObject {
var name: String?
var age: NSNumber?
var height: NSNumber!
var isMan: Bool = false
var address: Address?
var anyDictionary: [String:Int]?
var arrayAnyTypes: [Int]?
var modelsArray: [Address]?
}
```

```
class Address: NSObject {
var street: String?
var avenue: Int?
init(street: String, avenue: Int) {
super.init()
self.street = street
self.avenue = avenue
}
}
```

And it's initialized:

```
let homeAddress = Address(street: "Columbus", avenue: 12)
let workAddress = Address(street: "Sabaneta", avenue: 13)
let marketAddress = Address(street: "Cupertino", avenue: 15)
let user = User()
user.name = "Brian"
user.age = 26
user.address = homeAddress
user.height = 1.75
user.isMan = true
user.anyDictionary = ["key": 2]
user.arrayAnyTypes = [1, 2, 3, 4, 5]
user.modelsArray = [workAddress, marketAddress]
```

Create a dictionary from your model it's simply like:

let myDictionary: [String: AnyObject] = user.toDictionary()




## Author

[Sebastian Gomez Osorio](https://github.com/sebastian989),
Expand Down

0 comments on commit de3f077

Please sign in to comment.