gpt4 book ai didi

iphone - 核心数据 : sorting before saving

转载 作者:行者123 更新时间:2023-11-29 11:11:32 27 4
gpt4 key购买 nike

我正在向一个简单的实体添加一个名称列表(来自数组)。但在保存实体之前,我希望对它们进行排序。请哪位好心人告诉我该怎么做?

我见过如何对一些获取的数据进行排序,但我不知道该怎么做。一个书面的例子将不胜感激。非常感谢。

NSManagedObjectContext *context = [self managedObjectContext];
NSString *myTempString;

for (databaseMakerVC *aName in peoplesNames.theArray) {
myTempString = [[NSString alloc] initWithFormat:@"%@", aName];

peoplesNamesObject *newObject = [NSEntityDescription
insertNewObjectForEntityForName:@"peoplesNamesObject"
inManagedObjectContext:context];
newObject.aName = myTempString;
}


// *** Do the sorting here!? ***


// Save the context
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}

最佳答案

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"peoplesNamesObject" inManagedObjectContext:context];

[fetchRequest setEntity:entity];


NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"aName" ascending:YES];

NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

[fetchRequest setSortDescriptors:sortDescriptors];

NSError *error;


NSArray *array=[context executeFetchRequest:fetchRequest error:&error];

这会起作用

关于iphone - 核心数据 : sorting before saving,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11347244/

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