Mouhamad Lamaa
Mouhamad Lamaa

Reputation: 884

use % operator in xcode

i'm trying to make a function that use % operator in objective c my function is:

-(void) image1:(UIImageView *)img1 
        image10:(UIImageView *)img10 
        image100:(UIImageView *)img100 
        score:(NSInteger *)sc
{
        NSInteger* s100,s10,s1;

    s100 = sc % 100;
    s10 = (sc - s100 * 100) % 10;
    s1 = (sc - sc % 10);
    NSLog(@"%d",s1);
}

but i got errors.. can you guide me to some solution

Upvotes: 0

Views: 4752

Answers (2)

Tommy
Tommy

Reputation: 100632

You can't implicitly create objects like that. You want s100, s10, s1 and sc all to be C-style variables, but you've declared s100 and sc as pointers, as though they were going to hold Objective-C objects. I suggest you change NSInteger* to NSInteger in both cases. Not all things starting in NS are objects.

Upvotes: 1

JeremyP
JeremyP

Reputation: 86651

NSInteger is a primitive type. Depending on the environment it is either typedef'd to a 32 bit or 64 bit signed integer.

s100's declaration is

NSInteger* s100

so s100 is a pointer. Taking the modulus of a pointer is wrong. That line should be:

NSInteger s100,s10,s1;

And sc should probably be an NSInteger too. If you do really want to pass a pointer to an NSInteger, you need to dereference it when you do arithmetic with it:

s100 = *sc % 100;
s10 = (*sc - s100 * 100) % 10;
s1 = (*sc - *sc % 10);

Upvotes: 5

Related Questions