I am developing a tweak for iOS using Logos/Theos.
I need to store the last two characters the user has typed in at all times.
My code for this is:
static NSString *lastTwoChars = [[NSString alloc] init];
...
if (lastTwoChars.length < 2) {
lastTwoChars = [lastTwoChars stringByAppendingString:text];
} else {
lastTwoChars = [lastTwoChars stringByAppendingString:text];
lastTwoChars = [lastTwoChars substringFromIndex:1];
}
...
(In this scope, text
is the last character they entered in the keyboard)
This code works fine, until I press a third character. After a very long time of trial and error, I found that it is because the variable lastTwoChars
is getting released, and becoming a different object.
I cannot figure out how to keep the object from being deallocated and changing to another object. I've tried to retain
the variable, but either I'm doing it wrong, or that won't work.
Any help would be greatly appreciated.