Pergunta

I am currently trying to make a simple craps app for the iPhone.

In the model I have a method:

-(NSString *)resultsOfRoll:(int)firstRoll :(int)secondRoll
{
    NSString *results = @"";
    NSArray *naturalNumbers = @[@7,@11];
    NSArray *losingNumbers = @[@2, @3, @12];
    NSArray *pointNumbers = @[@4,@5,@6,@8,@9,@10,@11];
    int sum = firstRoll + secondRoll;
    if(sum in naturalNumbers)
    {
       return @"You rolled a natural! You won";
    }

    return results
}

But I am getting an error. I am pretty new to Obj C and I haven't used much enumeration(If that is wrong please correct me) yet. Could someone let me know if I am initializing the loop correctly and how I can check to see if the sum (roll + roll) is in an array? Also, does my method name look correct? I am coming from Java and these method sigs are still a little confusing for me.

Foi útil?

Solução

if(sum in naturalNumbers) obviously isn't valid syntax. You're using something like 'for in' and with an int instead of an object.

What you want is:

if ([naturalNumbers containsObject:@(sum)]) {

Which asks the array if it contains an NSNumber instance containing the sum.

Your method doesn't name the second parameter, which is legal but not encouraged. It would be better as:

-(NSString *)resultsOfRoll:(int)firstRoll secondRoll:(int)secondRoll
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top