Swift - optional parameter not working as expected

Viewed 442

I have a method that iterate a list of category class, and return items from all the categories in the end. the simple code look like this:

func iterateMyCategoriesItems(item:(_ category:Category) -> Void)
{
    for category in allCategories
    {
        item(category)
    }
}

when using it:

iterateMyCategoriesItems { (category) in

   // doing something here with every category...
}

so far so good, but now i want to add an optional completion to this method, so i changed the code to:

func iterateMyCategoriesItems(item:(_ category:Category) -> Void, _ completion:(() -> Void)? = nil)
{
    for category in allCategories
    {
        item(category)
    }

    completion?()
}

But now when i'm trying to use the method like this:

iterateMyCategoriesItems { (category) in

   // doing something here with every category...
}

The compiler shows an error:

Missing argument for parameter 'item' in call.

So, what i am doing wrong?

3 Answers
Related