This code is crashing my app:
Key is a core data object that has two NSString properties, called "shortcut" and "character". I would like to check, when a textView is changed, whether the text contains the shortcut for any of the keys, and if it does, to replace the shortcut with the character.
Here's what's logged:
Suggestions as to what I'm doing wrong?
Edit: I feel like it's my predicate that isn't working... I've tried changing it to this:
but it still doesn't work and I get the same log output...
2X Edit: After thinking of the output for a bit... it's occurring to me that my idea of "CONTAINS" and "IN" are not the same as what's actually happening...
for CONTAINS it's looking at the left side object as an array of strings and seeing if any of the strings are equal to the shortcut...
for IN it's looking at the right side object as an array of strings and seeing if any of the strings are equal to the shortcut...
thus these just won't work for what I want.
3X Edit: Alright, I've managed to get this to work by using:
I'd rather if it was more intelligent and actually only did searched for shortcuts containing the character that was just typed, but, eh, I guess this is good enough.
Code:
- (void)textViewDidChange:(UITextView *)textView
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"Key" inManagedObjectContext:managedObjectContext];
request.predicate = [NSPredicate predicateWithFormat:@"shortcut IN %@", typeView.text];
NSError *error;
[color="RED"]NSArray *keys = [managedObjectContext executeFetchRequest:request error:&error];[/color]
for (Key *key in keys)
{
NSLog(@"Checking if %@ is in %@", key.shortcut, typeView.text);
if ([typeView.text rangeOfString:key.shortcut].location != NSNotFound)
{
typeView.text = [typeView.text stringByReplacingOccurrencesOfString:key.shortcut withString:key.character];
}
}
}
Key is a core data object that has two NSString properties, called "shortcut" and "character". I would like to check, when a textView is changed, whether the text contains the shortcut for any of the keys, and if it does, to replace the shortcut with the character.
Here's what's logged:
Code:
2011-11-21 13:57:35.780 QuickClip[2123:15803] -[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x8a88c40
2011-11-21 13:57:35.783 QuickClip[2123:15803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x8a88c40'
*** First throw call stack:
(0x1e71052 0x2002d0a 0x1e72ced 0x1dd7f00 0x1dd7ce2 0x16c231c 0x16c1a68 0x16b458f 0x16b3adc 0x16ae4c6 0x16ae1ae 0x16adf60 0x16add33 0x16ad389 0x16ace20 0x16ac1cd 0x16aa06d 0xac5f 0xaa51 0x64ea 0x183fbf 0x18421b 0x1850f1 0xf3fec 0xf9572 0xf372b 0xe2bc2 0xe30b8 0x2d8a 0xba9d6 0xbb8a6 0xca743 0xcb1f8 0xbeaa9 0x1d5bfa9 0x1e451c5 0x1daa022 0x1da890a 0x1da7db4 0x1da7ccb 0xbb2a7 0xbca9b 0x2aed 0x2a65)
terminate called throwing an exception(gdb)
Suggestions as to what I'm doing wrong?
Edit: I feel like it's my predicate that isn't working... I've tried changing it to this:
Code:
request.predicate = [NSPredicate predicateWithFormat:@"%@ CONTAINS shortcut", typeView.text];
2X Edit: After thinking of the output for a bit... it's occurring to me that my idea of "CONTAINS" and "IN" are not the same as what's actually happening...
for CONTAINS it's looking at the left side object as an array of strings and seeing if any of the strings are equal to the shortcut...
for IN it's looking at the right side object as an array of strings and seeing if any of the strings are equal to the shortcut...
thus these just won't work for what I want.
3X Edit: Alright, I've managed to get this to work by using:
Code:
request.predicate = [NSPredicate predicateWithFormat:@"shortcut LIKE %@", @"*"];
I'd rather if it was more intelligent and actually only did searched for shortcuts containing the character that was just typed, but, eh, I guess this is good enough.
Last edited: