Guten Tag. Ich habe versucht, dieses Tutorial https://www.youtube.com/watch?v=3jJiqzbzutU
zu folgen und hatte diese Fehlermeldung Kann einen Wert vom Typ '[Int: UIColor]' mit einem Index des Typs NSNumber
nicht indizieren. Ich habe das gleiche getan wie im Tutorial und das ist schiefgelaufen. Bitte helfen Sie. Der Fehler kommt von dieser CodezeileKann einen Wert vom Typ '[Int: UIColor]' nicht mit einem Index vom Typ 'NSNumber' indizieren
self.view.backgroundColor = self.colors[closestBeacon.major]
.
Hier ist mein Code als Referenz.
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
let region1 = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "myuuidString")!, identifier: "skybeacon1")
let colors = [2: UIColor.redColor(), 1: UIColor.blueColor()]
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
if CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse {
locationManager.requestWhenInUseAuthorization()
}
locationManager.startRangingBeaconsInRegion(region1)
}
func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
let knownBeacons = beacons.filter{ $0 .proximity != CLProximity.Unknown }
if knownBeacons.count > 0 {
let closestBeacon = knownBeacons[0] as CLBeacon
self.view.backgroundColor = self.colors[closestBeacon.major]
}
}
}
'engsteBeacon.major' ist eine' NSNummer'. Und du willst ein "int" für self.colors [myInt]. Sie müssen also 'nestBeacon.major' in ein' int' konvertieren. – Larme