Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary operator '==' cannot be applied to operands of type 'Dimension' and 'Float' #140

Closed
WilliamHua opened this issue Jul 2, 2015 · 12 comments

Comments

@WilliamHua
Copy link

I read in #36 and #56 that Cartography should be able to support floats, but I keep on receiving the error Binary operator '==' cannot be applied to operands of type 'Dimension' and 'Float' when I try this code:

        layout(button) { fabButton in
            button.right == button.superview!.right - 16
            button.bottom == button.superview!.bottom - 16
            button.height == Constants.height
            button.width == Constants.width
        }

I have also tried it with CGFloat to no luck

@davidbarsky
Copy link

So it seems to me (and I might be wrong! Please correct me) that you don’t have a type error for the first two statements, but your Constants.height and Constants.width are defined as CGFloats. I’ve had something similar happen to me; try dropping the Constants.file, and instead use a raw numerical value. That might solve your type error.

@WilliamHua
Copy link
Author

Yes exactly, sorry if it wasn't clear. It works if I just use numerical values.

The only issue is that I can't drop the Constants.file, I the number isn't something I can hardcode.

@davidbarsky
Copy link

Are you saying that the values of the Constants file are updated dynamically at runtime, or are these organizational issues at hand?

@WilliamHua
Copy link
Author

Yes, the values Constants are updated dynamically at runtime

@davidbarsky
Copy link

Alright — I’m largely ignorant of your codebase, but updating a Constants file at runtime seems to be a bit of an anti-pattern. What might get you to the same solution is to pass in height and width values whenever you initialize a view (using a custom initializer), set them to a class’s properties (like buttonHeight, etc.) and use those values to set your button’s height and width. 😃

@WilliamHua
Copy link
Author

I understand your concern, it's a bit counter-intuitive. It's dependent on the screen size; that's the only reason it's updated at run-time.

With regards to your solution, wouldn't that still cause an issue since you're using a variable instead of a hardcoded number?

@davidbarsky
Copy link

Fair point.

Upon further reflection, have you considered that maybe you might be approaching this problem incorrectly? Auto Layout encourages you to think in relative, constraint-based way, and updating button dimensions at runtime with absolute values goes against that philosophy — that shouldn’t be your job, it’s Auto Layout’s. A constants file make me think that you might be approaching this problem incorrectly.

This apple documentation might prove useful — it explains the philosophy of Autolayout in a really good way. 👍

@WilliamHua
Copy link
Author

Thanks for your input (not sarcasm, I appreciate you taking the time to respond to me)! I will look into your suggestions.

However, there are cases (outside of this) where I just need to set the height and width of something based off of dynamic values. If I don't solve this issue, I don't see how I could do that with cartography

@davidbarsky
Copy link

I understand that — but those dynamic values should be calculated by Auto Layout.

And again! I don’t understand your codebase, nor the decisions you made. I am totally ignorant. It just seems the problem you’re describing is solved by Auto Layout far more directly.

@robb
Copy link
Owner

robb commented Jul 31, 2015

I can't reproduce this on my end, do you have more code that you can share?

@WilliamHua
Copy link
Author

I want to create a button of a certain width and height:

layout(button) { button in
        button.right == button.superview!.right - 16
        button.bottom == button.superview!.bottom - 16
        button.height == 56
        button.width == 56
}

However, I would also like to tie the numbers to variables rather than hard coded so I can change all button sizes at once. Ideally it would look like this

layout(button) { button in
        button.right == button.superview!.right - 16
        button.bottom == button.superview!.bottom - 16
        button.height == Constants.height
        button.width == Constants.width
}

In a different file I have this structure

@objc class Constants: NSObject {
    static let height: Float == 56
    static let width: Float = 56
}

With this setup I receive the error Binary operator '==' cannot be applied to operands of type 'Dimension' and 'Float'.


For some reason I was under the impression that I attempted this with CGFloat and it failed before, however now when I force the Constants to use CGFloat instead of Float, it works perfectly.

@robb
Copy link
Owner

robb commented Jul 31, 2015

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants