问题描述
我以前从未使用过Swift4,也不知道如何在其中使用KVC.
我尝试用Dictionary创建模型,这里是代码:
I never used Swift4 before, and dont know how to use KVC in it.
I try to create model with Dictionary, here the code:
class Person : NSObject {
var name: String = ""
var age: Int = 0
init(dict: [String : Any]) {
super.init()
self.setValuesForKeys(dict)
}
}
let dict: [String : Any] = ["name" : "Leon", "age" : 18]
let p = Person(dict: dict)
print(p.name, p.age)
我有两个问题:
1.为什么不使用AnyObject
? "Leon"
和18
推断为String
和Int
,是否在KVC中使用?
2. @objc var name: String = ""
,这种形式有效,但是我听不懂.
感谢所有帮助.
There I get two question:
1. Why dont I using AnyObject
? "Leon"
and18
was infer to String
and Int
, does it using in KVC?
2. @objc var name: String = ""
, this form is worked, but I can not understand it.
Thanks for all helps.
推荐答案
要在Swift 4中为属性实现KVC支持,您需要做两件事:
To implement KVC support for a property in Swift 4, you need two things:
-
由于KVC的当前实现是用Objective-C编写的,因此需要在属性上使用
@objc
批注,以便Objective-C可以看到它.这也意味着该属性的类型需要与Objective-C兼容.
Since the current implementation of KVC is written in Objective-C, you need the
@objc
annotation on your property so that Objective-C can see it. This also means that the property's type needs to be compatible with Objective-C.
除了将属性暴露给Objective-C外,您还需要设置通知,以便在属性更改时通知观察者.有三种方法可以做到这一点:
In addition to exposing the property to Objective-C, you will need to set up your notifications in order for observers to be notified when the property changes. There are three ways to do this:
对于存储的属性,最简单的操作是添加dynamic
关键字,如下所示:
For stored properties, the easiest thing to do is to add the dynamic
keyword like so:
@objc dynamic var foo: String
这将使Cocoa能够使用Objective-C魔术来自动为您生成所需的通知,这通常是您想要的.但是,如果需要更好的控制,还可以手动编写通知代码:
This will allow Cocoa to use Objective-C magic to automagically generate the needed notifications for you, and is usually what you want. However, if you need finer control, you can also write the notification code manually:
@objc private static let automaticallyNotifiesObserversOfFoo = false
@objc var foo: String {
willSet { self.willChangeValue(for: \.foo) }
didSet { self.didChangeValue(for: \.foo) }
}
此处的automaticallyNotifiesObserversOf<property name>
属性向KVC/KVO系统表示我们正在自行处理通知,可可不应该尝试为我们生成通知.
The automaticallyNotifiesObserversOf<property name>
property is there to signify to the KVC/KVO system that we are handling the notifications ourselves and that Cocoa shouldn't try to generate them for us.
最后,如果您的属性未存储,而是依赖于某些其他属性,则需要实现如下的keyPathsForValuesAffecting<your property name here>
方法:
Finally, if your property is not stored, but rather depends on some other property or properties, you need to implement a keyPathsForValuesAffecting<your property name here>
method like so:
@objc dynamic var foo: Int
@objc dynamic var bar: Int
@objc private static let keyPathsForValuesAffectingBaz: Set<String> = [
#keyPath(foo), #keyPath(bar)
]
@objc var baz: Int { return self.foo + self.bar }
在上面的示例中,当foo
的值或bar
的值更改时,将通知baz
属性的观察者.
In the example above, an observer of the baz
property will be notified when the value for foo
or the value for bar
changes.
这篇关于如何在Swfit 4.0中使用键值编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!