2

I was wondering is it possible to add functions to an array in swift?, And if so how would I go about doing that, an example would be helpful.

thankyou in advance

    enum MyFunc{
    case Arity0 (Void ->Void) 


}

var cycle = Array<MyFunc>()

to add to the array:

    func addToArray(){

    cycle.append(MyFunc.Arity0(ani1))
    cycle.append(MyFunc.Arity0(ani2))
    cycle.append(MyFunc.Arity0(ani3))
    cycle.append(MyFunc.Arity0(ani4))
    cycle.append(MyFunc.Arity0(ani5))

}
4
  • 1
    I think you should clarify the question. Do you want to make an array of functions or do you want to extend the "functionality" of an Array type? Both are possible. Commented Feb 24, 2016 at 20:17
  • @Eppilo , my reason for asking this question, is that i have a set of 5 images on screen that i have animated, they are the all same image, and i wanted each image to loop one by one, and then repeat. each of the images has a function which allows the animation to occur, i though it may be best(and i may be wrong here), that if i place the functions in to an array, i may be able to animate each image individually one after another (sorry for the long reply) Commented Feb 24, 2016 at 20:22
  • So if understand correctly you want to declare an array of functions. All you need to do is is to put the function signature in the square brackets and initialise. Example: " [()->()]() " Commented Feb 24, 2016 at 20:39
  • @Eppilo so would the above (added code) be another way of adding my functions to the array. my functions do not return anything. Commented Feb 24, 2016 at 20:45

1 Answer 1

2

If you want to create an array of functions, and each function does not take parameters and returns noting you can do:

var cycle = [()->()]()
cycle.append(nameOfYourFunctionNoBrackets)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.