我是客观C编程的新手。
我创建了一个PygLatin应用程序,但它一直在崩溃。如果您发现我的代码存在任何问题,请告诉我。
由于未捕获的异常终止应用' NSRangeException',原因:' - [__ NSArrayM objectAtIndex:]:index 5超越界限[0 .. 4]' 首先抛出调用堆栈:
非常感谢我能得到的所有帮助! :)
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.userInputTextField.delegate = self;
self.userInputTextField.text = @"";
}
-(void)print: (NSString*)printWords {
self.resultLabel.text = @"%@", printWords;
}
-(void)charsplitter: (NSArray*)charArraysplitter {
//word selection loop
int indexCount = 0;
for(indexCount = 0; indexCount < charArraysplitter.count; indexCount++)
{
self.wordsToBeSplit = charArraysplitter[indexCount];
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[self.wordsToBeSplit length]];
[self.wordsToBeSplit enumerateSubstringsInRange:NSMakeRange(0, self.wordsToBeSplit.length)
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[characters addObject:substring];
}];
[self vowelsComparator:characters];
}
}
-(void)vowelsComparator: (NSArray*)comparator {
self.myVowels = [NSArray arrayWithObjects: @"a", @"e", @"i", @"o",@"u", nil];
int charIndex;
self.subsequentCharCount = 0;
for(charIndex = 0; charIndex < comparator.count; charIndex++)
//loops to find if first character is a vowel
if([self.myVowels containsObject:comparator[0]]){
[self print: self.userInputTextField]; NSLog(@"working fine:");
}else{
//loops to find other vowels
while (self.subsequentCharCount < comparator.count){
self.subsequentCharCount++;
if ([self.myVowels containsObject:comparator[self.subsequentCharCount]]){
//moving the consonants behing the vowels
NSLog(@"working fine:");
NSString *combinedWords = [[self.wordsToBeSplit substringFromIndex:self.subsequentCharCount]stringByAppendingString:[self.wordsToBeSplit substringToIndex:self.subsequentCharCount]];
NSString *completedWord = [combinedWords stringByAppendingString:@"ay"];
[self print: completedWord];
}
};
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];//resigns the keyboard every time return button is pressed
return YES;
}
- (IBAction)pygButton:(id)sender
{
self.inputText = [self.userInputTextField.text lowercaseString];//user input is lowercase
NSArray *wordsArray = [self.inputText componentsSeparatedByString: @" "]; //separate words into arrays.
[self charsplitter: wordsArray];
}
@end
答案 0 :(得分:1)
放
self.subsequentCharCount++;
在while循环结束时表示在下面
[self print: completedWord];