本文将为您提供关于使用Swift添加不带指针的KVO观察器的详细介绍,同时,我们还将为您提供关于11.6Swift重写属性观察器、9.7Swift属性观察器、ios–使用swift添加Facebook
本文将为您提供关于使用Swift添加不带指针的KVO观察器的详细介绍,同时,我们还将为您提供关于11.6 Swift重写属性观察器、9.7 Swift属性观察器、ios – 使用swift添加Facebook时出现ParseUI错误、ios – 在swift 2.1中使用MapKit添加不同的引脚颜色的实用信息。
本文目录一览:- 使用Swift添加不带指针的KVO观察器
- 11.6 Swift重写属性观察器
- 9.7 Swift属性观察器
- ios – 使用swift添加Facebook时出现ParseUI错误
- ios – 在swift 2.1中使用MapKit添加不同的引脚颜色
使用Swift添加不带指针的KVO观察器
在Objective-C中,我通常会使用以下内容:
static NSString *kViewTransformChanged = @"view transform changed";// orstatic const void *kViewTransformChanged = &kViewTransformChanged;[clearContentView addObserver:self forKeyPath:@"transform" options:NSKeyValueObservingOptionNew context:&kViewTransformChanged];
我有两种重载方法可供选择,以添加KVO观察者,唯一的区别是上下文参数:
clearContentView.addObserver(observer: NSObject?, forKeyPath: String?, options: NSKeyValueObservingOptions, context: CMutableVoidPointer) clearContentView.addObserver(observer: NSObject?, forKeyPath: String?, options: NSKeyValueObservingOptions, kvoContext: KVOContext)
在Swift不使用指针的情况下,我不确定如何取消引用指针以使用第一种方法。
如果我创建自己的KVOContext常量以与第二种方法一起使用,那么我最终会要求这样做:
let test:KVOContext = KVOContext.fromVoidContext(context: CMutableVoidPointer)
编辑:CMutableVoidPointer和KVOContext之间有什么区别?谁能给我一个例子,说明如何同时使用它们以及何时使用它们?
编辑2:Apple的一名开发人员刚刚将其发布到了论坛上:KVOContext即将消失;现在使用全局引用作为您的上下文。
答案1
小编典典现在,Xcode 6 beta 3中已经没有了KVOContext,您可以执行以下操作。定义一个全局(即不是类属性),如下所示:
let myContext = UnsafePointer<()>()
添加观察者:
observee.addObserver(observer, forKeyPath: …, options: nil, context: myContext)
在观察者中:
override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: [NSObject : AnyObject]!, context: UnsafePointer<()>) { if context == myContext { … } else { super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context) }}
11.6 Swift重写属性观察器
/**
重写 属性观察器
1.只能给非lazy属性的变量存储属性设定属性观察器,不能给计算属性设定属性观察器。
属性观察器的限制:(1)不可以给只读的存储/计算属性,在子类中设定属性观察器,
(因为只读,不会改变嘛)
// 必须在父类中是可读可写的,才可以在子类中重写属性观察器啊。
// 可以重写父类中的计算属性的属性观察器
*/
class Observer {
var storeProperty: Int = 0 {
willSet {
print("storeProperty father will Set")
}
didSet {
print("storeProperty father did Set")
}
}
// 不能给计算属性设定属性观察器
var computeProperty: Int {
get {
return 0
}
set {
print("Do nothing!")
}
}
}
class ChildOfObserver: Observer {
// 可以重写父类中变量存储属性
override var storeProperty: Int {
willSet {
print("storeProperty will Set")
}
didSet {
print("storeProperty did Set")
}
}
// 可以重写父类中的计算属性的属性观察器
override var computeProperty: Int {
willSet {
print("computeProperty will Set")
}
didSet {
print("computeProperty did Set")
}
}
}
let co = ChildOfObserver.init()
co.storeProperty = 10
/**
will Set
father will Set
father did Set
did Set
*/
co.computeProperty = 9
/**
computeProperty will Set
Do nothing!
computeProperty did Set
*/
9.7 Swift属性观察器
/**
属性观察器
观察属性的变化,是指属性被修改时可以调用我们事先写好的代码去额外执行一些操作。
类似于OC中的KVO
存在两种属性观察器
1. willSet 在设置新值的时候调用
2. didSet 在设置新值的之后调用
可以直接为除 lazy属性之外的存储属性添加属性观察器,你不可以直接给类里面的计算属性添加属性观察器,
当然也可以在继承类中为父类的计算属性提供属性观察器(后面再讲)。
*/
struct MyRect {
var origion:(x: Double,y: Double) = (0,0) {
willSet {
// 属性被修改之前
print("will set==\(newValue.x) \(newValue.y)")
}
didSet {
// 属性被修改之后
print("did set==\(oldValue.x) \(oldValue.y)")
}
}
var size: (w: Double,h: Double) = (0,0)
var center: (x: Double,0)
}
var rect = MyRect()
rect.size = (100,100)
@H_772_301@ // 写 内容被修改的时候被调用
rect.origion = (9,9) // setter 写
@H_772_301@ // 这个时候不会调用了
let dd = rect.origion // getter 读
rect.center = (rect.origion.x + rect.size.w / 2,rect.origion.y + rect.size.h / 2)
ios – 使用swift添加Facebook时出现ParseUI错误
func application(application: UIApplication,openURL url: NSURL,sourceApplication: String?,annotation: AnyObject?) -> Bool { return FBSDKApplicationDelegate.sharedInstance.application(application,openURL: url,sourceApplication: sourceApplication,annotation: annotation) }
我错过了什么?
解决方法
func application(application: UIApplication,sourceApplication: String,annotation: AnyObject?) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application(application,annotation: annotation) }
ios – 在swift 2.1中使用MapKit添加不同的引脚颜色
import UIKit import MapKit class MapsViewController: UIViewController,MKMapViewDelegate{ var shops: NSArray? { didSet{ self.loadMaps() } } @IBOutlet weak var map: MKMapView? override func viewDidLoad() { super.viewDidLoad() loadMaps() self.title = "Carte" self.map!.delegate = self } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // dispose of any resources that can be recreated. } func mapView(mapView: MKMapView,viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { // simple and inefficient example let annotationView = MKPinAnnotationView() annotationView.pinTintColor = UIColor.purpleColor() return annotationView } func loadMaps(){ // navigationController?.navigationBar.topItem!.title = "Carte" let shopsArray = self.shops! as NSArray for shop in shopsArray { let location = CLLocationCoordinate2D( latitude: shop["lat"] as! Double,longitude: shop["long"] as! Double ) let annotation = MKPointAnnotation() annotation.coordinate = location annotation.title = shop["name"] as? String annotation.subtitle = shop["addresse"] as? String map?.addAnnotation(annotation) } // add point } /* // MARK: - Navigation // In a storyboard-based application,you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }
解决方法
在自定义类中,添加一个属性,比如pinColor,您可以使用该属性来自定义注释的颜色.
此示例是MKPointAnnotation的子类:
import UIKit import MapKit class ColorPointAnnotation: MKPointAnnotation { var pinColor: UIColor init(pinColor: UIColor) { self.pinColor = pinColor super.init() } }
创建ColorPointAnnotation类型的注释并设置其pinColor:
let annotation = ColorPointAnnotation(pinColor: UIColor.blueColor()) annotation.coordinate = coordinate annotation.title = "title" annotation.subtitle = "subtitle" self.mapView.addAnnotation(annotation)
在viewForAnnotation中,使用pinColor属性设置视图的pinTintColor:
func mapView(mapView: MKMapView,viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { if annotation is MKUserLocation { return nil } let reuseId = "pin" var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView if pinView == nil { pinView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: reuseId) let colorPointAnnotation = annotation as! ColorPointAnnotation pinView?.pinTintColor = colorPointAnnotation.pinColor } else { pinView?.annotation = annotation } return pinView }
今天的关于使用Swift添加不带指针的KVO观察器的分享已经结束,谢谢您的关注,如果想了解更多关于11.6 Swift重写属性观察器、9.7 Swift属性观察器、ios – 使用swift添加Facebook时出现ParseUI错误、ios – 在swift 2.1中使用MapKit添加不同的引脚颜色的相关知识,请在本站进行查询。
本文标签: