1

I made a struct, and a function inside of it and I don't understand why it is asking me for the wrong parameters when I try to call it. Here is my function declaration:

struct UserInterfaceVariables { func applyCustomAnimation(view: AnimatableView, animationType: String, duration: Double, delay: Double, damping: CGFloat, velocity: CGFloat, force: CGFloat) { view.animationType = animationType view.duration = duration view.delay = delay view.damping = damping view.velocity = velocity view.force = force } } 

And in another view controller, I try to call it, it asks me for a "UserInterfaceVariables" parameter, but that isn't the type of parameter I want to input... I want to input the parameters from my function declaration ("animationType", etc...)

UserInterfaceVariables.applyCustomAnimation(UserInterfaceVariables) 

Do note that the "UserInterfaceVariables" entry (in the parentheses) is the type of parameter it is expecting... i.e. this is what it looks like on Xcode:

enter image description here

Why is it not asking me for the parameters declared in my function definition ?

EDIT 1 Tentative solution:

let userInterfaceVariables = UserInterfaceVariables() applyCustomAnimation(view: ..., .....) 
3
  • 1
    You're trying to call your method on the type itself instead of an instance of it Commented Mar 10, 2016 at 23:10
  • Oh ok, so should I do something that ressembles the edit I just made ? Declare an instance of UserInterfaceVariables() and then call the function that was defined inside the UserInterfaceVariables struct ? Commented Mar 10, 2016 at 23:12
  • If you want to be able to call the method on the type then you should declare the function as static Commented Mar 10, 2016 at 23:12

2 Answers 2

3

Issue

You are calling an instance method from a type. Read more

Solution

Either create an instance of the struct like iGongora pointed out, or mark the your method as static

static func applyCustomAnimation(view: AnimatableView, animationType: String, duration: Double, delay: Double, damping: CGFloat, velocity: CGFloat, force: CGFloat) { view.animationType = animationType view.duration = duration view.delay = delay view.damping = damping view.velocity = velocity view.force = force } 

Now you will be able to call the method correctly with your current code

Cheers

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, I'm still a beginner at all this so to be honest, I have no idea what static really means. Thanks for the explanation, I just tested it and it works like a charm.
You are welcome. Just practice and read docs and you will be in good shape soon :)
2

That's because you need to init the struct before use it and the parameter is telling you. Once you init your struct, the parameters will change.

Try let userInterface = UserInterfaceVariables() and then userInterface.applyCustomAnimation(yourParameters)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.