Swifterswift: shadowOffset for UILabel is conflict with UILabel's property.

Created on 7 Jan 2019  Â·  23Comments  Â·  Source: SwifterSwift/SwifterSwift

[v] I've read, understood, and done my best to follow the Contributing guidelines before opening this issue.

What did you do?

Set shadowOffset, shadowColor, shadowRadius inspectable value on UILabel.

What did you expect to happen?

UILabel with proper shadow

What happened instead?

shadowOffset is not applied, because UILabel has shadowOffset property.
So shadow is always above text, because layer.shadowOffset default value is (0, -3)
(Also UILabel has shadowColor property.)

SwifterSwift Environment

  • SwifterSwift version: 4.6.0
  • Xcode version: 10
  • macOS version running Xcode: 10.14.2
  • Swift version: 4.2
  • Platform(s) running SwifterSwift: iOS

Demo Project

Please link to or upload a project we can download that reproduces the issue.

source good first issue help wanted

All 23 comments

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically closed because it has not had recent activity. Thank you for your contributions.

Not stale, issue still exists.

        navBarTitle = UILabel()
        navBarTitle.backgroundColor = .clear
        if let color = navigationController?.navigationBar.titleTextAttributes?[.foregroundColor] as? UIColor {
            navBarTitle.textColor = color
        } else {
            navBarTitle.textColor = self.titleColor
        }
        navBarTitle.shadowOffset = CGSize(width: 0, height: 1);             <---- error here
        navBarTitle.font = UIFont(name: "Roboto-Medium", size: 17.0)
        navBarTitle.textAlignment = .center
        navigationItem.titleView = navBarTitle;

Ambiguous use of 'shadowOffset'

SwifterSwift Environment

  • SwifterSwift version: 5.2.0
  • Xcode version: 11, 12b4
  • macOS version running Xcode: 11b4
  • Swift version: 5.x
  • Platform(s) running SwifterSwift: iOS

Whats interesting is that the Xcode says it found 2 candidate, however the first candidate takes me nowhere and the second candidate takes me to this line of code. Note: its a UIView extension, not a UILabel extension, and UILabel inherits UIView.

https://github.com/SwifterSwift/SwifterSwift/blob/master/Sources/SwifterSwift/UIKit/UIViewExtensions.swift#L144

Ah sorry, we let the bot close this one... Thanks for reopening this @multinerd
I think the simplest solution to this is to rename our UIView extension to something else, not really sure, but maybe viewShadowOffset? Because I honestly don't know if it is possible to disambiguate that at the call

I think we can add UILabel extension viewShadowColor,not rename UIView, but it is not possible to disambiguate that at the UILabel.
And we need to pay attention when using it on Storyboard.

Of course if we rename our UIView extension, the previously set shadowColor will be retained in User Defined Runtime Attributes, so we must reset the view, delete shadowColor and add viewShadowColor

@Jayxiang sorry, the issue is with shadowColor or shadowOffset?
I this case I think we can't do anything on UILabel because shadowOffset is defined on UIKit as seemed in the ambiguous note 1. Found this candidate (UIKit.UILabel) as ours is Found this candidate (SwifterSwift.UIView) so we have to rename the UIView from SwifterSwift, since I can't think any other way to disambiguate this

@LucianoPAlmeida yes, only shadowColor and shadowOffset have the same names as UILabel. Maybe we don’t need to make any changes, just add a note to shadowColor and shadowOffset:
e.g.
-Note: This property does not work in UILabel, you can use label.layer.shadowColor

@Jayxiang I think that still doesn't solve the problem because people who install SwifterSwift remain unable to use a native UILabel property, which is not ideal. IMHO I think those kinds of extensions could be deprecated then removed since the abstract simply one level access (from view.layer.property to view.property) but seems like the more used for people is the IBOutlet and the ability to change this on storyboard ... so in reality, we can't really remove then. The thing is rename seemed like the ideal although it could cause issues with people using it in storyboard.

Hey @Jayxiang, thanks for doing those tests :)

So trying to use this testLabel.shadowOffset doesn't give an ambiguous error? That is the problem I was mentioning (not the fact of the property having the expected visual effect). Users aren't able to access this native UILabel property without ambiguous error.

@LucianoPAlmeida I use this testLabel.shadowOffset doesn't give any errors.

SwifterSwift Environment

  • SwifterSwift version: 5.2.0
  • Xcode version: 11.6 (11E708)
  • macOS version running Xcode: 11.6 (11E708)
  • Swift version: 5.x
  • Platform(s) running SwifterSwift: iOS

@multinerd Is it the reason for Xcode? Are you sure there is an error?

I am definitely getting an ambiguous error. I created a sample project that reproduces the error, hopefully that will help.

I doubt it makes a difference but I am on MacOS 11 but the error persists in xcode 11.4 and 12

https://github.com/multinerd/SwifterSwift-Bug

@Jayxiang Did you explicit import SwifterSwift? Because to me, the ambiguous error is the expected behavior as there's currently no way the compiler can select an overload from that member.
I'm getting the same error on Xcode 12 Beta 1

I'm doing the same as @Jayxiang and getting no compiler or visual errors. I'm also on the latest Xcode, so perhaps it was an issue fixed in later versions of the Swift compiler?

I tried both with copy-pasting the UIView extension code directly into my project and including SwifterSwift as a pod dependency. Same result each time.

@guykogus Does this compile for you?

https://github.com/multinerd/SwifterSwift-Bug

@guykogus Does this compile for you?

https://github.com/multinerd/SwifterSwift-Bug

Yes, but I had to convert it for Xcode 11.6. I'd need to download Xcode 12 beta to test it properly. Maybe that's the issue?

@guykogus I could have sworn I had the issue with xcode 11 also, but after double checking it appears to only throw an error on xcode 12. Apologies for the incorrect information. Seems to only affect xcode 12.

@multinerd That being said I think the compiler was wrong before in accepting this, and now although is a change that breaks source compatibility, it is indeed a fact that this is ambiguous, at least in my opinion... because the compiler should not assume one or the other declaration in this case. I mean why would it select one of the overloads? there are no general rules for that. So to me, the ambiguous error is correct :)

Since the properties are declared in different modules a possible way to disambiguate that is use declaration imports

import var SwifterSwift.shadowOffset // If you want the SwifterSwift one 
import var UIKit.shadowOffset // If you want the UIKit one 

I think that could work ... but didn't test it

But if you think this worth a discussion and this is a source compat compiler regression, you can start a topic on https://forums.swift.org or open a JIRA ticket on https://bugs.swift.org

Testing... seems like it doesn't work... :(

I have downloaded Xcode 12.0 beta 5 (12A8189h), it does report an error Ambiguous use of 'shadowOffset'.

Test:
if import SwifterSwift

            let viewLabel = testLabel as UIView
            viewLabel.shadowOffset = CGSize(width: 0, height: 4) 

It work for me.
Maybe the best way is to rename UIView extension...0.0

We could also look into instating #645 😬

For the reference https://forums.swift.org/t/import-declarations/39562
I think the best option we have then is to rename our UIView extensions to something like viewShadowOffset it is going to be a breaking change but at least we avoid problems with name collision from other modules.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omaralbeik picture omaralbeik  Â·  3Comments

omaralbeik picture omaralbeik  Â·  5Comments

omaralbeik picture omaralbeik  Â·  3Comments

omaralbeik picture omaralbeik  Â·  3Comments

bjimenezned picture bjimenezned  Â·  3Comments