Swift 我可以在CLCircularRegion中设置值吗?
我正在使用Swift 我可以在CLCircularRegion中设置值吗?,swift,swift4,clcircularregion,Swift,Swift4,Clcircularregion,我正在使用CLCircularRegion开发应用程序 我想在CLCircularRegion中设置值。所以我做了region.setValue(forKey:key),但它显示了 if CLLocationManager.locationServicesEnabled() { for obj in pushInfoArr! { let localLatiStr = obj["local_latit"] as! String
CLCircularRegion
开发应用程序
我想在CLCircularRegion
中设置值。所以我做了region.setValue(forKey:key)
,但它显示了
if CLLocationManager.locationServicesEnabled() {
for obj in pushInfoArr! {
let localLatiStr = obj["local_latit"] as! String
let localLongitStr = obj["local_longit"] as! String
let localMsg = obj["local_mesg"] as! String
let url = obj["cnnct_url"] as! String
let localLati = Double(localLatiStr)
let localLongi = Double(localLongitStr)
let center = CLLocationCoordinate2DMake(localLati!, localLongi!)
region = CLCircularRegion(center: center, radius: 1000, identifier: localMsg)
region.setValue(localMsg, forKey: "msg")
region.setValue(url, forKey: "url")
region.notifyOnEntry = true
self.locationManager.startMonitoring(for: region)
}
}
“由于未捕获的异常“NSUnknownKeyException”而终止应用程序,
理由:'[
setValue:forUndefinedKey::此类不是键值
符合密钥bb的编码要求”
我可以在
CLCircularRegion
中设置值(url)吗?不,CLCircularRegion没有名为url的属性,因此您不能在该类型的对象上设置url。Swift不是Javascript-您不能在现有类或结构上创建新属性,它继承的CLCircularRegion
或CLRegion
都没有属性msg
或url
您误解了setValue
的功能-它是基类NSObject
上的一个方法,允许通过引用类的“名称”或键来设置类的现有属性。在Swift中不建议使用它(这是Objective-C遗留版本)
您需要做的是创建一个结构或子类来保存您的区域和所需的属性:
或者
struct MyRegion {
var region: CLCircularRegion
var msg: String
var url: String
}
或
p、 不要使用“url”作为非
url
属性的名称-称之为“urlString”或类似名称。我只是用它来对应您问题的术语。请显示您正在使用的代码。您要设置什么值?边界框(“bb”)?您可以通过clRegion.radius=123.4
简单地设置半径-中心位置类似。请看这里更新的问题!我想设置url和消息以显示通知。当我通过通知运行应用程序时,我想去保存的url,因为我看不到它继承的CLCircularRegion
或CLRegion
都有属性msg
或url
-所以你不能使用setValue
来设置这些属性。没有办法保存url吗?是否只有标识符可用?
class MyRegion: CLCircularRegion {
var msg: String
var url: String
init(centre: CLLocationCoordinate2D, radius: Double, identifier: String, msg: String, url: String) {
super.init(centre: centre, radius: radius, identifier: identifier)
self.msg = msg
self.url = url
}
}