Tipos em Objective-C no iOS


147

Quero perguntar sobre os tipos de dados fundamentais no Objective-C no iOS.

Eu preciso do tamanho em que a variável é representada e do intervalo da variável. Portanto, no exemplo: int curto - 2 bytes - assinado: -32768 a 32767 e sem sinal: 0 a 65535 Este é apenas um exemplo.


O Objective-C é uma camada fina no topo de C , o que significa que você tem os mesmos tipos de dados fundamentais queC.
Cœur

Respostas:


349

Esta é uma boa visão geral:

http://reference.jumpingmonkey.org/programming_languages/objective-c/types.html

ou execute este código:

Processo de 32 bits:

  NSLog(@"Primitive sizes:");
  NSLog(@"The size of a char is: %d.", sizeof(char));
  NSLog(@"The size of short is: %d.", sizeof(short));
  NSLog(@"The size of int is: %d.", sizeof(int));
  NSLog(@"The size of long is: %d.", sizeof(long));
  NSLog(@"The size of long long is: %d.", sizeof(long long));
  NSLog(@"The size of a unsigned char is: %d.", sizeof(unsigned char));
  NSLog(@"The size of unsigned short is: %d.", sizeof(unsigned short));
  NSLog(@"The size of unsigned int is: %d.", sizeof(unsigned int));
  NSLog(@"The size of unsigned long is: %d.", sizeof(unsigned long));
  NSLog(@"The size of unsigned long long is: %d.", sizeof(unsigned long long));
  NSLog(@"The size of a float is: %d.", sizeof(float));
  NSLog(@"The size of a double is %d.", sizeof(double));

  NSLog(@"Ranges:");
  NSLog(@"CHAR_MIN:   %c",   CHAR_MIN);
  NSLog(@"CHAR_MAX:   %c",   CHAR_MAX);
  NSLog(@"SHRT_MIN:   %hi",  SHRT_MIN);    // signed short int
  NSLog(@"SHRT_MAX:   %hi",  SHRT_MAX);
  NSLog(@"INT_MIN:    %i",   INT_MIN);
  NSLog(@"INT_MAX:    %i",   INT_MAX);
  NSLog(@"LONG_MIN:   %li",  LONG_MIN);    // signed long int
  NSLog(@"LONG_MAX:   %li",  LONG_MAX);
  NSLog(@"ULONG_MAX:  %lu",  ULONG_MAX);   // unsigned long int
  NSLog(@"LLONG_MIN:  %lli", LLONG_MIN);   // signed long long int
  NSLog(@"LLONG_MAX:  %lli", LLONG_MAX);
  NSLog(@"ULLONG_MAX: %llu", ULLONG_MAX);  // unsigned long long int

Quando executado em um iPhone 3GS (o iPod Touch e os iPhones mais antigos devem produzir o mesmo resultado), você obtém:

Primitive sizes:
The size of a char is: 1.                
The size of short is: 2.                 
The size of int is: 4.                   
The size of long is: 4.                  
The size of long long is: 8.             
The size of a unsigned char is: 1.       
The size of unsigned short is: 2.        
The size of unsigned int is: 4.          
The size of unsigned long is: 4.         
The size of unsigned long long is: 8.    
The size of a float is: 4.               
The size of a double is 8.               
Ranges:                                  
CHAR_MIN:   -128                         
CHAR_MAX:   127                          
SHRT_MIN:   -32768                       
SHRT_MAX:   32767                        
INT_MIN:    -2147483648                  
INT_MAX:    2147483647                   
LONG_MIN:   -2147483648                  
LONG_MAX:   2147483647                   
ULONG_MAX:  4294967295                   
LLONG_MIN:  -9223372036854775808         
LLONG_MAX:  9223372036854775807          
ULLONG_MAX: 18446744073709551615 

Processo de 64 bits:

The size of a char is: 1.
The size of short is: 2.
The size of int is: 4.
The size of long is: 8.
The size of long long is: 8.
The size of a unsigned char is: 1.
The size of unsigned short is: 2.
The size of unsigned int is: 4.
The size of unsigned long is: 8.
The size of unsigned long long is: 8.
The size of a float is: 4.
The size of a double is 8.
Ranges:
CHAR_MIN:   -128
CHAR_MAX:   127
SHRT_MIN:   -32768
SHRT_MAX:   32767
INT_MIN:    -2147483648
INT_MAX:    2147483647
LONG_MIN:   -9223372036854775808
LONG_MAX:   9223372036854775807
ULONG_MAX:  18446744073709551615
LLONG_MIN:  -9223372036854775808
LLONG_MAX:  9223372036854775807
ULLONG_MAX: 18446744073709551615

6
Observe que, com o lançamento do iOS 7 SDK, alguns tipos são maiores no modo de 64 bits.
precisa saber é o seguinte

13
Atualizado para o processo de 64 bits
jjxtra

Resposta incrível realmente útil. Engraçado, porém, no Swift você pode apenas declarar uma "var" e deixar por isso mesmo haha :)

1
Por que int é 32 bits em um processador de 64 bits? Vejo que existe algo chamado LP64 no processador do iPhone A7, mas não entendo o motivo. Talvez por compatibilidade com versões anteriores ou legado ... não sei. Alguém sabe o motivo?
Ricardo

1
Onde fica UINT_MAX?
codehearted

20

Observe que você também pode usar os tipos de largura fixa C99 perfeitamente no Objective-C:

#import <stdint.h>
...
int32_t x; // guaranteed to be 32 bits on any platform

A página da wikipedia possui uma descrição decente do que está disponível neste cabeçalho se você não tiver uma cópia do padrão C (você deve, porém, uma vez que o Objective-C é apenas uma pequena extensão do C). Você também pode encontrar os cabeçalhos limits.he inttypes.hser útil.


Há também SInt32, UInt32, etc. (usado muito no Core Audio).
Nicolas Miari

12

Atualização para o novo arco de 64 bits

Ranges:
CHAR_MIN:   -128
CHAR_MAX:   127
SHRT_MIN:   -32768
SHRT_MAX:   32767
INT_MIN:    -2147483648
INT_MAX:    2147483647
LONG_MIN:   -9223372036854775808
LONG_MAX:   9223372036854775807
ULONG_MAX:  18446744073709551615
LLONG_MIN:  -9223372036854775808
LLONG_MAX:  9223372036854775807
ULLONG_MAX: 18446744073709551615
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.