Core data 用于获取字段所有值的Coredata

Core data 用于获取字段所有值的Coredata,core-data,field,Core Data,Field,我有一个实体(“设置”),我想在一个字段(“状态”)中获取所有值。我使用的是coredata。有人能帮我吗 首先,u可以得到一组值: NSFetchRequest *requestSettings = [[NSFetchRequest alloc] init]; [requestCodesList setEntity:[NSEntityDescription entityForName:@"Setting" inManagedObjectContext:managedObjectContext]

我有一个实体(“设置”),我想在一个字段(“状态”)中获取所有值。我使用的是coredata。有人能帮我吗

首先,u可以得到一组值:

NSFetchRequest *requestSettings = [[NSFetchRequest alloc] init];
[requestCodesList setEntity:[NSEntityDescription entityForName:@"Setting" inManagedObjectContext:managedObjectContext]];
NSArray *setting = [managedObjectContext executeFetchRequest:requestCodesList error:&error];
if (error) NSLog(@"Failed to executeFetchRequest to data store: %@", [error localizedDescription]); 
接下来,u传输NSString中的u阵列:

ComponentsJoinedByString:构造 并返回一个NSString对象,该对象为 插入给定值的结果 元素之间的分隔符 数组

  • (NSString*)组件通过字符串连接:(NSString*)分隔符 代码是:
在您可以在UI中的任何位置使用此选项后(binding,setStringValue e.t.c)

在.h文件中声明此选项: NSMutableArray*eventArray

在.m文件中

- (void)fetchRecords {   
    // Setup the fetch request  
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    // Define our table/entity to use  
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Settings" inManagedObjectContext:managedObjectContext]; 

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

    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    [fetchRequest setEntity:entity];

    [fetchRequest setEntity:entity]; 

    NSError *error; 

    // Get array of results.

    NSMutableArray *theResults = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];

    if (!theResults) {  
        // Handle the error.  
        // This is a serious error and should advise the user to restart the application  
    } 

    // Grab unique neighborhoods through NSSet.

    NSSet *uniqueElements = [NSSet setWithArray:[theResults valueForKey:@"Status"]];
    // Dump NSSet uniques into new array.

    NSMutableArray *sortedResults = [[NSMutableArray alloc] initWithArray:[uniqueElements allObjects]]; 

    for(int i = 0; i < [sortedResults count];i++)
    {
        NSLog(@"%d. %@", i+1, [sortedResults objectAtIndex:i]); 
    }

    // Save our fetched data to an array

    [self setEventArray: sortedResults];
}
-(void)fetchRecords{
//设置获取请求
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
//定义要使用的表/实体
NSEntityDescription*实体=[NSEntityDescription entityForName:@“设置”在managedObjectContext:managedObjectContext中];
NSSortDescriptor*sortDescriptor=[[NSSortDescriptor alloc]initWithKey:@“状态”升序:是];
NSArray*sortDescriptors=[[NSArray alloc]initWithObjects:sortDescriptor,nil];
[FetchRequestSetSortDescriptors:sortDescriptors];
[FetchRequestSetEntity:entity];
[FetchRequestSetEntity:entity];
n错误*错误;
//获取结果数组。
NSMutableArray*theResults=[[managedObjectContext executeFetchRequest:fetchRequest错误:&错误]mutableCopy];
如果(!theResults){
//处理错误。
//这是一个严重错误,应建议用户重新启动应用程序
} 
//通过NSSet获取独特的社区。
NSSet*uniqueElements=[NSSet setWithArray:[theResults valueForKey:@“Status”];
//将NSSet UNIQUE转储到新阵列中。
NSMutableArray*sortedResults=[[NSMutableArray alloc]initWithArray:[uniqueElements allObjects]];
对于(int i=0;i<[sortedResults count];i++)
{
NSLog(@“%d.%@”,i+1,[sortedResults objectAtIndex:i]);
}
//将提取的数据保存到数组中
[self-setEventArray:sortedResults];
}

您是否试图获取所有设置对象中“状态”的所有值的列表?你在乎复制品吗?
- (void)fetchRecords {   
    // Setup the fetch request  
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

    // Define our table/entity to use  
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Settings" inManagedObjectContext:managedObjectContext]; 

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

    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    [fetchRequest setEntity:entity];

    [fetchRequest setEntity:entity]; 

    NSError *error; 

    // Get array of results.

    NSMutableArray *theResults = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];

    if (!theResults) {  
        // Handle the error.  
        // This is a serious error and should advise the user to restart the application  
    } 

    // Grab unique neighborhoods through NSSet.

    NSSet *uniqueElements = [NSSet setWithArray:[theResults valueForKey:@"Status"]];
    // Dump NSSet uniques into new array.

    NSMutableArray *sortedResults = [[NSMutableArray alloc] initWithArray:[uniqueElements allObjects]]; 

    for(int i = 0; i < [sortedResults count];i++)
    {
        NSLog(@"%d. %@", i+1, [sortedResults objectAtIndex:i]); 
    }

    // Save our fetched data to an array

    [self setEventArray: sortedResults];
}