Giter Site home page Giter Site logo

Comments (5)

cadizjavier avatar cadizjavier commented on July 26, 2024 4

Just for further reference regarding this same issue:

The recommended way mentioned in the last comment fails (tested with v2.2.6) with the error Cannot invoke 'value' with no arguments.
value() is generic function and returns a T type inferred from the the variable declaration.

So in order to fix this we should add a type like this (or the type that id belongs to):

required init?(_ map: Map) {
    if let id: Int = map["id"].value() {
      self.id = id
    } else { return nil }
}

from objectmapper.

robertoschwald avatar robertoschwald commented on July 26, 2024

Need that, too.

from objectmapper.

tristanhimmelman avatar tristanhimmelman commented on July 26, 2024

Interesting idea. I will put some thought into how I could add this functionality to the library.

Cheers,
Tristan

from objectmapper.

ldiqual avatar ldiqual commented on July 26, 2024

@tristanhimmelman Is there an open issue where you're tracking ideas & future developments for official support of non-optional mapping? I understand that it's not possible (or not very elegant) at the moment, but it'd be awesome to gather the current recommended way to do it and brainstorm on other solutions in a single place.

from objectmapper.

SimplGy avatar SimplGy commented on July 26, 2024

I'm wondering the same thing, @ldiqual. Is there a discussion about pros, cons, and challenges of supporting failable initialization with non-optional model properties. Did you find anything? I'm imagining it must be difficult to do, and there may be some prior discussion of the challenges involved with implementing this.

I tried a few light things, but can't figure a way to do a failable init with non-optional mapping in a symmetrical, DRY way.

This video expresses my critique succinctly--I don't want the transform layer to enforce optional or mutable state in my models:

screen shot 2016-05-16 at 3 28 35 pm

This is a good option: https://github.com/Hearst-DD/ObjectMapper/pull/216/files

And this was recommended:

required init?(_ map: Map) {
    if let id = map["id"].value() {
      self.id = id
    } else { return nil }
}

...but this won't serialize back out the same way unless I add that to the mapping function, and enforced symmetricality (where foo == Foo(json: foo.toJSON())) is one of the things I love best about this library (along with extendable serializers and the mapping syntax).

from objectmapper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.