Swift protocol to return a dictionary of selector
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to create a protocol where one of the methods will return a dictionary of selector. But I'm running into an issue...
here is the protocol code:
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> [String:Selector]
}
and here is the compiler error I'm getting:
MazeTableViewController.swift:12:24: Method cannot be marked @objc because its result type cannot be represented in Objective-C
If I remove the @objc
in front of the method, I get a similar error.
swift swift-protocols
add a comment |
I'm trying to create a protocol where one of the methods will return a dictionary of selector. But I'm running into an issue...
here is the protocol code:
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> [String:Selector]
}
and here is the compiler error I'm getting:
MazeTableViewController.swift:12:24: Method cannot be marked @objc because its result type cannot be represented in Objective-C
If I remove the @objc
in front of the method, I get a similar error.
swift swift-protocols
1
Why did you add the @objc in the first place? Also, have you tried declaring your class like this:public protocol MazeProtocol: class { ...
– RX9
Nov 23 '18 at 14:18
add a comment |
I'm trying to create a protocol where one of the methods will return a dictionary of selector. But I'm running into an issue...
here is the protocol code:
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> [String:Selector]
}
and here is the compiler error I'm getting:
MazeTableViewController.swift:12:24: Method cannot be marked @objc because its result type cannot be represented in Objective-C
If I remove the @objc
in front of the method, I get a similar error.
swift swift-protocols
I'm trying to create a protocol where one of the methods will return a dictionary of selector. But I'm running into an issue...
here is the protocol code:
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> [String:Selector]
}
and here is the compiler error I'm getting:
MazeTableViewController.swift:12:24: Method cannot be marked @objc because its result type cannot be represented in Objective-C
If I remove the @objc
in front of the method, I get a similar error.
swift swift-protocols
swift swift-protocols
edited Nov 23 '18 at 16:03
Damon
5361619
5361619
asked Nov 23 '18 at 14:16
otuswebotusweb
5411617
5411617
1
Why did you add the @objc in the first place? Also, have you tried declaring your class like this:public protocol MazeProtocol: class { ...
– RX9
Nov 23 '18 at 14:18
add a comment |
1
Why did you add the @objc in the first place? Also, have you tried declaring your class like this:public protocol MazeProtocol: class { ...
– RX9
Nov 23 '18 at 14:18
1
1
Why did you add the @objc in the first place? Also, have you tried declaring your class like this:
public protocol MazeProtocol: class { ...
– RX9
Nov 23 '18 at 14:18
Why did you add the @objc in the first place? Also, have you tried declaring your class like this:
public protocol MazeProtocol: class { ...
– RX9
Nov 23 '18 at 14:18
add a comment |
3 Answers
3
active
oldest
votes
Well [String: Selector]
is Dictionary<String, Selector>
which is a struct and structs cannot be represented in Objective-C, so you would need an NSDictionary
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> NSDictionary
}
add a comment |
As you can't use Selector
in Objective C Dictionary
directly, you can change your Swift dictionary's both key and value type to String as like below.
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> [String:String]
}
So when you want to get your Selector
from configurations
dictionary, get it as like below.
let selectorString = configurations()["KeyToSelector"]
let selector = NSSelectorFromString(selectorString)
add a comment |
As RX9 suggests, there's no reason (at least that you've explained) to mark this as @objc
, at either the function or protocol level. The following is fine:
public protocol MazeProtocol: AnyObject {
static func configurations() -> [String:Selector]
}
The point of @objc
is to allow ObjC objects to interact with this protocol. If you have Objective-C that needs to interact with this protocol, I strongly suggest defining this protocol on the ObjC side rather than on the Swift side. (But if you have that case, leave a comment, and we can walk through how to get what you need; as olejnjak notes, you can't put Selector directly in a dictionary that ObjC understands.
My goal is to have UIView and UIViewController adopt this protocol, so my understanding is that I need the @objc
– otusweb
Nov 26 '18 at 13:49
As long as the adoption is done in Swift, there's no need to mark this@objc
. The implementation language of the superclass is not important. All that's important is the code that references the protocol and method.
– Rob Napier
Nov 26 '18 at 14:15
Actually I dove back in the code and I forgot that I use the objectiveC runtime to find which class adopt this protocol. hence the @objc.
– otusweb
Nov 26 '18 at 15:59
In that case, this method can't be marked@objc
becauseSelector
can't be the value of anNSDictionary
. You'll need to rewrite it to use something other thanSelector
(generally you useString
and useNSStringFromSelector
andNSSelectorFromString
to convert.) Note that this likely means that all implementing classes must also be@objc
, so you probably meanNSObject
rather thanAnyObject
.
– Rob Napier
Nov 26 '18 at 16:01
I was able to store the selector in the dictionary, but I'm getting compiler error further down. I created a new question with more background as technically this question was answered even though my overall problem is not fixed :-) I'd love for you to take a look: stackoverflow.com/questions/53502451/…
– otusweb
Nov 27 '18 at 15:04
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448324%2fswift-protocol-to-return-a-dictionary-of-selector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well [String: Selector]
is Dictionary<String, Selector>
which is a struct and structs cannot be represented in Objective-C, so you would need an NSDictionary
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> NSDictionary
}
add a comment |
Well [String: Selector]
is Dictionary<String, Selector>
which is a struct and structs cannot be represented in Objective-C, so you would need an NSDictionary
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> NSDictionary
}
add a comment |
Well [String: Selector]
is Dictionary<String, Selector>
which is a struct and structs cannot be represented in Objective-C, so you would need an NSDictionary
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> NSDictionary
}
Well [String: Selector]
is Dictionary<String, Selector>
which is a struct and structs cannot be represented in Objective-C, so you would need an NSDictionary
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> NSDictionary
}
answered Nov 23 '18 at 14:21
olejnjakolejnjak
483313
483313
add a comment |
add a comment |
As you can't use Selector
in Objective C Dictionary
directly, you can change your Swift dictionary's both key and value type to String as like below.
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> [String:String]
}
So when you want to get your Selector
from configurations
dictionary, get it as like below.
let selectorString = configurations()["KeyToSelector"]
let selector = NSSelectorFromString(selectorString)
add a comment |
As you can't use Selector
in Objective C Dictionary
directly, you can change your Swift dictionary's both key and value type to String as like below.
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> [String:String]
}
So when you want to get your Selector
from configurations
dictionary, get it as like below.
let selectorString = configurations()["KeyToSelector"]
let selector = NSSelectorFromString(selectorString)
add a comment |
As you can't use Selector
in Objective C Dictionary
directly, you can change your Swift dictionary's both key and value type to String as like below.
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> [String:String]
}
So when you want to get your Selector
from configurations
dictionary, get it as like below.
let selectorString = configurations()["KeyToSelector"]
let selector = NSSelectorFromString(selectorString)
As you can't use Selector
in Objective C Dictionary
directly, you can change your Swift dictionary's both key and value type to String as like below.
@objc public protocol MazeProtocol: AnyObject {
@objc static func configurations() -> [String:String]
}
So when you want to get your Selector
from configurations
dictionary, get it as like below.
let selectorString = configurations()["KeyToSelector"]
let selector = NSSelectorFromString(selectorString)
answered Nov 23 '18 at 14:41
NatarajanNatarajan
2,29131127
2,29131127
add a comment |
add a comment |
As RX9 suggests, there's no reason (at least that you've explained) to mark this as @objc
, at either the function or protocol level. The following is fine:
public protocol MazeProtocol: AnyObject {
static func configurations() -> [String:Selector]
}
The point of @objc
is to allow ObjC objects to interact with this protocol. If you have Objective-C that needs to interact with this protocol, I strongly suggest defining this protocol on the ObjC side rather than on the Swift side. (But if you have that case, leave a comment, and we can walk through how to get what you need; as olejnjak notes, you can't put Selector directly in a dictionary that ObjC understands.
My goal is to have UIView and UIViewController adopt this protocol, so my understanding is that I need the @objc
– otusweb
Nov 26 '18 at 13:49
As long as the adoption is done in Swift, there's no need to mark this@objc
. The implementation language of the superclass is not important. All that's important is the code that references the protocol and method.
– Rob Napier
Nov 26 '18 at 14:15
Actually I dove back in the code and I forgot that I use the objectiveC runtime to find which class adopt this protocol. hence the @objc.
– otusweb
Nov 26 '18 at 15:59
In that case, this method can't be marked@objc
becauseSelector
can't be the value of anNSDictionary
. You'll need to rewrite it to use something other thanSelector
(generally you useString
and useNSStringFromSelector
andNSSelectorFromString
to convert.) Note that this likely means that all implementing classes must also be@objc
, so you probably meanNSObject
rather thanAnyObject
.
– Rob Napier
Nov 26 '18 at 16:01
I was able to store the selector in the dictionary, but I'm getting compiler error further down. I created a new question with more background as technically this question was answered even though my overall problem is not fixed :-) I'd love for you to take a look: stackoverflow.com/questions/53502451/…
– otusweb
Nov 27 '18 at 15:04
add a comment |
As RX9 suggests, there's no reason (at least that you've explained) to mark this as @objc
, at either the function or protocol level. The following is fine:
public protocol MazeProtocol: AnyObject {
static func configurations() -> [String:Selector]
}
The point of @objc
is to allow ObjC objects to interact with this protocol. If you have Objective-C that needs to interact with this protocol, I strongly suggest defining this protocol on the ObjC side rather than on the Swift side. (But if you have that case, leave a comment, and we can walk through how to get what you need; as olejnjak notes, you can't put Selector directly in a dictionary that ObjC understands.
My goal is to have UIView and UIViewController adopt this protocol, so my understanding is that I need the @objc
– otusweb
Nov 26 '18 at 13:49
As long as the adoption is done in Swift, there's no need to mark this@objc
. The implementation language of the superclass is not important. All that's important is the code that references the protocol and method.
– Rob Napier
Nov 26 '18 at 14:15
Actually I dove back in the code and I forgot that I use the objectiveC runtime to find which class adopt this protocol. hence the @objc.
– otusweb
Nov 26 '18 at 15:59
In that case, this method can't be marked@objc
becauseSelector
can't be the value of anNSDictionary
. You'll need to rewrite it to use something other thanSelector
(generally you useString
and useNSStringFromSelector
andNSSelectorFromString
to convert.) Note that this likely means that all implementing classes must also be@objc
, so you probably meanNSObject
rather thanAnyObject
.
– Rob Napier
Nov 26 '18 at 16:01
I was able to store the selector in the dictionary, but I'm getting compiler error further down. I created a new question with more background as technically this question was answered even though my overall problem is not fixed :-) I'd love for you to take a look: stackoverflow.com/questions/53502451/…
– otusweb
Nov 27 '18 at 15:04
add a comment |
As RX9 suggests, there's no reason (at least that you've explained) to mark this as @objc
, at either the function or protocol level. The following is fine:
public protocol MazeProtocol: AnyObject {
static func configurations() -> [String:Selector]
}
The point of @objc
is to allow ObjC objects to interact with this protocol. If you have Objective-C that needs to interact with this protocol, I strongly suggest defining this protocol on the ObjC side rather than on the Swift side. (But if you have that case, leave a comment, and we can walk through how to get what you need; as olejnjak notes, you can't put Selector directly in a dictionary that ObjC understands.
As RX9 suggests, there's no reason (at least that you've explained) to mark this as @objc
, at either the function or protocol level. The following is fine:
public protocol MazeProtocol: AnyObject {
static func configurations() -> [String:Selector]
}
The point of @objc
is to allow ObjC objects to interact with this protocol. If you have Objective-C that needs to interact with this protocol, I strongly suggest defining this protocol on the ObjC side rather than on the Swift side. (But if you have that case, leave a comment, and we can walk through how to get what you need; as olejnjak notes, you can't put Selector directly in a dictionary that ObjC understands.
answered Nov 23 '18 at 19:13
Rob NapierRob Napier
207k28306434
207k28306434
My goal is to have UIView and UIViewController adopt this protocol, so my understanding is that I need the @objc
– otusweb
Nov 26 '18 at 13:49
As long as the adoption is done in Swift, there's no need to mark this@objc
. The implementation language of the superclass is not important. All that's important is the code that references the protocol and method.
– Rob Napier
Nov 26 '18 at 14:15
Actually I dove back in the code and I forgot that I use the objectiveC runtime to find which class adopt this protocol. hence the @objc.
– otusweb
Nov 26 '18 at 15:59
In that case, this method can't be marked@objc
becauseSelector
can't be the value of anNSDictionary
. You'll need to rewrite it to use something other thanSelector
(generally you useString
and useNSStringFromSelector
andNSSelectorFromString
to convert.) Note that this likely means that all implementing classes must also be@objc
, so you probably meanNSObject
rather thanAnyObject
.
– Rob Napier
Nov 26 '18 at 16:01
I was able to store the selector in the dictionary, but I'm getting compiler error further down. I created a new question with more background as technically this question was answered even though my overall problem is not fixed :-) I'd love for you to take a look: stackoverflow.com/questions/53502451/…
– otusweb
Nov 27 '18 at 15:04
add a comment |
My goal is to have UIView and UIViewController adopt this protocol, so my understanding is that I need the @objc
– otusweb
Nov 26 '18 at 13:49
As long as the adoption is done in Swift, there's no need to mark this@objc
. The implementation language of the superclass is not important. All that's important is the code that references the protocol and method.
– Rob Napier
Nov 26 '18 at 14:15
Actually I dove back in the code and I forgot that I use the objectiveC runtime to find which class adopt this protocol. hence the @objc.
– otusweb
Nov 26 '18 at 15:59
In that case, this method can't be marked@objc
becauseSelector
can't be the value of anNSDictionary
. You'll need to rewrite it to use something other thanSelector
(generally you useString
and useNSStringFromSelector
andNSSelectorFromString
to convert.) Note that this likely means that all implementing classes must also be@objc
, so you probably meanNSObject
rather thanAnyObject
.
– Rob Napier
Nov 26 '18 at 16:01
I was able to store the selector in the dictionary, but I'm getting compiler error further down. I created a new question with more background as technically this question was answered even though my overall problem is not fixed :-) I'd love for you to take a look: stackoverflow.com/questions/53502451/…
– otusweb
Nov 27 '18 at 15:04
My goal is to have UIView and UIViewController adopt this protocol, so my understanding is that I need the @objc
– otusweb
Nov 26 '18 at 13:49
My goal is to have UIView and UIViewController adopt this protocol, so my understanding is that I need the @objc
– otusweb
Nov 26 '18 at 13:49
As long as the adoption is done in Swift, there's no need to mark this
@objc
. The implementation language of the superclass is not important. All that's important is the code that references the protocol and method.– Rob Napier
Nov 26 '18 at 14:15
As long as the adoption is done in Swift, there's no need to mark this
@objc
. The implementation language of the superclass is not important. All that's important is the code that references the protocol and method.– Rob Napier
Nov 26 '18 at 14:15
Actually I dove back in the code and I forgot that I use the objectiveC runtime to find which class adopt this protocol. hence the @objc.
– otusweb
Nov 26 '18 at 15:59
Actually I dove back in the code and I forgot that I use the objectiveC runtime to find which class adopt this protocol. hence the @objc.
– otusweb
Nov 26 '18 at 15:59
In that case, this method can't be marked
@objc
because Selector
can't be the value of an NSDictionary
. You'll need to rewrite it to use something other than Selector
(generally you use String
and use NSStringFromSelector
and NSSelectorFromString
to convert.) Note that this likely means that all implementing classes must also be @objc
, so you probably mean NSObject
rather than AnyObject
.– Rob Napier
Nov 26 '18 at 16:01
In that case, this method can't be marked
@objc
because Selector
can't be the value of an NSDictionary
. You'll need to rewrite it to use something other than Selector
(generally you use String
and use NSStringFromSelector
and NSSelectorFromString
to convert.) Note that this likely means that all implementing classes must also be @objc
, so you probably mean NSObject
rather than AnyObject
.– Rob Napier
Nov 26 '18 at 16:01
I was able to store the selector in the dictionary, but I'm getting compiler error further down. I created a new question with more background as technically this question was answered even though my overall problem is not fixed :-) I'd love for you to take a look: stackoverflow.com/questions/53502451/…
– otusweb
Nov 27 '18 at 15:04
I was able to store the selector in the dictionary, but I'm getting compiler error further down. I created a new question with more background as technically this question was answered even though my overall problem is not fixed :-) I'd love for you to take a look: stackoverflow.com/questions/53502451/…
– otusweb
Nov 27 '18 at 15:04
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448324%2fswift-protocol-to-return-a-dictionary-of-selector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Why did you add the @objc in the first place? Also, have you tried declaring your class like this:
public protocol MazeProtocol: class { ...
– RX9
Nov 23 '18 at 14:18