Objective-C basic data type

Time:2021-12-5

Objective-C basic data type

Because Objective-C (hereinafter referred to as objc) is essentially a superset of C language, objc also supports all the basic data types supported by C language, and objc also supports some other common data types.

Int and nsinteger

Int in C language is also supported in objc, but it is not recommended that you use int, but nsinteger in cocoa framework,

See nsinteger definition for details

#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif

Note that this is mainly to match 64 bit and 32-bit processors at the same time. In the code of the above official framework, we can see that the nsinteger in the 64 bit kernel is of long type, while it is of int type in the 32-bit kernel. If you use nsinteger, you don’t have to deliberately consider the kernel bit width.

Bool and bool

There are no Boolean variables in the C language standard. Bool types in C + + are true and false, which are the same in many other C-like languages, such as Java, c#, PHP, etc. However, in objc, you can use bool types, but it is more recommended to use bool types dedicated to objc. The values of this basic Boolean are yes and No.

Float and cgfloat

Cgfloat is not the basic variable of the foundation framework, but is defined in the UIKit framework. CG represents coregraphic (core drawing framework). From the definition, the difference between float and cgfloat is also based on the bit width and type of the system kernel.

#if defined(__LP64__) && __LP64__
# define CGFLOAT_TYPE double
# define CGFLOAT_IS_DOUBLE 1
# define CGFLOAT_MIN DBL_MIN
# define CGFLOAT_MAX DBL_MAX
#else
# define CGFLOAT_TYPE float
# define CGFLOAT_IS_DOUBLE 0
# define CGFLOAT_MIN FLT_MIN
# define CGFLOAT_MAX FLT_MAX
#endif

/* Definition of the `CGFloat' type and `CGFLOAT_DEFINED'. */

typedef CGFLOAT_TYPE CGFloat;

As you can see, cgfloat is double in 64 bits and float in 32 bits.

NSString

Char and string types are also available in objc, but they are basically unnecessary. As the soul class of the whole cocoa framework, nsstring is extremely powerful and basically competent for all basic and more complex string processing in modern programming languages. The detailed use method will be introduced in a special chapter later.

The nsstring class does not need alloc and init. The cocoa framework highly optimizes the nsstring class so that it operates like a basic type when instantiated. for example

NSString* textA = @"123";     
NSString* textB = textA;
textA = @"456";
NSLog(@"%@",textA); // Output 456
NSLog(@"%@",textB); // Output 123

Have you noticed that after the textb pointer points to texta, the texta value changes, and textb should also change. But the actual situation is not, because for nsstring type, the equal sign assignment is actually a deep copy. Texta = @ “456” the pointer of texta has been changed. The actual operation is equivalent to texta = [@ “456” copy].
Textb = texta, the actual operation is equivalent to textb = [texta copy].
The copy function here is the immutable copy method of nsobject.

In addition, the nsstring class itself supports inter conversion with many basic types.

//Cgpoint to string
 NSString *NSStringFromCGPoint(CGPoint point);
 //Cgvector vector to string
 NSString *NSStringFromCGVector(CGVector vector);
 //Cgsize to string
 NSString *NSStringFromCGSize(CGSize size);
 //Rectangle to string
 NSString *NSStringFromCGRect(CGRect rect);
 //Matrix transformation to string
 NSString *NSStringFromCGAffineTransform(CGAffineTransform transform);
 //Boundary to string
 NSString *NSStringFromUIEdgeInsets(UIEdgeInsets insets);
 //Shift to string
 NSString *NSStringFromUIOffset(UIOffset offset);

//Reverse method above
 CGPoint CGPointFromString(NSString *string);
 CGVector CGVectorFromString(NSString *string);
 CGSize CGSizeFromString(NSString *string);
 CGRect CGRectFromString(NSString *string);
 CGAffineTransform CGAffineTransformFromString(NSString *string);
 UIEdgeInsets UIEdgeInsetsFromString(NSString *string);
 UIOffset UIOffsetFromString(NSString *string);

Note that all of the above methods are C functions that can be called anywhere.
The basic types of CG series are declared using the structure of C language.

In addition, nsstring can also be converted to numbers

NSString* number = @"1111113";
    NSInteger intValue = [number integerValue]; // Rotational shaping
    CGFloat   floatValue = [number doubleValue]; // To floating point

NSValue

Nsvalue is a class that can interoperate with various basic data types. Including cgpoint, cgrect, cgsize, etc. for example

[NSValue valueWithCGSize:CGSizeMake(100, 100)];
[NSValue valueWithRange:NSMakeRange(0, 10)];

NSNumber

The difference between nsnumber and the above is that nsnumber is not a basic data type, but an object.
Nsnumber inherits from nsvalue, and nsvalue inherits from nsobject.
Nsnumber supports inter conversion with basic data types.

In addition, nsnumber supports the same functions as nsstring@Symbol abbreviation

NSNumber * number = @(123);
NSNumber * number1 = @(3.1415);
NSNumber * number2 = @(YES);

NSInteger intValue = [number integerValue];
CGFloat floatValue = [number1 doubleValue];
BOOL boolValue = [number2 boolValue];

array

The basic type array commonly used in C and C + + corresponds to the nsarray class in objc. The data stored in this class must also be a class, not a basic data type. Therefore, the basic data type should be converted to object storage, such as

//Note that the nssarray object is returned in @[] mode
NSArray* numbers = @[@(1),@(2),@(3)];

Here, the numbers array stores three nsnumber types of data: 1, 2, and 3.

In objc, two-dimensional arrays or multi-dimensional arrays are not common. If necessary, it is recommended to use the array of C for storage.

Section

In addition to basic data types, this chapter also introduces some basic containers of objc, such as nsstring, nsvalue, nsarray, etc. these basic containers are indispensable blood of cocoa framework throughout. We will introduce more basic containers and their basic usage in later chapters.

Recommended Today

The real problem of Alibaba IOS algorithm can’t hang up this time

More and more IOS developers continue to enter the peak of job hopping in 2020 Three main trends of interview in 2020: IOS bottom layer, algorithm, data structure and audio and video development Occupied the main battlefield. Data structure and algorithm interview, especially figure, has become the main reason for the failure of most first-line […]