有没有一种方法可以使用value(forKeyPath :)访问KVC中的数组元素?

时间:2018-10-10 21:29:37

标签: swift kvc

假设我有一些与KVC兼容的对象,类似于以下内容:

class Person : NSObject {
   var office: Office
   var firstName: String
   var lastName: String
   var reports: [Report]

   init( office: Office, 
         firstName: String,
         lastName: String,
         reports: [Report] ) {

      ...

   }
}

class Office : NSObject {
   var building: String
   var room: Int

   init( building: String, 
         room: Int ) {

      ...

   }

}

class Report : NSObject {
   var title: String
   var contents: String 

   init( title: String, 
         contents: String ) {

      ...

   }
}

然后我创建一个实例,人

let person = Person(
                office: Office( 
                    building: "Main",
                    room: 2 ),
                firstName: "Bob",
                lastName: "Roberts",
                reports: [
                    Report( title: "Today's weather", contents: "..." ),
                    Report( title: "Traffic", contents: "..." ),
                    Report( title: "Stocks", contents: "..." )
                ] )

我可以使用person.value(forKeyPath :)来访问person的属性和嵌套属性,如下所示:

person.value(forKeyPath: "firstName") // "Bob"
person.value(forKeyPath: "office.room" ) // 2

但是,在KVC中,第二种报告的标题是否有办法?

类似

person.value(forKeyPath: "reports[1].title" ) // "Traffic"

2 个答案:

答案 0 :(得分:0)

即使使用结构,Swift的本机KVC也是可能的,不需要ObjC运行时

Device error: glClear:466 GL err 0x502
glClear:466 GL err 0x502
glClear:466 GL err 0x502
glClear:466 GL err 0x502

答案 1 :(得分:0)

这里的澄清是:您不需要将NSObject类更改为struct。甚至一堂课也可以。我认为这是答案。

class Person : NSObject {
    var office: Office!
    @objc var firstName: String!
    var lastName: String!
    @objc var reports: [Report]!

    init( office: Office,
          firstName: String,
          lastName: String,
          reports: [Report] ) {
        super.init()
        self.office = office
        self.firstName = firstName
        self.lastName = lastName
        self.reports = reports
    }
}

class Office : NSObject {
    var building: String!
    var room: Int!

    init( building: String,
          room: Int ) {
        self.building = building
        self.room = room
    }
}

class Report : NSObject {
    var title: String!
    var contents: String!

    init( title: String,
          contents: String ) {
        self.title =  title
        self.contents = contents
    }
}

let person = Person(
    office: Office(
        building: "Main",
        room: 2 ),
    firstName: "Bob",
    lastName: "Roberts",
    reports: [
        Report( title: "Today's weather", contents: "..." ),
        Report( title: "Traffic", contents: "..." ),
        Report( title: "Stocks", contents: "..." )
    ] )


person[keyPath: \Person.reports[1].title]