A framework is a collection of classes, methods, functions, and documents organized in accordance with certain rules, so that programmers can develop programs more easily and quickly. The cocoa program is written mainly in two frames Foundation and Applicationkit (Uikit). The foundation framework mainly defines some basic classes for programmers to use, while Applicationkit is primarily a user-interface-designed class used for Mac development. All classes in the foundation framework inherit from NSObject.
As a general-purpose object-oriented library of functions, Foundation provides strings, numeric management, containers and enumerations, and other features that are not directly related to the graphical user interface.
In the previous study, we have learned the basic data type. But these are basic data types, they're not objects, so you can't send messages to them. Sometimes, however, they need to be treated as objects. For example, we need to put the basic type into the collection (the cocoa can only hold objects, not the basic data type), then we need to convert the base data type to a numeric object. The number object "NSNumber" is provided in OC to "wrap" the basic data type into objects so that we can handle them as basic data types. Here, we can compare and study the boxed unboxing in Java.
To initialize a numeric object using a class method: (Objects created using the class method do not require us to manage memory)
class method to create an object;
int num2 =;
NSNumber *number2 = [NSNumber numberwithint:num2];
BOOL Boo2 = true;
NSNumber *bool2 = [NSNumber Numberwithbool:boo2];
char CH2 = ' a ';
NSNumber *character2 = [NSNumber numberwithchar:ch2];
To initialize a numeric object using an instance method: (An object created using an instance method requires us to manage memory, which is not necessary for arc)
instance method creation object;
int num =;
NSNumber *number = [[NSNumber alloc] initwithint:num];
BOOL boo = true;
NSNumber *booll = [[NSNumber alloc] initwithbool:boo];
char ch = ' a ';
NSNumber *character = [[NSNumber alloc] initwithchar:ch];
Similarly, we can convert the NSNumber object to the basic type:
The NSNumber object is converted to the base type
int nn = [number intvalue];
BOOL BB = [Booll boolvalue];
char cc = [character charvalue];
Now we're going to output the NSNumber object and the base data type:
instance method creation object;
int num =;
NSNumber *number = [[NSNumber alloc] initwithint:num];
NSLog (@ "num =%@", number);
BOOL Boo = true;
NSNumber *booll = [[NSNumber alloc] initwithbool:boo];
NSLog (@ "boo =%@", booll);
char ch = ' a ';
NSNumber *character = [[NSNumber alloc] initwithchar:ch];
NSLog (@ "character =%@", character);
The NSNumber object is converted to the base type
int nn = [number intvalue];
NSLog (@ "NN =%d", nn);
BOOL BB = [Booll boolvalue];
NSLog (@ "BB =%hhd", BB);
char cc = [character charvalue];
NSLog (@ "CC =%c", cc);
The output results are as follows:
The output is exactly the same, note that the ASCII value of ' a ' is 97. When you output an object, the format controller uses "%@".
GitHub home: https://github.com/chenyufeng1991. You are welcome to visit.