我得到了大約10個警告標誌,讓我知道我應該考慮在這些情況下使用let代替var。讓var是變化的,這是一個常數。在這種情況下,會不會做出正確的決定?在Swift 2中使用var vs let讓地圖
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var latitude: CLLocationDegrees = 50.0358
var longitute: CLLocationDegrees = 19.1783
var latDelta: CLLocationDegrees = 0.5
var longDelta: CLLocationDegrees = 0.5
var span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitute)
var region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: true)
var annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Auschwiz"
annotation.subtitle = "German Nazi concentration camp."
mapView.addAnnotation(annotation)
var uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")
uilpgr.minimumPressDuration = 2
mapView.addGestureRecognizer(uilpgr)
Swift團隊建議默認情況下使用常量,並且只有在您確實需要在代碼邏輯中更改此對象時才使用變量。所以默認情況下,除非你真的不能使它工作並需要一個變量,否則使用'let'。 tl; dr編譯器是正確的。 – Moritz