Code Monkey home page Code Monkey logo

Comments (7)

quinncnl avatar quinncnl commented on August 16, 2024 1

I kind of agree with @ivensdenner. My preferred usage is:

if let v = element["MyString"] {
    print(v.stringValue)
}

from aexml.

tadija avatar tadija commented on August 16, 2024 1

Hi all, thanks for this discussion.

In the latest version (3.0.0) I've improved error handling logic to be as you recommended here, that is:

  • there is no errorElementName anymore
  • subscript returns empty element (value = nil and stringValue = "")
  • this empty element will have error property different then nil

Feel free to comment this change if you find anything suspicious (unit tests are passing but this can make breaking changes in other people code, that's why it's a major version bump).

This issue can be closed now.

from aexml.

tadija avatar tadija commented on August 16, 2024

Hmmm, I think I agree. :)
This will be left open for now, maybe I'll change that in some upcoming version.

from aexml.

ivensdenner avatar ivensdenner commented on August 16, 2024

What about the subscript returning an optional type? It sounds more natural to me.

from aexml.

tadija avatar tadija commented on August 16, 2024

No, that's what I wanted to avoid from the initial version. It's easier for end users when subscript is not an optional. And problem with empty string is that you couldn't differentiate when something really has no value or the complete element is missing.

from aexml.

Stitch7 avatar Stitch7 commented on August 16, 2024

Helped myself with:

extension AEXMLElement {
    var string: String? {
        if name == AEXMLElement.errorElementName { return nil }
        return stringValue
    }
}

from aexml.

cbh2000 avatar cbh2000 commented on August 16, 2024

In SwiftyJSON, there are two getters: string: String? and stringValue: String. The equivalent of SwiftyJSON's string: String? is AEXMLElement's value: String?, except that in SwiftyJSON if there's an error, string: String? will be nil instead of an error message. AEXMLElement can be made to mimic SwiftyJSON if subclassed like below:

class MyAEXMLElement: AEXMLElement {
    override var value: String? {
        get {
            guard name != AEXMLElement.errorElement else {
                return nil
            }
            return super.value
        }
        set {
            super.value = newValue
        }
    }

    override var stringValue: String {
        guard name != AEXMLElement.errorElement else {
            return ""
        }
        return super.stringValue
    }
}

And SwiftyJSON handles the errors by having an explicit error property, in case you want the actual error:

extension MyAEXMLElement {
    var error: String? {
        guard let value = value where name == AEXMLElement.errorElement else {
            return nil
        }
        return value
    }
}

Of course, I'm not saying the above is exactly how I think it should be implemented. I'm only showing how I would adapt the existing code to behave like SwiftyJSON.

from aexml.

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.