我可以在 CLCircularRegion 中设置值吗?
Could I set value in CLCircularRegion?
我正在使用 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)
}
}
"Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[
setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key bb"
我可以在 CLCircularRegion
中设置值 (url) 吗?
不,CLCircularRegion 没有名为 url 的 属性,因此您不能在该类型的对象上设置 url。
Swift 不是 Javascript - 您不能在现有 class 或结构上创建新属性,CLCircularRegion
和 CLRegion
都不能它继承的属性具有 msg
或 url
。
您误解了 setValue
的作用 - 它是基于 class NSObject
的方法,它允许设置 现有 class 的 属性 通过引用其 "name" 或键。 Swift 不推荐使用它(它是 Objective-C 遗留的)。
您需要做的是创建一个包含您的区域和所需属性的结构或子class:
两者之一
struct MyRegion {
var region: CLCircularRegion
var msg: String
var url: String
}
或
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
}
}
p.s。不要使用 'url' 作为不是 URL
的 属性 的名称 - 称它为 'urlString' 或类似名称。我只是用它来对应你问题的术语。
我正在使用 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)
}
}
"Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key bb"
我可以在 CLCircularRegion
中设置值 (url) 吗?
不,CLCircularRegion 没有名为 url 的 属性,因此您不能在该类型的对象上设置 url。
Swift 不是 Javascript - 您不能在现有 class 或结构上创建新属性,CLCircularRegion
和 CLRegion
都不能它继承的属性具有 msg
或 url
。
您误解了 setValue
的作用 - 它是基于 class NSObject
的方法,它允许设置 现有 class 的 属性 通过引用其 "name" 或键。 Swift 不推荐使用它(它是 Objective-C 遗留的)。
您需要做的是创建一个包含您的区域和所需属性的结构或子class:
两者之一
struct MyRegion {
var region: CLCircularRegion
var msg: String
var url: String
}
或
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
}
}
p.s。不要使用 'url' 作为不是 URL
的 属性 的名称 - 称它为 'urlString' 或类似名称。我只是用它来对应你问题的术语。