Jack Berstrem
Jack Berstrem

Reputation: 535

Why is my Swift function asking me for the wrong variables

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: ..., .....)

Upvotes: 1

Views: 83

Answers (2)

E-Riddie
E-Riddie

Reputation: 14780

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

Upvotes: 3

iGongora
iGongora

Reputation: 23

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)

Upvotes: 2

Related Questions