Search code examples
ioscore-datamagicalrecord

Get all records from core data and get string with one attribute from every record (MagicalRecord)


I'm try to get all the elements that the user saved inside the app(this could be easy), get one attribute from every record and have these attributes listed inside a string(that's the problem). How can I do it? I wrote something but I don't think it is the right way. help me please!

 -(NSString*)getAllRecords
{
    NSArray* arra = [NewItem MR_findAllSortedBy:@"data" ascending:YES];
    NewItem* ctn = arra.;// I have not any idea

    return [NSString stringWithFormat:@"%@", [arra componentsJoinedByString:@", "]];

}

Solution

  • If I understand your problem correctly, this should do what you want:

    NSArray *elements = [NewItem MR_findAllSortedBy:@"data" ascending:YES];
    NSArray *attributes = [elements valueForKey:@"attributeName"];
    return [NSString stringWithFormat:@"%@", [attributes componentsJoinedByString:@", "]];
    

    Applying valueForKey:key to an array returns an array containing the results of invoking valueForKey:key on each of the array's objects.