Swift func, class
func
func method()-> Int {
println("")
return 1
}
var and:Int = method()
func method(val1:Int, val2:Int) -> Int {
return val1 + val2
}
let test = method(2, val2: 3)
func method2(val1:Int = 3) -> Int {
return val1
}
Return multiple values
func multires() -> (Int, String, Bool) {
return (1, "Wow", true)
}
var answer = multires()
answer.0
answer.1
answer.2
Class
.swift No header
Example
class FirstClass {
var str = "Good!"
func method1(val:String) {
println("Go \(str)")
}
class func method2(val:Int) {
return 1
}
}
var val1 = FirstClass()
class method starts with class
Extend class
ViewController Example
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Protocol
class ViewController: UIViewController, UITableViewDataSource, UITabViewDelegate {
}
Extension
Instead of Category, we can use Extension.
This is very simple.
Example
extension UIWebView {
func URLString() -> String? {
return self.stringByEvaluatingJavaScriptFromString("document.URL")
}
}
