SORU
11 Temmuz 2009, CUMARTESİ


Bölümler ilk harfi tarafından oluşturulan NSFetchedResultsController bir dize

İPhone Temel Veri öğrenme. Çekirdek Veri bölümleri ile tablo görünümü yerleştirmek için birkaç örnek var. CoreDataBooks örnek bölümleri kullanır, ama modeli içinde tam dizeleri elde ediyorlar. Son bir ismin ilk harfine göre bölümlere Çekirdek Veri tablosu, la Adres Defterini düzenlemek istiyorum.

Gidip başka bir öznitelik bölüm bölüm olarak hareket edebilmek için, yani tek bir harf, her bir kişi için yaratabilirim, ama bu kludgy görünüyor.

Burada başladım işte ... numara sectionNameKeyPath kandırıyor gibi görünüyor:

- (NSFetchedResultsController *)fetchedResultsController {
//.........SOME STUFF DELETED
    // Edit the sort key as appropriate.
    NSSortDescriptor *orderDescriptor = [[NSSortDescriptor alloc] initWithKey:@"personName" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:orderDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = 
            [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
            managedObjectContext:managedObjectContext 
            sectionNameKeyPath:@"personName" cacheName:@"Root"];
//....
}

CEVAP
11 Temmuz 2009, CUMARTESİ


Dave DeLong yaklaşımı iyi, en azından benim durumumda, istediğin iki şey ihmal sürece. Benim için çalışıyor:

  • Yeni isteğe bağlı bir dize öznitelik Ekle işletmenin adı "" (ya da bir şey lastNameİnitial bu yönde).

    Bu özellik geçici olun. Bu Çekirdek Veri dert olmayacak anlamına gelir verilerinizi içine dosya kaydediliyor. Bu özellik sadece bellek yok olacak bu gerekiyor.

    Sınıf bu dosyaları oluşturmak varlık.

    Bunun için bir ayarlayıcı merak etme özellik. (Bu, bu alıcı oluşturun IMHO yarısı magic)

< / ^ hr .

// THIS ATTRIBUTE GETTER GOES IN YOUR OBJECT MODEL
- (NSString *) committeeNameInitial {
    [self willAccessValueForKey:@"committeeNameInitial"];
    NSString * initial = [[self committeeName] substringToIndex:1];
    [self didAccessValueForKey:@"committeeNameInitial"];
    return initial;
}


// THIS GOES IN YOUR fetchedResultsController: METHOD
// Edit the sort key as appropriate.
NSSortDescriptor *nameInitialSortOrder = [[NSSortDescriptor alloc] 
        initWithKey:@"committeeName" ascending:YES];

[fetchRequest setSortDescriptors:[NSArray arrayWithObject:nameInitialSortOrder]];

NSFetchedResultsController *aFetchedResultsController = 
        [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
        managedObjectContext:managedObjectContext 
        sectionNameKeyPath:@"committeeNameInitial" cacheName:@"Root"];

< / ^ hr .

Daha ÖNCE: harf Dave'in ilk adımları geçersiz bir argüman ile setPropertiesToFetch üzerine ölür sorunları oluşturulan özel durum Şu. Aşağıda Kodu ve hata ayıklama bilgilerini geldi:

NSDictionary * entityProperties = [entity propertiesByName];
NSPropertyDescription * nameInitialProperty = [entityProperties objectForKey:@"committeeNameInitial"];
NSArray * tempPropertyArray = [NSArray arrayWithObject:nameInitialProperty];

//  NSARRAY * tempPropertyArray RETURNS:
//    <CFArray 0xf54090 [0x30307a00]>{type = immutable, count = 1, values = (
//    0 : (<NSAttributeDescription: 0xf2df80>), 
//    name committeeNameInitial, isOptional 1, isTransient 1,
//    entity CommitteeObj, renamingIdentifier committeeNameInitial, 
//    validation predicates (), warnings (), versionHashModifier (null), 
//    attributeType 700 , attributeValueClassName NSString, defaultValue (null)
//    )}

//  NSInvalidArgumentException AT THIS LINE vvvv
[fetchRequest setPropertiesToFetch:tempPropertyArray];

//  *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
//    reason: 'Invalid property (<NSAttributeDescription: 0xf2dfb0>), 
//    name committeeNameInitial, isOptional 1, isTransient 1, entity CommitteeObj, 
//    renamingIdentifier committeeNameInitial, 
//    validation predicates (), warnings (), 
//    versionHashModifier (null), 
//    attributeType 700 , attributeValueClassName NSString, 
//    defaultValue (null) passed to setPropertiesToFetch: (property is transient)'

[fetchRequest setReturnsDistinctResults:YES];

NSSortDescriptor * nameInitialSortOrder = [[[NSSortDescriptor alloc]
    initWithKey:@"committeeNameInitial" ascending:YES] autorelease];

[fetchRequest setSortDescriptors:[NSArray arrayWithObject:nameInitialSortOrder]];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] 
    initWithFetchRequest:fetchRequest 
    managedObjectContext:managedObjectContext 
    sectionNameKeyPath:@"committeeNameInitial" cacheName:@"Root"];

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • karneson

    karneson

    23 Temmuz 2006
  • NewsyTech

    NewsyTech

    2 AĞUSTOS 2010
  • thenewboston

    thenewboston

    4 ŞUBAT 2008