如何在Swift中从关联数组获取UIColor [英] How to get a UIColor from Associative Array in Swift

查看:112
本文介绍了如何在Swift中从关联数组获取UIColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带有UIColors的数组:

I have this array with some UIColors:

let colors = [
    35302: UIColor(red: 66/255, green: 55/255, blue: 101/255, alpha: 1),
    53350: UIColor(red: 158/255, green: 218/255, blue: 222/255, alpha: 1),
    54747: UIColor(red: 158/255, green: 222/255, blue: 189/255, alpha: 1)
]

现在,我正在尝试访问数组中的某些索引:

Now, I'm trying to access some index in Array:

func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, 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.minor]
    }
}

该行:

self.view.backgroundColor = self.colors[closestBeacon.minor]

我收到此错误:

Cannot subscript a value of type '[Int: UIColor]' with an index of type 'NSNumber'

我正在尝试用新的Beacon创建一个HelloWorld,而我被困在这一部分中. 我想了解这是如何工作的.

I'm trying to make a HelloWorld with my new Beacons and I'm stuck in this part. I would like to understand how this really work.

谢谢

推荐答案

您使用NSNumber,但数组需要Int作为索引.选择以下选项之一:

You use NSNumber but array requires Int as index. Choose one of the following:

self.view.backgroundColor = self.colors[closestBeacon.minor as Int]
self.view.backgroundColor = self.colors[closestBeacon.minor.integerValue]
self.view.backgroundColor = self.colors[Int(closestBeacon.minor)]

您还可以检查此问题将NSNumber的对象快速转换为双

这篇关于如何在Swift中从关联数组获取UIColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