Preface
A few days ago, when I was doing some small functions, I suddenly found that I had forgotten some basic data conversions, so I quickly sorted them out and wrote them down! It is convenient for you to check it in the future, and also for friends in need to refer to it. I won’t say much about it below, let’s see the detailed content.
1. NSString
String stitching:
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 strings
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 as int type inside the computer)
The Boolean type is _Bool (alias BOOL), and the value range 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 as int type inside the computer)
If you need to define a set of related constants, you can use enumeration types and define these constants into a type. For example, the game can be enumerated in the upper, lower, left and right directions:enum direction{up,down,left,right}.
where up starts from 0, down is 1, and so on add 1. If you do not want to start from 0, you can also specify the initial value, such as:enum direction{up=1,down,left,right}
V. nil, NULL, NSNull
Used to assign values to objects (any object in OC belongs to the id type), Null assigns values to any pointer, NULL and nil cannot be interchanged, nil is used for class pointer assignment (in OC, a class is an object, an instance of the meta-class of the class), while NSNull is used for collection operations. Although they represent null values, they are used in different situations.
There is a feature that when sending a message to the nil object, the system returns a value of 0 instead of causing an exception. This is completely different from the direct crash of Java's NullPointerException and C/C++ programs, because nil is the legal value of the object, and the nil object can also receive messages.
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. It is equivalent to void * in C language. It can map any object pointer type to it, or map it to other objects. Of course, you can also pass any message to the id, but if the id does not support this message, a runtime exception will be returned.
The data type can store any type of object. In a sense, it is a general object type. If you want to use a basic type instead, you need to encapsulate the basic data type.
is an object that inherits the Object (or NSObject) class. It should be noted that id
It 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 the int type as the default data type (in C language, int is the default return type).
Summarize
The above is the entire content of this article. I hope that the content of this article will be of some help to everyone’s study or work. If you have any questions, you can leave a message to communicate.