Domanda

In iOS 7 Sprite Kit

    NSMutableArray *temp = [[NSMutableArray alloc] init];
    int x = [temp count];
    NSLog(@"%02d", x);

This simple example produces this warning

Implicit conversion loses integer precision: 'NSUInteger' (aka 'unsigned long') to 'int'

In a standard app project, the exact same code does not produce this warning.

Its not a big issue, and I can work around it, with this

    NSMutableArray *temp = [[NSMutableArray alloc] init];
    NSUInteger x = [temp count];
    NSLog(@"%02lu", x);

Just wanna know why.

Thanks

È stato utile?

Soluzione

There can be a problem when you cast an unsigned type to an integer type. Specifically if the unsigned type's value is higher than INT_MAX / 2 the integer value will become negative.

It is generally a good idea to have this warning enabled, and to fix any such warnings. For example, this is one case where the reverse is problematic:

if (NSUInteger i = 100; i < 0; i--)
{
    // do stuff
}

Notice the problem?

Rule of thumb: always care about your integer's signededness and never ignore signed/unsigned mismatch warnings.

That the warning only appears in Sprite Kit may simply have to do with an updated respectively it being a different project template.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top