我可以在nift参数中使用Swift中的NSPredicate吗? [英] Can I use an NSPredicate in Swift with a nil argument?
问题描述
我试图将使用Core Data的项目从Objective-C转换为Swift。
数据模型的结构使我有一个主文件夹包含其他文件夹 - 这些文件夹也可以通过parentFolder关系包含其他文件夹。
目前,我在Objective-C中检索主文件夹它找到唯一没有parentFolder的文件夹,并按预期工作):
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:夹];
request.predicate = [NSPredicate predicateWithFormat:@parentFolder ==%@,nil];
在转换为Swift时,我想做同样的事情:
let request = NSFetchRequest(entityName:Folder)
request.predicate = NSPredicate(format:parentFolder ==%@,nil )
...但是编译器在调用中抱怨Missing argument label'argumentArray:我似乎很困惑它认为我需要使用NSPredicate(format:argumentArray :)方法代替...)
有正确的方法来做到这一点Swift?
它看起来像是(与当前Xcode 6 beta 3版本)一般很难
工作方式: p>
let predicate = NSPredicate(format:parentFolder ==%@,0)
print(predicate)
// Output:parentFolder == nil
但最简单的解决方案可以简单地写谓语为
NSPredicate(format:parentFolder == nil)
I'm trying to convert a project that uses Core Data from Objective-C to Swift.
The data model is structured so that I have one master folder which contains other folders - and those folders can also contain other folders, via a "parentFolder" relationship.
Currently, I do this in Objective-C to retrieve the master folder (it finds the only folder without a "parentFolder", and works as expected):
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:"Folder"];
request.predicate = [NSPredicate predicateWithFormat:@"parentFolder == %@", nil];
In converting to Swift, I'd like to do the same thing:
let request = NSFetchRequest(entityName: "Folder")
request.predicate = NSPredicate(format: "parentFolder == %@", nil)
... but the compiler complains with "Missing argument label 'argumentArray:' in call. (I seem to be confusing it into thinking I need to use the NSPredicate(format: argumentArray:) method instead...)
Is there a correct way to do this in Swift?
It looks as if it is (with the current Xcode 6 beta 3 release) generally difficult
to pass nil
in a variable argument list.
This seems to work:
let predicate = NSPredicate(format: "parentFolder == %@", 0)
print(predicate)
// Output: parentFolder == nil
But the easiest solution would be to simply write the predicate as
NSPredicate(format: "parentFolder == nil")
这篇关于我可以在nift参数中使用Swift中的NSPredicate吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!