Swift

swift的#keyPath

2016-11-03  本文已影响495人  xingou

在oc里,kvc的key和key path都是字符串,那么就难免出现写错的情况。
swift中可以使用#keyPath来指定key和keypath,能够在编译时就检查出错误。
比如

class Person:NSObject{
  var name = ""
  var age = ""
}
print(  #keyPath(Person.name) )
///将会打印name

以前一直不知道有这个东西,一直用的字符串,今天看文档看到了,以后指定keypath都用这种方式了。

附上文档地址:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-ID35
的最后一小节

上一篇 下一篇

猜你喜欢

热点阅读