美文网首页Swift
swift的#keyPath

swift的#keyPath

作者: xingou | 来源:发表于2016-11-03 23:51 被阅读495次

在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
的最后一小节

相关文章

网友评论

    本文标题:swift的#keyPath

    本文链接:https://www.haomeiwen.com/subject/ncpiuttx.html