0
我有屬性名字和姓氏在NSManagedObject類中。我需要另一個屬性uppercaseFirstLetter這是從這兩個計算。物業uppercaseFirstLetter不能是短暫的,它會使用sectionNamedKeyPath核心數據中計算的持久屬性
[[NSFetchedResultsController alloc] initWithFetchRequest: fetchRequest
managedObjectContext: [DatabaseManager context]
sectionNameKeyPath: @"uppercaseFirstLetter"
cacheName: nil];
和排序描述符
NSSortDescriptor *sortLastNameDescriptor =
[[NSSortDescriptor alloc] initWithKey: @"uppercaseFirstLetter"
ascending: YES
selector: @selector(localizedStandardCompare:)];
這是計算uppercaseFirstLetter代碼:
NSString *aString = nil;
NSString *lastName = [self valueForKey:@"firstName"];
NSString *firstName = [self valueForKey:@"lastName"];
if (lastName.length) {
aString = [lastName uppercaseString];
} else if (firstName.length) {
aString = [firstName uppercaseString];
} else {
aString = @"#";
}
NSString *stringToReturn = [aString substringToIndex:1];
我如何需要創建uppercaseFirstLetter以及代碼中的更改應該怎麼做才能達到這個結果?
你看過添加自定義setter方法嗎?或者'NSManagedObject'上的其他方法可以提供什麼幫助? – Wain
相關:http://stackoverflow.com/questions/4874193/core-data-willsave-method – Wain