Search code examples
iosswiftuibuttonhiddensender

SWIFT: "removeAtIndex" don't work with (sender:UIButton)


@IBOutlet var items: [UIButton]
@IBAction func itemsHidden(sender: UIButton) {
    sender.hidden = true
    items.removeAtIndex(sender)
    }

Hello.

For example, i have array of items.

The code has the error: "Cannot invoke 'removeAtIndex' with an argument list of type (UIButton)". What i need to do, that "removeAtIndex" works?

Thanks...


Solution

  • A removeAtIndex method expects to get an index as a parameter. If you want to remove an object use func removeObject(_ anObject: AnyObject)

    EDIT

    There's no removeObject in a swift's array (only in NSMutableArray). In order to remove an element, you need to figure out it's index first:

    if let index = find(items, sender) {
        items.removeAtIndex(index)
    }