我有兩個實體Device和MDM。設備有兩個屬性,asset_tag和location。 MDM有兩個屬性,asset_tag和os。我正在嘗試爲每個asset_tag設備獲取asset_tag,os和位置。我有Xcode中創建我的子類:核心數據Swift 3關係
extension Device {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Device> {
return NSFetchRequest<Device>(entityName: "Device")
}
@NSManaged public var asset_tag: String?
@NSManaged public var location: String?
@NSManaged public var devices: MDM?
}
extension MDM {
@nonobjc public class func fetchRequest() -> NSFetchRequest<MDM> {
return NSFetchRequest<MDM>(entityName: "MDM")
}
@NSManaged public var asset_tag: String?
@NSManaged public var os: String?
@NSManaged public var mdms: Device?
}
我取的請求如下:
var request = NSFetchRequest<NSFetchRequestResult>()
request = Device.fetchRequest()
request.returnsObjectsAsFaults = false
let results = try context.fetch(request) as! [Device]
不知道如何得到這樣的device.mdms.os
合作,獲取特定設備的操作系統。
正如另一個問題所述,您的命名很混亂,請考慮慣例。一個一對一的關係是'device:Device'(單數),一對多關係是'devices:NSSet'(複數)。 – vadian
我扭轉了我的關係,並讓Xcode重拍我的子類,所以現在看起來像這樣:'@NSManaged public var devices:Device' – Martheli
'devices'(複數)意味着**很多**設備。相應的類型是'NSSet'。否則(**一個**設備,單數)使用'device'(無尾隨's')。 – vadian