NSPredicate - 无法为谓词生成 SQL,我想知道为什么?

发布于 2024-12-08 13:35:16 字数 1135 浏览 0 评论 0原文

我已经[盲目地]解决了我的问题,但没有了解根本原因。但我宁愿从专业人士那里理解一个概念。那么您能否告诉我为什么下面相同的代码一个有效,但另一个无效。

代码 1:不起作用

//Above code omitted...

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"gender == m"]; //NOTICE HERE
[request setPredicate:predicate];   
NSError *error = nil;
self.people = [self.managedObjectContext executeFetchRequest:request error:&error];

 //Below code omitted...

代码 2:起作用

//Above code omitted...

NSString *type = @"m";    
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"gender == %@",type]; //NOTICE HERE
[request setPredicate:predicate];   
NSError *error = nil;
self.people = [self.managedObjectContext executeFetchRequest:request error:&error];

//Below code omitted...


Forgot to tell about what error I got, I got SIGABRT on below line, When I executed Code 1.

 self.people = [self.managedObjectContext executeFetchRequest:request error:&error];

还有一件事,在 GCC 错误中,它无法格式化谓词,因为“gender == m”。


Enlighten me!!

谢谢

I have already solved my problem [Blindly] without understanding root cause. But I would rather understand a concept from a professional. So could you please tell me why below identical code one works but another doesn't.

Code 1: Doesn't work

//Above code omitted...

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"gender == m"]; //NOTICE HERE
[request setPredicate:predicate];   
NSError *error = nil;
self.people = [self.managedObjectContext executeFetchRequest:request error:&error];

 //Below code omitted...

Code 2: Does work

//Above code omitted...

NSString *type = @"m";    
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"gender == %@",type]; //NOTICE HERE
[request setPredicate:predicate];   
NSError *error = nil;
self.people = [self.managedObjectContext executeFetchRequest:request error:&error];

//Below code omitted...


Forgot to tell about what error I got, I got SIGABRT on below line, When I executed Code 1.

 self.people = [self.managedObjectContext executeFetchRequest:request error:&error];

And one more thing, in GCC error was it cannot format predicate because of "gender == m".


Enlighten me!!

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上青苔 2024-12-15 13:35:17

请参阅 谓词编程指南(标题“文字”)。您可以在字符串中使用文字,但必须将它们括在引号中,这样

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"gender == 'm'"];

就可以了。当 predicateWithFormat 添加参数时,它知道它是一个字符串。当你只有 m 在那里时,它不知道如何处理它,因此会出现错误。

See the predicate programming guide (heading "Literals"). You can use literals in your string but you have to enclose them in quotes, so

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"gender == 'm'"];

Would have worked. When predicateWithFormat adds in the argument, it knows it is a string. When you just have m in there, it doesn't know what to do with it, hence the error.

不及他 2024-12-15 13:35:17

Swift 示例

let appDelegate =
        UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext!
        let fetchRequest = NSFetchRequest(entityName:"Words")
        fetchRequest.predicate = NSPredicate(format: "letter == '\(letter)'")
        var error: NSError?
        let fetchedResults =
        managedContext.executeFetchRequest(fetchRequest,
            error: &error) as? [NSManagedObject]
        if let results = fetchedResults {
                       println(results)
        } else {
            println("Could not fetch \(error), \(error!.userInfo)")
        }

example with swift

let appDelegate =
        UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext!
        let fetchRequest = NSFetchRequest(entityName:"Words")
        fetchRequest.predicate = NSPredicate(format: "letter == '\(letter)'")
        var error: NSError?
        let fetchedResults =
        managedContext.executeFetchRequest(fetchRequest,
            error: &error) as? [NSManagedObject]
        if let results = fetchedResults {
                       println(results)
        } else {
            println("Could not fetch \(error), \(error!.userInfo)")
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文