How To Store Swift function in a variableHow do JavaScript closures work?How to “add existing frameworks” in Xcode 4?Using Auto Layout in UITableView for dynamic cell layouts & variable row heightsHow to call Objective-C code from Swift#ifdef replacement in the Swift language#pragma mark in Swift?How can I extend typed Arrays in Swift?Swift Beta performance: sorting arraysUsage of protocols as array types and function parameters in swiftSplit a String into an array in Swift?

Is it possible to do 50 km distance without any previous training?

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

Do I have a twin with permutated remainders?

How can I make my BBEG immortal short of making them a Lich or Vampire?

How to say job offer in Mandarin/Cantonese?

Have astronauts in space suits ever taken selfies? If so, how?

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

Is this a crack on the carbon frame?

Why do falling prices hurt debtors?

What's the point of deactivating Num Lock on login screens?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

Example of a continuous function that don't have a continuous extension

Why are electrically insulating heatsinks so rare? Is it just cost?

Why Is Death Allowed In the Matrix?

What do you call a Matrix-like slowdown and camera movement effect?

Is it important to consider tone, melody, and musical form while writing a song?

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

Is it unprofessional to ask if a job posting on GlassDoor is real?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

What does "Puller Prush Person" mean?

Python: return float 1.0 as int 1 but float 1.5 as float 1.5

Why don't electron-positron collisions release infinite energy?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?



How To Store Swift function in a variable


How do JavaScript closures work?How to “add existing frameworks” in Xcode 4?Using Auto Layout in UITableView for dynamic cell layouts & variable row heightsHow to call Objective-C code from Swift#ifdef replacement in the Swift language#pragma mark in Swift?How can I extend typed Arrays in Swift?Swift Beta performance: sorting arraysUsage of protocols as array types and function parameters in swiftSplit a String into an array in Swift?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















// Assume you have two functions.
// Swift considers these functions as distinct
func cars(left look: String)
func cars(right look: String)

// How can I store them in a variable for later use?
let c: (left : String) -> () = cars
let c2: (right : String) -> () = cars


When I try to store these methods into a variable, I get an error stating that "cars" is ambiguous. What can I do to differentiate them?










