ios - Closures in Swift, blocks in Objective-C: their usefulness and when to use -
i've found objective-c socketio library , trying implement in first swift app. here's code i'm trying port: __weak typeof(self) weakself = self; self.socket.onconnect = ^() { weakself.socketisconnected = yes; [weakself mapview: weakself.mapview didupdateuserlocation: weakself.mapview.userlocation]; }; from limited understanding ^() {} block in objective c. i've looked , closures seem loose equivalent swift . first obvious question how same result in swift? i've tried following error fatal error: unexpectedly found nil while unwrapping optional value (lldb) : self.socket.onconnect = { () -> void in println("connected!") } also, behind scenes what's happening here? asynchronous callback function seem appropriate wasn't used , i'd understand why. update as pointed out @jtbandes, socket in fact nil code running outside of connection callback (i know, silly mistake). solution first question: siosocket.socketwith...