Giter Site home page Giter Site logo

Comments (12)

schneiderandre avatar schneiderandre commented on August 22, 2024

Yes, instead of using Strings like "fadeIn" we could use an enum like this

enum Animation: String {
    case FadeIn = "FadeIn"
}

And since all properties are @IBInspectable we have to use rawValue.
This gives us the type safety of an Enum in Code and we are still able to set the properties in Interface Builder.

from spring.

jamztang avatar jamztang commented on August 22, 2024

I would like to propose an alternative is to use typealise and constant, to keep the animation in String type value, but clear enough for the public interface know it's not any random string

from spring.

PGLongo avatar PGLongo commented on August 22, 2024

In my opinion if String type is not necessary an enum should be used in this case. Instead of search the animation name/constant I could make Animation. and see all the possibility and in Interface Builder there would be a select box instead of a textfield.

@jamztang explain why you prefer String.

from spring.

jamztang avatar jamztang commented on August 22, 2024

I do prefer enum. Main reason we have to keep it as string type is because IBInpectables does not support drop down for Enums. Unless it already does?

from spring.

PGLongo avatar PGLongo commented on August 22, 2024

My mistake. It works only with UIColor.

The only way is with workaround like :

enum Animation: String {
    case FadeIn = "FadeIn"
}

@IBDesignable
class SpringView: UIView {

    var animation : Animation = .FadeIn{
        didSet {
            self.animationName = animation.rawValue
        }
    }

    @IBInspectable var animationName: String = ""

from spring.

schneiderandre avatar schneiderandre commented on August 22, 2024

@jamztang Using Enums or String Arrays is not possible for IBInspectables.
I would also try to avoid two variables for setting the same property. This leads to confusion.

I would prefer an Enumeration, like I wrote before. In InterfaceBuilder you have to use the String, and in code you can also use the String or the Enumeration:

springView.animation = SpringView.Animation.FadeIn.rawValue

from spring.

PGLongo avatar PGLongo commented on August 22, 2024

@schneiderandre Enum doesn't appear in InterfaceBuilder.

from spring.

schneiderandre avatar schneiderandre commented on August 22, 2024

@PGLongo right, that's what I wrote, but as I mentioned we can use:

@IBInspectable var animation: String

and set it

springView.animation = SpringView.Animation.FadeIn.rawValue

from spring.

jamztang avatar jamztang commented on August 22, 2024

Ok so this is to my surprise that it's not working for @IBInspectables :(

typealias AnimationName = String

class SpringView : UIView {
    @IBInspectable var animation : AnimationName = ""
}

I'm actually looking into something like this:

public struct SpringAnimation {
    static let FadeIn = "fadeIn"
    static let FadeOut = "fadeOut"
    static let SlideLeft = "slideLeft"
}

// So to set it will be slightly nicer
springView.animation = SpringAnimation.FadeIn

I do have one for Objective-C but I guess it's way too much addition to the code base, so I'll just link it here for our reference.

Double property from @PGLongo is a clever workaround but if we change the animation to animationName will cause incompatibility issues for v1.0.0 which is already out there.

I'm okay to go with either @schneiderandre solution or the struct solution.

from spring.

SachiraChin avatar SachiraChin commented on August 22, 2024

Swift 1.2 supports @objc enums.

https://developer.apple.com/swift/blog/?id=22

from spring.

PGLongo avatar PGLongo commented on August 22, 2024

@SachiraChin also objc enums doesn't appear in InterfaceBuilder.

from spring.

jamztang avatar jamztang commented on August 22, 2024

Right. I guess right note the best is to just keep our options open, optimistically I think we might see Apple support enums in the future

from spring.

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.