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;
// 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
add a comment |
// 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
add a comment |
// 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
// 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
ios swift xcode polymorphism closures
edited Jul 21 '16 at 0:06
DerrickHo328
asked Jul 20 '16 at 23:41
DerrickHo328DerrickHo328
2,02541839
2,02541839
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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:)
add a comment |
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.
add a comment |
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
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%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
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:)
add a comment |
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:)
add a comment |
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:)
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:)
answered Jul 21 '16 at 0:29


Alain T.Alain T.
8,54711329
8,54711329
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jul 21 '16 at 0:04


Christian AbellaChristian Abella
4,75622234
4,75622234
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jul 21 '16 at 0:04


Luca AngelettiLuca Angeletti
41.7k575118
41.7k575118
add a comment |
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%2f38492552%2fhow-to-store-swift-function-in-a-variable%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