Objective - C implicit conversion traci dokładność całkowitą "nsuinteger" (aka "unsigned long") na Ostrzeżenie "int"

Pracuję nad kilkoma ćwiczeniami i dostałem Ostrzeżenie, które stwierdza:

Konwersja Implicit traci dokładność całkowitą: "NSUInteger" (aka "unsigned long") na " int "

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool {

        NSArray *myColors;

        int i;
        int count;

        myColors = @[@"Red", @"Green", @"Blue", @"Yellow"];

        count = myColors.count; //  <<< issue warning here

        for (i = 0; i < count; i++)

        NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]);
    }

    return 0;
}

Zrzut ekranu

Author: Cœur, 2013-06-04

4 answers

Metoda count NSArray zwraca NSUInteger, a na 64-bitowej platformie OS X

  • NSUInteger definiuje się jako unsigned long, oraz
  • unsigned long jest 64-bitową niepodpisaną liczbą całkowitą.
  • int jest 32-bitową liczbą całkowitą.

Więc int jest" mniejszym " typem danych niż NSUInteger, dlatego Ostrzeżenie kompilatora.

Zobacz także NSUInteger w "Foundation Data Types Reference":

Podczas budowania aplikacji 32-bitowych, NSUInteger jest 32-bitowym niepodpisanym liczba całkowita. 64-bitowa aplikacja traktuje NSUInteger jako 64-bitową niepodpisaną liczba całkowita.

Aby naprawić to Ostrzeżenie kompilatora, możesz zadeklarować lokalną zmienną count jako

NSUInteger count;

Lub (jeśli jesteś pewien, że Twoja tablica nigdy nie będzie zawierać więcej niż 2^31-1 elementów!), dodaj obsadę:

int count = (int)[myColors count];
 475
Author: Martin R,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-04 13:25:40

Wbrew odpowiedzi Martina, przerzucanie do int (lub ignorowanie ostrzeżenia) nie zawsze jest bezpieczne, nawet jeśli wiesz, że Twoja tablica nie ma więcej niż 2^31-1 elementów. Nie przy kompilacji dla 64-bitów.

Na przykład:

NSArray *array = @[@"a", @"b", @"c"];

int i = (int) [array indexOfObject:@"d"];
// indexOfObject returned NSNotFound, which is NSIntegerMax, which is LONG_MAX in 64 bit.
// We cast this to int and got -1.
// But -1 != NSNotFound. Trouble ahead!

if (i == NSNotFound) {
    // thought we'd get here, but we don't
    NSLog(@"it's not here");
}
else {
    // this is what actually happens
    NSLog(@"it's here: %d", i);

    // **** crash horribly ****
    NSLog(@"the object is %@", array[i]);
}
 24
Author: Adrian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-30 23:16:17

Zmień klucz w projekcie > Ustawienia budowania "typecheck wywołuje printf / scanf : NIE "

Wyjaśnienie : [Jak to działa]

Sprawdzanie połączeń do printf i scanf itp., aby upewnić się, że podane argumenty mają typy odpowiednie do określonego ciągu formatu i że konwersje określone w łańcuchu formatu mają sens.

Hope it work

Inne ostrzeżenia

Objective C implicit conversion traci liczbę całkowitą precision ' NSUInteger '(aka' unsigned long') to ' int

Zmień klucz "Konwersja domyślna na typ 32Bits > Debug > * 64 Architektura: No "

[Uwaga: może unieważnić inne ostrzeżenie o konwersji architektury 64 bitów] .

 5
Author: Darshit Shah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-18 11:52:07

Wykonanie expicit castingu do " int " rozwiązuje problem w moim przypadku. Miałem ten sam problem. Więc:

int count = (int)[myColors count];
 3
Author: Vladimir Despotovic,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-12 11:42:49