CoreData predicate: string property length?

后端 未结 3 1092
眼角桃花
眼角桃花 2020-12-20 16:51

Say if I have an entity Fragment, it has an attribute \'text\' which is a string, I want to query the list of Fragment whose text is o

相关标签:
3条回答
  • 2020-12-20 17:13

    You cannot use Objective-C functions like length in a Core Data fetch request. But you can replace it with the "LIKE" operator, which does a simple pattern matching:

    [NSPredicate predicateWithFormat:@"text LIKE %@", @"?????"];
    

    An interesting point is that Core Data does not throw an exception or return with an error, but just ignores the length method, i.e. it just uses the predicate "text = '5' instead. This can be seen by activating Core Data debug output by setting the launch argument

    -com.apple.CoreData.SQLDebug 3
    

    (which is generally a good method to locate Core Data fetch problems).

    0 讨论(0)
  • 2020-12-20 17:17

    I think that it's taking text.lengh as a relationship. Try to set a predicate only with position and then do a loop looking for text.length == 5.

    0 讨论(0)
  • 2020-12-20 17:30

    There is no length attribute for strings in the NSPredicate. Use regex instead. Your predicate should look as follows:

    [NSPredicate predicateWithFormat:@"position == %@ AND text MATCHES %@", pos, @".{5}"];
    
    0 讨论(0)
提交回复
热议问题