gpt4 book ai didi

swift - 关系属性计数的 keyPath

转载 作者:可可西里 更新时间:2023-11-01 00:21:27 29 4
gpt4 key购买 nike

我正在尝试为获取的结果 Controller 创建一个 NSPredicate,该 Controller 应该只观察具有关系(不为零)并且关系计数大于 0 的对象。

我已经了解了如何使用字符串作为谓词的参数:

[NSPredicate predicateWithFormat:@"excludedOccurrences.@count == 0"];

来自:https://stackoverflow.com/a/1195519/4096655

但我试图摆脱文字字符串,所以我想使用关键路径。

例子:

import UIKit
import CoreData

class Animal: NSManagedObject {

@NSManaged var name: String
@NSManaged var friends: NSSet?
}

let keyPath = #keyPath(Animal.friends.count)

导致错误:对成员“计数”的引用不明确

最佳答案

#keyPath 不知道 @count 运算符。但是你可以将它用于“Animal.friends”键路径并附加 @count在谓词格式字符串中:

NSPredicate(format: "%K.@count = 0", #keyPath(Animal.friends))

关于swift - 关系属性计数的 keyPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41467613/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com