Skip to content
This repository was archived by the owner on May 20, 2021. It is now read-only.

Implement the possibility to override the value of any properties tha…#44

Closed
Aymenworks wants to merge 1 commit intoJohnSundell:swift3from
Aymenworks:swift3
Closed

Implement the possibility to override the value of any properties tha…#44
Aymenworks wants to merge 1 commit intoJohnSundell:swift3from
Aymenworks:swift3

Conversation

@Aymenworks
Copy link

Since I need this feature, I decided to continue this idea : https://github.com/JohnSundell/Wrap/issues/23.

So this PR Implement the possibility to override the value of any properties that contains nil values

Here is an example of use and output :

struct SimpleTrip {
    let name: String
    let numberOfDays: Int?
    let numberOfSpots: Int?
    let updatedDate: Date?
    let endDate: Date?
}

extension SimpleTrip: WrapCustomizable {
    func keyForWrapping(propertyName: String) -> String? {
        if propertyName == "updatedDate" {
            return "newUpdatedDate"
        } else if propertyName == "endDate" {
            return "newEndDate"
        }
        
        return propertyName
    }

    func fallbackValueForProperty(named propertyName: String) -> Any? {
        if propertyName == "updatedDate" {
            return Optional<Any?>(nil)
        } else if propertyName == "numberOfSpots" {
            return 0
        } else if propertyName == "endDate" {
            return Date()
        }
        
        return nil
    }
}

let value = SimpleTrip(name: "Name1", numberOfDays: nil, numberOfSpots: nil, updatedDate: nil, endDate: nil)

let dictionary: WrappedDictionary = try Wrap(value)

Output of dictionary:

["name": "Name1", "newUpdatedDate": nil, "numberOfSpots": 0, "newEndDate": 2017-09-08 14:53:50 +0000]

@Aymenworks
Copy link
Author

Aymenworks commented Sep 8, 2017

Ooops !

It seems it's a duplicate of #36.

But it also seems that there is no news from this PR.

What do we do @JohnSundell ?

@Aymenworks
Copy link
Author

It seems that my changes worked on a playground but not in the real project, strange..

I'll use the #36 for now.

@Aymenworks Aymenworks closed this Sep 8, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant