What is the purpose of using "->" on a variable in Swift? -


i following apple's ebook titled "the swift programming language". in it, there code sample creates function. function uses "-> bool" @ end, understand means function have boolean output.

to configure function, uses 2 input variables. 1 of variables "int -> bool" (see code below). maybe there better explanation of later in ebook searches not quite explain how , why use "->" nomenclature on variable.

func hasanymatches(list: [int], condition: int -> bool) -> bool {     item in list {         if condition(item) {             return true         }     }     return false } func lessthanten(number: int) -> bool {     return number < 10 } var numbers = [20, 19, 10, 12] hasanymatches(numbers, lessthanten) 

since not explained, best way can read code sample "condition" variable not variable function in , of itself. therefore way read code follows:

"condition function accepts integer , returns boolean variable."

is assumption correct?

thank you.

you assumption correct. in swift, functions can passed arguments within functions.

from believe, code referencing apple's "the swift programming language" ebook. nearby, states that:

“a function can take function 1 of arguments.”

basically, happening here have created function, "lessthanten()" takes int argument , returns bool value. next create function, "hasanymatches" uses function within (i.e, int -> bool). can useful if have different functions take , return same value types, have different purpose.

you can pass original function argument variable. function can take form long satisfies conditions specified second function (i.e, "condition: int -> bool").

in conclusion, in second function have line

if condition(item) { 

basically, line runs function "condition" which, in reality, function passed argument. create function takes int , returns bool value , pass along "hasanymatches".

if want read more this, suggest looking further along in "the swift programming language" chapter titled "functions". there states:

“you can use function type such (int, int) -> int parameter type function. enables leave aspects of function’s implementation function’s caller provide when function called.” (pg. 141)

i hope answers question :)


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -