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;
}







1















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.










share|improve this question




















  • 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















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.










share|improve this question




















  • 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








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












3 Answers
3






active

oldest

votes


















3














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
}





share|improve this answer































    0














    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)





    share|improve this answer































      0














      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.






      share|improve this answer
























      • 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 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












      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      3














      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
      }





      share|improve this answer




























        3














        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
        }





        share|improve this answer


























          3












          3








          3







          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
          }





          share|improve this answer













          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
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 14:21









          olejnjakolejnjak

          483313




          483313

























              0














              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)





              share|improve this answer




























                0














                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)





                share|improve this answer


























                  0












                  0








                  0







                  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)





                  share|improve this answer













                  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)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 14:41









                  NatarajanNatarajan

                  2,29131127




                  2,29131127























                      0














                      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.






                      share|improve this answer
























                      • 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 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
















                      0














                      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.






                      share|improve this answer
























                      • 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 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














                      0












                      0








                      0







                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      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 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



















                      • 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 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

















                      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


















                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy