【问题标题】:NSFetchResultController i am using predicate with attribute in array?NSFetchedResultsController 我在数组中使用带有属性的谓词?
【发布时间】:2012-12-11 14:48:54
【问题描述】:

NSFetchRsultController 我正在使用:

[NSPredicate predicateWithFormat:@"type in %@", myArray];

我想要数据按照数组中数据的顺序排列,但是NSSortDescriptorNSFetchResultController的必填项,那么NSSortDescriptor我得用什么呢?

【问题讨论】:

    标签: iphone ios ipad core-data


    【解决方案1】:

    您可以使用sortDescriptors 将数据按数组顺序排列。

    排序描述符指定发出获取请求时返回的对象应如何排序——例如按姓氏然后按名字。排序描述符按照它们在 sortDescriptors 数组中出现的顺序应用(以最低数组索引优先顺序)。

    代码示例:

    ...
    NSMutableArray * sortDescriptors = [[NSMutableArray alloc] initWithCapacity:[myArray count]];
    for (NSString * key in myArray)
      [sortDescriptors addObject:[[NSSortDescriptor alloc] initWithKey:key ascending:NO]];
    
    [fetchRequest setSortDescriptors:sortDescriptors];
    [sortDescriptors release];
    ...
    

    【讨论】:

    • 但是 Array 是类型值,我如何将它用作 SortDesriptor 键?
    • @user1800529 类型值?你的意思是像int这样的东西吗?您可以使用[NSValue valueWithInt:aType] 存储到数组中。
    【解决方案2】:

    用户1800529,

    您似乎希望将顺序顺序的东西插入到您的数据库中。 Core Data 不保证该顺序。因此,您需要选择要排序的东西。我使用创建时间,这在我的大多数实体中都有,但您可以轻松使用其他属性。

    安德鲁

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 2011-05-20
      • 2011-01-12
      • 2019-12-06
      相关资源
      最近更新 更多