Conversion of Basic Data Types in ios Study Notes

  • 2021-07-01 08:18:01
  • OfStack

Preface

A few days ago, when I was doing some small functions, I suddenly found that some basic data conversion was forgotten, so I quickly sorted it out and wrote it down! It is convenient for me to consult later, and also give some references to friends in need. Let's not say much below, let's look at the detailed content.

1. NSString

String splicing:


NSString *string = [NSString stringWithFormat:@"%@%@", Object , Object ];

String and int


int intString = [newString intValue];
NSString *string = [NSString stringWithFormat:@"%d",intSteing];

String and float


float floatString = [newString floatValue];
NSString *string = [NSString stringWithFormat:@"%f",floatString];

NSData and String


NSString *dataStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];

NSData *strData = [dataStr dataUsingEncoding: NSUTF8StringEncoding];

2. NSData

NSData and Byte


NSString *testString = @"1234567890";

NSData *byteData = [testString dataUsingEncoding:NSUTF8StringEncoding];    

Byte *testByte = (Byte *)[byteData bytes];

//----------------------------------------------------
Byte byte[] ={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};

NSData *adata = [[NSData alloc] initWithBytes:byte length:24];

NSData and UIImage


UIImage *aimage = [UIImage imageWithData: imageData];

NSData *imageData = UIImagePNGRepresentation(aimae);

json to NSData


NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];

3. Boolean type (stored inside the computer as int type)

The Boolean type is _ Bool (alias BOOL), and the range of values is 1 or 0, where 1 can be represented by TURE and YES, and 0 can be represented by FALSE and NO.

4. Enumeration type (stored inside the computer as int type)

If you need to define a set of related constants, you can use enumeration types to define these constants as one type. For example, if the game is in the upper, lower, left and right directions, you can enumerate types: enum direction{up,down,left,right}. Where up starts at 0, down is 1, and so on, plus 1. If you don't want to start from 0, you can also specify initial values, such as: enum direction{up=1,down,left,right}

5. nil, NULL, NSNull

1. nil is used to assign values to objects (any object in OC belongs to id type), Null is used to assign values to any pointer, NULL and nil are not interchangeable, nil is used to assign values to class pointers (in OC, the class is an object and is an instance of meta-class of the class), and NSNull is used for collection operations. Although they all represent null values, they are used in different situations.

2. OC has a feature that when a message is sent to an nil object, the system returns a value of 0 instead of causing an exception, which is completely different from the processing of NullPointerException of Java and the direct crash of C/C + + program, because nil is the legal value of the object, and nil object can also receive messages.

3. nil is defined as an empty object, that is, an object with a value of 0.

6. id type

1. The three most common types in OC are id, Class and SEL. id is a pointer to an OC object, which is equivalent to void * in C language and can be mapped to any object pointer type or to other objects. Of course, you can also pass any message to id, but if the id does not support this message, it will return 1 runtime exception.

2. The id data type can store objects of any type. In a sense, it is a general object type. If you want to replace it with a basic type, you need to encapsulate the basic data type.

3. id is an object pointing to any one that inherits the Object (or NSObject) class. It should be noted that id
Is a pointer, so there is no need to add an asterisk when using id. For example: id foo = nil;

4. In OC, id replaces int type as the default data type (int is the default return type for function return values on C language).

Summarize

The above is the whole content of this article. I hope the content of this article will bring 1 certain help to everyone's study or work. If you have any questions, you can leave a message for communication.


Related articles: