Array Issue in Objective C Xcode

2019-09-12 15:46发布

问题:

I have a problem with my algorithm for calculating a sore. The user enters a word into the UITextField, and if the word matches a string in the array (@"The Word") the int 'score' will be added by 1.

Then the int score is set as a label as the user gets a word right. (DISPLAYING THE SCORE)

THE PROBLEM, a user can just keep on entering the same word over and over again and the score will keep going up by one. IS there a command for knowing if a word has already been entered, so you can only use the word once.

The Code

 NSArray *scoreArray1 = [NSArray arrayWithObjects:
@"Word 1", @"Word 2", @"Word 3", nil];

NSString *inputtwo =_EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in scoreArray1) {
    if([inputtwo isEqual:possible] ) {
        isItright = YES;
        break;
    }
}

if(isItright) {

    static int myInt = 0;
    myInt++;
    NSString *score = [NSString stringWithFormat:@"%d", myInt];
    [_scorelabel setText:score];

}

UPDATE!!!!!!

    NSArray *scoreArray1 = [NSArray arrayWithObjects:
@"Alan Shearer", @"Shearer", @"Andrew Cole", @"Andy Cole", @"Cole", @"Thierry Henry", @"Henry", @"Robbie Fowler", @"Fowler", @"Frank Lampard", @"Lampard", @"Michael Owen", @"Owen", nil];

NSSet *set2 = [NSSet setWithArray:scoreArray1];



NSString *inputtwo =_EnterNameText.text;
BOOL isItright = NO;
for(NSString *possible in set2) {
    if([inputtwo isEqual:possible] ) {
        isItright = YES;
        break;
    }
}

if(isItright) {

    static int myInt = 0;
    myInt++;
    NSString *score = [NSString stringWithFormat:@"%d", myInt];
    [_scorelabel setText:score];

}

HOWEVER NOW THE APP DOES NOT WORK, IT CRASHES, any suggestions?

回答1:

Why don't you keep a second Array where you store the given (correct) answers. Whit this you can just do a contains inside your if....problem solved.

a second option is not to put string in your array but "Answer" Objects, that have a field that you can flag as already used.



回答2:

You could just create an NSMutableSet and put a copy of the word into there whenever one is entered. Then you just need to check if the word exists in the set before incrementing the score.

I'm suggesting a set because it uses hashed access, so lookups are fast. Also, if you add the same string more than once, the set will still only have one reference to the string.



回答3:

Actually, if you have an array of "legal" words, the way to go is to simply remove each word as it's called out, until the array gets to be zero entries long.

NSMutableArray* scoreArrayCopy = [NSMutableArray arrayWithArray:scoreArray];
int originalCount = scoreArrayCopy.count;
...
while (scoreArrayCopy.count > 0) {
   NSString* guess = <get next guess>;
   [scoreArrayCopy removeObject:guess];
   score = originalCount - scoreArrayCopy.count;
}

(If you have a lot of words things would be more efficient if you used an NSMutableSet instead of an NSMutableArray, but the logic would be the same.)