Bright Future Bright Future - 2 months ago 8
Swift Question

What is return type " () "?

I have a function, which takes several other functions as arguments:

class Iterator {
func iterateItems(itemArray: [Items], removeItem: (Items) -> Void, addItem: (inout Items) -> Void, calculateEfficiency: () -> Void) -> [Items] {
// function body
}
}


And I call it in its class' subclass like this:

class WPCalculator: Iterator {

func removeWeaponItem(item: WeaponItems) { ... }
func addWeaponItem(item: inout WeaponItems) { ... }
func calcWeaponDamage() { ... }

func iterateWPItems() {
return iterateItems(itemArray: WeaponItems.weaponItems, removeItem: removeWeaponItem as! (Items) -> Void, addItem: addWeaponItem as! (inout Items) -> Void, calculateEfficiency: calcWeaponDemage)
}
}


Also the
WeaponItems
class is a subclass of
Items
class:

class WeaponItems: Items { ... }


Xcode gives me a warning on
addWeaponItem
:


Cast from
(inout WeaponItems) -> ()
to unrelated type
(inout Items) -> Void
always fails





I can't understand the
-> ()
return type, clearly its not what my function does, the other functions passed as arguments without inout parameters just worked fine

Answer

Your function iterateItems has removeItem addItem calculateEfficiency parameter is function type. So you must use function name as params when call function.

like this:

iterateItems(itemArray: WeaponItems.weaponItems, removeItem: removeWeaponItem, addItem: addWeaponItem, calculateEfficiency: calcWeaponDamage)
Comments