2016-08-12 3 views
-2

Ich bin neu in der Programmierung von Objektiven C.App stürzt immer wieder ab und erstellt ein PygLatin-Spiel - Objective C

Ich habe eine PygLatin-Anwendung erstellt, aber es stürzt immer wieder ab. Sag mir, wenn du Probleme mit meinem Code entdeckt hast.

App beenden aufgrund nicht abgefangene Ausnahme 'NSRangeException', Grund: ' - [__ NSArrayM objectAtIndex:]: Index 5 über Grenzen [0 .. 4]' ersten Wurf Call-Stack:

würde mich freuen, alle Hilfe, die ich bekommen kann, danke! :)

@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 
+1

Bitte sehen http://www.raywenderlich.com/10209/my-app-crashed-now -was-Teil-1, um zu lernen, wie man einen Absturz debuggt. – rmaddy

Antwort

1

setzen
self.subsequentCharCount++;
am Ende der while-Schleife bedeutet unter
[self print: completedWord];

+0

Ich tat, was Sie sagten, aber mein resultLabel zeigt% @ nur für Wörter an, die mit Vokalen beginnen, und für einige Wörter stürzt es automatisch ab, indem es einen ** Index 10 außerhalb der Grenzen angibt [0 .. 9 ** variiert zwischen den Wörtern. –

+0

in Ihrer Druckmethode self.resultLabel.text = @ "% @", printWords –

+0

direkt zuweisen printWords self.resultLabel.text = printWords; –