Trying to do a basic web view app for experimenting, I ran into the loadRequest method. Methods with swift accept the named parameters, however loadRequest does not. Just wondering on the reasoning for this? I found the inconsistency kind of odd.
import UIKit
class ViewController: UIViewController {
@IBOutlet var webView: UIWebView
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://en.wikipedia.org")
let request = NSURLRequest(URL: url)
self.webView.loadRequest(request)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The parameters in standalone functions and instance methods in a class are handled differently. The Swift book has this to say about it:
“Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default. This convention matches the typical naming and calling convention you will be familiar with from writing Objective-C methods, and makes for expressive method calls without the need to qualify your parameter names.”
An example:
class Counter {
var count: Int = 0
func incrementBy(amount: Int, numberOfTimes: Int) {
count += amount * numberOfTimes
}
}
var myCounter = Counter()
myCounter.incrementBy(3, numberOfTimes: 4)
println(myCounter.count) // 12
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With