Tipi in Objective-C su iOS


147

Voglio chiedere i tipi di dati fondamentali in Objective-C su iOS.

Ho bisogno della dimensione su cui è rappresentata la variabile e dell'intervallo della variabile. Quindi nell'esempio: int breve - 2 byte - con segno: da -32768 a 32767 e senza segno: da 0 a 65535 Questo è solo un esempio.


Objective-C è uno strato sottile sopra C , il che significa che hai gli stessi tipi di dati fondamentali diC.
Cœur il

Risposte:


349

Questa è una buona panoramica:

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

o esegui questo codice:

Processo a 32 bit:

  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 si esegue su un iPhone 3GS (iPod Touch e iPhone precedenti dovrebbero produrre lo stesso risultato) si ottiene:

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 a 64 bit:

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
Si noti che con il rilascio dell'SDK di iOS 7, alcuni tipi sono più grandi in modalità 64 bit.
JeremyP,

13
Aggiornato per processo a 64 bit
jjxtra il

Risposta fantastica davvero utile. Divertente però, in Swift puoi semplicemente dichiarare un "var" e lasciarlo a quel

1
Perché int è 32 bit in un processore a 64 bit? Vedo che c'è qualcosa chiamato LP64 nel processore iPhone A7, ma non capisco il motivo. Forse per compatibilità all'indietro o eredità .... Non lo so. Qualcuno sa il motivo?
Ricardo,

1
Dov'è UINT_MAX?
Codehearted

20

Nota che puoi anche usare perfettamente i tipi di larghezza fissa C99 in Objective-C:

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

La pagina di Wikipedia ha una descrizione decente di ciò che è disponibile in questa intestazione se non hai una copia dello standard C (dovresti, tuttavia, poiché Objective-C è solo una piccola estensione di C). Puoi anche trovare le intestazioni limits.hed inttypes.hessere utile.


C'è anche SInt32, UInt32, ecc. (Usato molto in Core Audio).
Nicolas Miari,

12

Aggiornamento per il nuovo arco a 64 bit

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
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.