Quindi quello che voglio è una classe che può ottenere una chiusura passata ad esso in una funzione, potrebbe anche a un certo punto voler ignorare una chiusura. Come posso verificare se la variabile di chiusura è impostata e hwo posso cancellarla quando ho finito?chiusura opzionale e verificare se è nil
non può invocare '=!' Con una lista di argomenti di tipo '(@lvalue (sucsess: Bool !, prodotti:! [ANYOBJECT]) ->() ?, NilLiteralConvertible)' Tipo '(sucsess : Bool !, prodotti: [AnyObject]!) ->()? ' non è conforme al protocollo 'NilLiteralConvertible'
class someClass{
//typealias completionHandlerClosureType = (sucsess:Bool!, items:[AnyObject]!)->()
var completionHandler:(sucsess:Bool!, items:[AnyObject]!)->()?
var hitpoints = 100
var someset = ["oh no!","avenge me!"]
init(){}
func getHitFunc(impact:Int, passedCompletionsHandler:(sucsess:Bool!, items:[AnyObject]!)->()){
completionHandler = passedCompletionsHandler
hitpoints = hitpoints - impact
}
func checkIfDead{
if hitpoints<=0 { // The error received
if completionHandler != nil{// Cannot invoke '!=' with an argument list of type
//'(@lvalue (sucsess: Bool!, products: [AnyObject]!) ->()?, NilLiteralConvertible)'
//run the handler if dead
completionHandler(sucsess: true, items: someset)
//do not run it again
completionHandler = nil //Type '(sucsess: Bool!, products: [AnyObject]!) ->()?' does not conform to protocol 'NilLiteralConvertible'
}
}
else{
completionHandler = nil //Type '(sucsess: Bool!, products: [AnyObject]!) ->()?' does not conform to protocol 'NilLiteralConvertible'
}
}
}
l'alias di tipo è la soluzione ideale – Yitzchak