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

[SR-1759] Subclassing UIColor leads to error : "Declarations from extensions cannot be overridden yet" #44368

Open
swift-ci opened this issue Jun 14, 2016 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself SDKOverlay

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1759
Radar rdar://26800142
Original Reporter Hal (JIRA User)
Type Bug

Attachment: Download

Environment

macOS 10.11.5 (15F34)
Xcode 8.0 beta (8S128d)
Apple Swift version 3.0 (swiftlang-800.0.30 clang-800.0.24)

Additional Detail from JIRA
Votes 4
Component/s Compiler
Labels Bug, SDKOverlay
Assignee None
Priority Medium

md5: 4a18f1c80354d3e40eb0894dc7e3be33

Issue Description:

When you subclassing UIColor, you need to add a @nonobjc required convenience public init(colorLiteralRed red: Float, green: Float, blue: Float, alpha: Float) method.
When adding it, the error "Declarations from extensions cannot be overridden yet" appears.

@swift-ci
Copy link
Collaborator Author

Comment by Seyed Samad Gholamzadeh (JIRA)

I have this problem too.
I think it's a bug!🙁

@swift-ci
Copy link
Collaborator Author

Comment by swesthk (JIRA)

Same problem here!!!

@swift-ci
Copy link
Collaborator Author

Comment by Seyed Samad Gholamzadeh (JIRA)

I think I found the cause of this problem. It seems since Xcode 8 apple don't allow to developers create subclass for UIColor.
this is what apple has written in UIColor api reference:

Subclassing Notes
Most developers have no need to subclass UIColor. The only time doing so might be necessary is if you require support for additional colorspaces or color models. If you do subclass, the properties and methods you add must also be safe to use from multiple threads.

By the way I have written a small extension for UIColor that maybe solves your problem:
Xcode Pencils Colors
With Regard.

@swift-ci
Copy link
Collaborator Author

Comment by Marc Knaup (JIRA)

The subclassing notes for UIColor aren't new. Apple clearly allows subclassing.

The Objective-C -> Swift import seems to have a bug which imports an extension initializer as required which isn't even allowed in Swift!

extension UIColor {

    @nonobjc required public convenience init(colorLiteralRed red: Float, green: Float, blue: Float, alpha: Float)
}

The only workaround so far seems to be to add only convenience initializers in the subclass or to not add any initializers at all. That way the problematic initializer is inherited automatically.

(Xcode 8.2)

@swift-ci
Copy link
Collaborator Author

Comment by Martin Straub (JIRA)

This is just annoying...

Tried to get a nice solution using UIColor and Codable. But it's neither possible to add the Codable requirements in an extension, nor using subclasses because of this bug :-/

@swift-ci
Copy link
Collaborator Author

Comment by Sergey A. Novitsky (JIRA)

I have observed the same for NSString (Xcode 9 and Swift 4).

Xcode offers a fix for the missing required initializer of an extension:

required convenience init(stringLiteral value: StaticString) {

    fatalError("init(stringLiteral🙂 has not been implemented")

}

But it obviously does not compile.

@swift-ci
Copy link
Collaborator Author

Comment by Vitalii Budnik (JIRA)

Xcode 10.2 (10E125)
Swift: Apple Swift version 5.0 (swiftlang-1001.0.69.5 clang-1001.0.46.3)
macOS: 10.14.4 (18E226)

The problem is still here. The code below doesn't compile.

import Foundation
#if os(iOS)
import UIKit
typealias NSColor = UIColor
#elseif os(macOS)
import AppKit
#endif

internal final class AnotherColorClass: NSColor {
  
  internal init(param: Any) {}
  
  @nonobjc required convenience init(
    _colorLiteralRed red: Float,
    green: Float,
    blue: Float,
    alpha: Float
    )
  {
    fatalError("init(_colorLiteralRed:green:blue:alpha:) has not been implemented")
  }
  
  internal required init?(coder: NSCoder) {
    fatalError("init?(coder:) has not been implemented")
  }
  
  #if os(macOS)
  internal required init?(
    pasteboardPropertyList propertyList: Any,
    ofType type: NSPasteboard.PasteboardType
    )
  {
    fatalError("init?(pasteboardPropertyList:ofType:) has not been implemented")
  }
  #endif
}

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself SDKOverlay
Projects
None yet
Development

No branches or pull requests

1 participant