Files
swift-mirror/test/Interpreter/SDK/objc_keypath.swift
Doug Gregor 9f0cec4984 SE-0062: Implement #keyPath expression.
Implement the Objective-C #keyPath expression, which maps a sequence
of @objc property accesses to a key-path suitable for use with
Cocoa[Touch]. The implementation handles @objc properties of types
that are either @objc or can be bridged to Objective-C, including the
collections that work with key-value coding (Array/NSArray,
Dictionary/NSDictionary, Set/NSSet).

Still to come: code completion support and Fix-Its to migrate string
literal keypaths to #keyPath.

Implements the bulk of SR-1237 / rdar://problem/25710611.
2016-05-18 23:30:15 -07:00

71 lines
1.7 KiB
Swift

// RUN: %target-run-simple-swift | FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop
import Foundation
class Person : NSObject {
@objc(firstNameString) var firstName: String
var lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
override var description: String {
return "\(lastName), \(firstName)"
}
}
class Band : NSObject {
var members: [Person] = []
}
class RecordLabel : NSObject {
var bands: [String : Band] = [:]
}
let band = Band()
band.members = [Person(firstName: "John", lastName: "Lennon"),
Person(firstName: "Paul", lastName: "McCartney"),
Person(firstName: "George", lastName: "Harrison"),
Person(firstName: "Ringo", lastName: "Star")]
// CHECK: ===Members===
// CHECK-NEXT: (
// CHECK-NEXT: "Lennon, John",
// CHECK-NEXT "McCartney, Paul",
// CHECK-NEXT "Harrison, George",
// CHECK-NEXT "Star, Ringo"
// CHECK-NEXT)
print("===Members===")
print(band.value(forKeyPath: #keyPath(Band.members))!.description)
// CHECK: ===First Names===
// CHECK-NEXT: (
// CHECK-NEXT: John,
// CHECK-NEXT: Paul,
// CHECK-NEXT: George,
// CHECK-NEXT: Ringo
// CHECK-NEXT:)
print("===First Names===")
print(band.value(forKeyPath: #keyPath(Band.members.firstName))!.description)
let recordLabel = RecordLabel()
recordLabel.bands["Beatles"] = band
// CHECK: ===Last Names===
// CHECK-NEXT: (
// CHECK-NEXT: Lennon,
// CHECK-NEXT: McCartney,
// CHECK-NEXT: Harrison,
// CHECK-NEXT: Star
// CHECK-NEXT: )
print("===Last Names===")
print(recordLabel.value(forKeyPath: #keyPath(RecordLabel.bands.Beatles.members.lastName))!.description)
// CHECK: DONE
print("DONE")