share|improve this question






























    0















    // Assume you have two functions.
    // Swift considers these functions as distinct
    func cars(left look: String)
    func cars(right look: String)

    // How can I store them in a variable for later use?
    let c: (left : String) -> () = cars
    let c2: (right : String) -> () = cars


    When I try to store these methods into a variable, I get an error stating that "cars" is ambiguous. What can I do to differentiate them?










    share|improve this question


























      0












      0








      0








      // Assume you have two functions.
      // Swift considers these functions as distinct
      func cars(left look: String)
      func cars(right look: String)

      // How can I store them in a variable for later use?
      let c: (left : String) -> () = cars
      let c2: (right : String) -> () = cars


      When I try to store these methods into a variable, I get an error stating that "cars" is ambiguous. What can I do to differentiate them?










      share|improve this question
















      // Assume you have two functions.
      // Swift considers these functions as distinct
      func cars(left look: String)
      func cars(right look: String)

      // How can I store them in a variable for later use?
      let c: (left : String) -> () = cars
      let c2: (right : String) -> () = cars


      When I try to store these methods into a variable, I get an error stating that "cars" is ambiguous. What can I do to differentiate them?







      ios swift xcode polymorphism closures






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 21 '16 at 0:06







      DerrickHo328

















      asked Jul 20 '16 at 23:41









      DerrickHo328DerrickHo328

      2,02541839




      2,02541839






















          3 Answers
          3






          active

          oldest

          votes


















          3














          You have to help the compiler decide which function you want because it does not distinguish between parameter names when deciding if a function's signature is valid for assignment.



          You can disambiguate your intent by being specific on the parameter names



          let c: (left :String) -> () = cars(left:)
          let c2: (right:String) -> () = cars(right:)





          share|improve this answer






























            0














            Your two functions have the same set of parameters, string.
            To be able to have a polymorphic function, each function must have
            a different set of parameter types.



            func cars( look: String) 
            func cars( look: Int)


            You were able to get away from the compiler by adding left and right
            description which is the preferred style for Objective-C but not for swift.






            share|improve this answer






























              0














              If you already have 2 functions with the same name which you cannot change



              func cars(left look: String) print(0) 
              func cars(right look: String) print(1)


              and you need to store them into 2 variables you could follow this approach



              let c0: (left : String) -> () = cars(left:$0) 
              let c1: (right : String) -> () = cars(right:$0)



              As you can see I created 2 closures, they call respectively the first and the second one function.




              Test



              c0(left: "") // 0
              c1(right: "") // 1





              share|improve this answer























                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%2f38492552%2fhow-to-store-swift-function-in-a-variable%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














                You have to help the compiler decide which function you want because it does not distinguish between parameter names when deciding if a function's signature is valid for assignment.



                You can disambiguate your intent by being specific on the parameter names



                let c: (left :String) -> () = cars(left:)
                let c2: (right:String) -> () = cars(right:)





                share|improve this answer



























                  3














                  You have to help the compiler decide which function you want because it does not distinguish between parameter names when deciding if a function's signature is valid for assignment.



                  You can disambiguate your intent by being specific on the parameter names



                  let c: (left :String) -> () = cars(left:)
                  let c2: (right:String) -> () = cars(right:)





                  share|improve this answer

























                    3












                    3








                    3







                    You have to help the compiler decide which function you want because it does not distinguish between parameter names when deciding if a function's signature is valid for assignment.



                    You can disambiguate your intent by being specific on the parameter names



                    let c: (left :String) -> () = cars(left:)
                    let c2: (right:String) -> () = cars(right:)





                    share|improve this answer













                    You have to help the compiler decide which function you want because it does not distinguish between parameter names when deciding if a function's signature is valid for assignment.



                    You can disambiguate your intent by being specific on the parameter names



                    let c: (left :String) -> () = cars(left:)
                    let c2: (right:String) -> () = cars(right:)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 21 '16 at 0:29









                    Alain T.Alain T.

                    8,54711329




                    8,54711329























                        0














                        Your two functions have the same set of parameters, string.
                        To be able to have a polymorphic function, each function must have
                        a different set of parameter types.



                        func cars( look: String) 
                        func cars( look: Int)


                        You were able to get away from the compiler by adding left and right
                        description which is the preferred style for Objective-C but not for swift.






                        share|improve this answer



























                          0














                          Your two functions have the same set of parameters, string.
                          To be able to have a polymorphic function, each function must have
                          a different set of parameter types.



                          func cars( look: String) 
                          func cars( look: Int)


                          You were able to get away from the compiler by adding left and right
                          description which is the preferred style for Objective-C but not for swift.






                          share|improve this answer

























                            0












                            0








                            0







                            Your two functions have the same set of parameters, string.
                            To be able to have a polymorphic function, each function must have
                            a different set of parameter types.



                            func cars( look: String) 
                            func cars( look: Int)


                            You were able to get away from the compiler by adding left and right
                            description which is the preferred style for Objective-C but not for swift.






                            share|improve this answer













                            Your two functions have the same set of parameters, string.
                            To be able to have a polymorphic function, each function must have
                            a different set of parameter types.



                            func cars( look: String) 
                            func cars( look: Int)


                            You were able to get away from the compiler by adding left and right
                            description which is the preferred style for Objective-C but not for swift.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jul 21 '16 at 0:04









                            Christian AbellaChristian Abella

                            4,75622234




                            4,75622234





















                                0














                                If you already have 2 functions with the same name which you cannot change



                                func cars(left look: String) print(0) 
                                func cars(right look: String) print(1)


                                and you need to store them into 2 variables you could follow this approach



                                let c0: (left : String) -> () = cars(left:$0) 
                                let c1: (right : String) -> () = cars(right:$0)



                                As you can see I created 2 closures, they call respectively the first and the second one function.




                                Test



                                c0(left: "") // 0
                                c1(right: "") // 1





                                share|improve this answer



























                                  0














                                  If you already have 2 functions with the same name which you cannot change



                                  func cars(left look: String) print(0) 
                                  func cars(right look: String) print(1)


                                  and you need to store them into 2 variables you could follow this approach



                                  let c0: (left : String) -> () = cars(left:$0) 
                                  let c1: (right : String) -> () = cars(right:$0)



                                  As you can see I created 2 closures, they call respectively the first and the second one function.




                                  Test



                                  c0(left: "") // 0
                                  c1(right: "") // 1





                                  share|improve this answer

























                                    0












                                    0








                                    0







                                    If you already have 2 functions with the same name which you cannot change



                                    func cars(left look: String) print(0) 
                                    func cars(right look: String) print(1)


                                    and you need to store them into 2 variables you could follow this approach



                                    let c0: (left : String) -> () = cars(left:$0) 
                                    let c1: (right : String) -> () = cars(right:$0)



                                    As you can see I created 2 closures, they call respectively the first and the second one function.




                                    Test



                                    c0(left: "") // 0
                                    c1(right: "") // 1





                                    share|improve this answer













                                    If you already have 2 functions with the same name which you cannot change



                                    func cars(left look: String) print(0) 
                                    func cars(right look: String) print(1)


                                    and you need to store them into 2 variables you could follow this approach



                                    let c0: (left : String) -> () = cars(left:$0) 
                                    let c1: (right : String) -> () = cars(right:$0)



                                    As you can see I created 2 closures, they call respectively the first and the second one function.




                                    Test



                                    c0(left: "") // 0
                                    c1(right: "") // 1






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jul 21 '16 at 0:04









                                    Luca AngelettiLuca Angeletti

                                    41.7k575118




                                    41.7k575118



























                                        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%2f38492552%2fhow-to-store-swift-function-in-a-variable%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

                                        Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                                        How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

                                        Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme