Skip to content

Instantly share code, notes, and snippets.

@ryugoo
Created June 8, 2014 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryugoo/00ec5ac8be648f020103 to your computer and use it in GitHub Desktop.
Save ryugoo/00ec5ac8be648f020103 to your computer and use it in GitHub Desktop.
Swift closure
class MyClass {
// Instance variables
var myStr: String
// Constructor
init() {
myStr = "Hello "
}
// Method
func myFunc(value:String) -> String {
var myClosure: (String) -> (String) = {
[unowned self] (value: String) -> (String) in
let retStr = self.myStr + value
return retStr
}
return myClosure(value)
}
func myMethod(value:String) -> String {
let retStr = self.myStr + value
return retStr
}
}
let myInstance = MyClass()
myInstance.myFunc("World")
myInstance.myMethod("World")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment