I am trying to write an extension to a UIKit class, but am running into a can’t-win situation:
The code I ‘want’ to write looks like:
public extension UISplitViewController {
override public func viewDidLoad() {
super.viewDidLoad()
if UIDevice.current.userInterfaceIdiom == .pad {
preferredDisplayMode = .automatic
} else {
preferredDisplayMode = .primaryOverlay
}
}
}
This generates the error message
/Users/rlaurb/Projects/Cooks-Memory/Cooks-Memory/AppDelegate.swift:131:23: Overriding instance method must be as accessible as the declaration it overrides
/Users/rlaurb/Projects/Cooks-Memory/Cooks-Memory/AppDelegate.swift:131:23: Overridden declaration is here (UIKit.UIViewController)
But I can’t change the access control of the function to ‘open’, because I get the warning that the function can’t be “more” accessible than the extension.
And I can’t change the extension’s access to ‘open’ because apparently extensions can’t be open.
Now I don’t want to get into a debate about whether this code works — it’s just an experiment — but is it even possible to express this idea?? I.e., is it possible to express this idea without subclassing?
I don’t get your problem here. If you don’t want to debate the correctness of your code, why are you asking for help or even showing error messages for a code snippet that cannot work?
1. Drop the access modifier from the extension itself, because this is only for convenience, which may or may not rule over the members of the extension members. If you’re already explicitly setting the access modifier on the extension members then the convenience access modifier makes no sense.
2. The code cannot work, because you cannot override `viewDidLoad` on a class that you don’t own, on a subclass of `UISplitViewController` that would be possible.
class MySplitViewController : UISplitViewController {}
extension MySplitViewController {
override open func viewDidLoad() {
super.viewDidLoad()
/* ... */
}
}
···
Am 20. September 2017 um 21:41:31, Rick Aurbach via swift-users (swift-users@swift.org) schrieb:
I am trying to write an extension to a UIKit class, but am running into a can’t-win situation:
The code I ‘want’ to write looks like:
public extension UISplitViewController {
override public func viewDidLoad() {
super.viewDidLoad()
if UIDevice.current.userInterfaceIdiom == .pad {
preferredDisplayMode = .automatic
} else {
preferredDisplayMode = .primaryOverlay
}
}
}
This generates the error message
/Users/rlaurb/Projects/Cooks-Memory/Cooks-Memory/AppDelegate.swift:131:23: Overriding instance method must be as accessible as the declaration it overrides
/Users/rlaurb/Projects/Cooks-Memory/Cooks-Memory/AppDelegate.swift:131:23: Overridden declaration is here (UIKit.UIViewController)
But I can’t change the access control of the function to ‘open’, because I get the warning that the function can’t be “more” accessible than the extension.
And I can’t change the extension’s access to ‘open’ because apparently extensions can’t be open.
Now I don’t want to get into a debate about whether this code works — it’s just an experiment — but is it even possible to express this idea?? I.e., is it possible to express this idea without subclassing?
(What I meant by “debating the correctness of my code” was that I wanted to talk about the approach and not about the content of the viewDidLoad() method.)
Cheers,
Rick Aurbach
···
On Sep 20, 2017, at 3:13 PM, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:
I don’t get your problem here. If you don’t want to debate the correctness of your code, why are you asking for help or even showing error messages for a code snippet that cannot work?
1. Drop the access modifier from the extension itself, because this is only for convenience, which may or may not rule over the members of the extension members. If you’re already explicitly setting the access modifier on the extension members then the convenience access modifier makes no sense.
2. The code cannot work, because you cannot override `viewDidLoad` on a class that you don’t own, on a subclass of `UISplitViewController` that would be possible.
class MySplitViewController : UISplitViewController {}
extension MySplitViewController {
override open func viewDidLoad() {
super.viewDidLoad()
/* ... */
}
}
Am 20. September 2017 um 21:41:31, Rick Aurbach via swift-users (swift-users@swift.org <mailto:swift-users@swift.org>) schrieb:
I am trying to write an extension to a UIKit class, but am running into a can’t-win situation:
The code I ‘want’ to write looks like:
public extension UISplitViewController {
override public func viewDidLoad() {
super.viewDidLoad()
if UIDevice.current.userInterfaceIdiom == .pad {
preferredDisplayMode = .automatic
} else {
preferredDisplayMode = .primaryOverlay
}
}
}
This generates the error message
/Users/rlaurb/Projects/Cooks-Memory/Cooks-Memory/AppDelegate.swift:131:23: Overriding instance method must be as accessible as the declaration it overrides
/Users/rlaurb/Projects/Cooks-Memory/Cooks-Memory/AppDelegate.swift:131:23: Overridden declaration is here (UIKit.UIViewController)
But I can’t change the access control of the function to ‘open’, because I get the warning that the function can’t be “more” accessible than the extension.
And I can’t change the extension’s access to ‘open’ because apparently extensions can’t be open.
Now I don’t want to get into a debate about whether this code works — it’s just an experiment — but is it even possible to express this idea?? I.e., is it possible to express this idea without subclassing?
I don’t get your problem here. If you don’t want to debate the correctness
of your code, why are you asking for help or even showing error messages
for a code snippet that cannot work?
1. Drop the access modifier from the extension itself, because this is
only for convenience, which may or may not rule over the members of the
extension members. If you’re already explicitly setting the access modifier
on the extension members then the convenience access modifier makes no
sense.
2. The code cannot work, because you cannot override `viewDidLoad` on a
class that you don’t own, on a subclass of `UISplitViewController` that
would be possible.
I think the point is that this is possible in ObjcC. So the question is
really whether you can override methods in Swift from classes that are not
your own.
The answer, as far as I’ve seen, is no. Whenever I’ve needed this feature
I’ve used ObjC.
- Geordie
···
Adrian Zubarev via swift-users <swift-users@swift.org> schrieb am Mi. 20. Sep. 2017 um 22:13:
class MySplitViewController : UISplitViewController {}
extension MySplitViewController {
override open func viewDidLoad() {
super.viewDidLoad()
/* ... */
}
}
Am 20. September 2017 um 21:41:31, Rick Aurbach via swift-users (
swift-users@swift.org) schrieb:
I am trying to write an extension to a UIKit class, but am running into a
can’t-win situation:
The code I ‘want’ to write looks like:
public extension UISplitViewController {
override public func viewDidLoad() {
super.viewDidLoad()
if UIDevice.current.userInterfaceIdiom == .pad {
preferredDisplayMode = .automatic
} else {
preferredDisplayMode = .primaryOverlay
}
}
}
This generates the error message
/Users/rlaurb/Projects/Cooks-Memory/Cooks-Memory/AppDelegate.swift:131:23:
Overriding instance method must be as accessible as the declaration it
overrides
/Users/rlaurb/Projects/Cooks-Memory/Cooks-Memory/AppDelegate.swift:131:23:
Overridden declaration is here (UIKit.UIViewController)
But I can’t change the access control of the function to ‘open’, because I
get the warning that the function can’t be “more” accessible than the
extension.
And I can’t change the extension’s access to ‘open’ because apparently
extensions can’t be open.
Now I don’t want to get into a debate about whether this code works — it’s
just an experiment — but is it even possible to express this idea?? I.e.,
is it possible to express this idea without subclassing?