Created
November 2, 2009 23:58
-
-
Save masakih/224639 to your computer and use it in GitHub Desktop.
NSTokenFieldの補完データをコアデータで管理する。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| コアデータとNSTokenField | |
| NSTokenFieldの補完データをコアデータで管理する。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma mark#### NSTokenField Delegate #### | |
| - (NSArray *)tokenField:(NSTokenField *)tokenField | |
| completionsForSubstring:(NSString *)substring | |
| indexOfToken:(NSInteger)tokenIndex | |
| indexOfSelectedItem:(NSInteger *)selectedIndex | |
| { | |
| NSManagedObjectContext *moc = [appDelegate managedObjectContext]; | |
| NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@", substring]; | |
| NSEntityDescription *entry = [NSEntityDescription entityForName:@"VoiceActor" | |
| inManagedObjectContext:moc]; | |
| NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease]; | |
| [fetch setEntity:entry]; | |
| [fetch setPredicate:predicate]; | |
| NSError *error = nil; | |
| NSArray *objects = [moc executeFetchRequest:fetch error:&error]; | |
| if(!objects) { | |
| if(error) { | |
| NSLog(@"fail fetch reason -> %@", error); | |
| } | |
| } | |
| NSMutableArray *result = [NSMutableArray array]; | |
| for(id obj in objects) { | |
| [result addObject:[obj valueForKey:@"name"]]; | |
| } | |
| return result; | |
| } | |
| - (void)registerVoiceActor:(NSTokenField *)tokenField | |
| { | |
| id array = [tokenField objectValue]; | |
| if(![array isKindOfClass:[NSArray class]]) return; | |
| NSManagedObjectContext *moc = [appDelegate managedObjectContext]; | |
| NSEntityDescription *entry = [NSEntityDescription entityForName:@"VoiceActor" | |
| inManagedObjectContext:moc]; | |
| NSFetchRequest *fetch = [[[NSFetchRequest alloc] init] autorelease]; | |
| [fetch setEntity:entry]; | |
| for(id token in array) { | |
| NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] %@", token]; | |
| [fetch setPredicate:predicate]; | |
| NSError *error = nil; | |
| NSUInteger count = [moc countForFetchRequest:fetch error:&error]; | |
| if(error) { | |
| NSLog(@"fail fetch reason -> %@", error); | |
| continue; | |
| } | |
| if(count == 0) { | |
| id obj = [NSEntityDescription insertNewObjectForEntityForName:@"VoiceActor" inManagedObjectContext:moc]; | |
| [obj setValue:token forKey:@"name"]; | |
| } | |
| } | |
| } | |
| - (BOOL)control:(id)control textShouldEndEditing:(NSText *)fieldEditor | |
| { | |
| if([control tag] == 2000) { | |
| [self registerVoiceActor:control]; | |
| } | |
| return YES; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment