Objective-C data type – nsobject
Objective-C (hereinafter referred to as objc) language is an object-oriented encapsulation of C language. It can be seen from the English name that object is an object, which is equivalent to class in C + + and Java.
Object is the core of programming in objc. The so-called objects and classes are the general name of a class of abstract things. For example, if we want to write an application like wechat, I will create it
contactsIt has its own attributes and is called in objc
head portraitAnd so on.
Another example is that our application needs to manage vehicles throughout the company,
vehicleCan be used as a class, and
Passenger carthen is
vehicleofSubclass, in objc, subclasses inherit the properties and methods of the parent class, and can also have their own unique properties and methods.
In objc, the framework we use is called cocoa, and the corresponding IOS version is called cocoa touch. Some codes of the two frameworks are the same, such as the most basic and important
FoundationAlmost all objects in the framework begin with NS, which is the code left over by the nextstep operating system of the computer company nextstep created by jobs in the years when he left apple.
Among all ns objects, the most basic class is
NSObjectClass is the base class of all objects in the cocoa framework, and all other ns objects are
NSObjectIt contains all the basic properties and methods in NS class, such as
+ (void)load; // This function is executed when the class is referenced into the program + (void)initialize;// This function is executed when the class is first executed
These two methods have nothing to do with each other. There is no sequence of calls. They are rarely used. There are only a few ways to use them. They will be explained in detail in the following chapters.
+ (instancetype)alloc;// Instantiation function - (instancetype)init; // Initialization function + (instancetype)new; // Initialization function The method consists of: +/-(return value type) method name: (parameter type) parameter name: (parameter type 2) parameter name 2, etc
In objc, functions are divided into
Example method, respectively
The type in the first bracket is the return value type, which can be an object, a common variable type (int integer, bool Boolean value, etc.), or no return value (void).
Of the above three methods
instancetypeIs to replace the type of the current class. For example, in the nsobject class,
instancetypeIs to return the nsobject instance.
Class method and instance method
Class methods are methods that can be called directly with class names, such as
NSObject* someObject = [NSObject new];
initMethod is an instance method, which needs to be called first
+ (instancetype)alloc; // Allocate space and return instances
After returning the instance, call with the instance.
NSObject* someObject = [[NSObject alloc] init];
The nested writing here is equivalent to
NSObject* someObject = [NSObject alloc]; [someObject init];
Class method or instance method can pass in unlimited parameters or no parameters. The writing format is
Colon (parameter type) parameter name, the colon can be preceded by a description or not. Generally, we write it. For example, we have a computer class that has two methods
- (BOOL)loginInWithName:(id)name password:(id)password; - (void)loginOut;
These are the two methods we assume, corresponding to user name login and logout respectively.
In the login method, the user name and password are passed in, and a bool result is returned to tell the user whether the login is successful, while the logout method does not need to pass in parameters or return values.
The generic type in objc refers to the ID type. Because objc is a runtime linking language, the method calls of classes and instances are bound only when the program is actually running. During compilation, if you cheat the compiler, you can pass, but there will be errors at runtime.
For example (the following code can’t be run directly, just for illustration)
Book* book = [Book new]; id car = book; // Note that car is generic here [car openDoor];// The compiler does not know the real type of car, and no error will be reported in this step
But the real implementation
openDoorOnly when the car is a book, can we know that the car is not a book
Open the door opendoorMethod, an exception is thrown. More practical uses of generics will be explained in later chapters.
After Xcode introduces arc (automatic memory management), nsobjec does not need to be called manually
- (void)dealloc; // release
To free up memory. Moreover, the compiler will prohibit writing and calling dealloc or other methods related to reference counting in arc environment, such as
- (instancetype)retain OBJC_ ARC_ UNAVAILABLE; // Reference count + 1 - (oneway void)release OBJC_ ARC_ UNAVAILABLE; // Reference count - 1 - (instancetype)autorelease OBJC_ ARC_ UNAVAILABLE; // Join auto release pool - (NSUInteger)retainCount OBJC_ ARC_ UNAVAILABLE; // Returns the reference count
Note the macros after these methods
OBJC_ARC_UNAVAILABLE, is to tell the compiler that these methods are disabled under arc.
- (BOOL)isEqual:(id)object; // Object comparison @property (readonly) NSUInteger hash; // Unique hash value
Each instantiated object in memory has a unique hash value as a reference for comparison.
isEqualThe method is to compare the hash value.
@property (readonly) Class superclass; // Parent class - (Class)class OBJC_ SWIFT_ UNAVAILABLE("use 'anObject.dynamicType' instead"); // Current class
The data type returned by these two functions is class, which is a C language structure. The following two methods can be used to determine the type of the current class.
- (BOOL)isKindOfClass:(Class)aClass; // Is it aclass - (BOOL)isMemberOfClass:(Class)aClass; // Is it aclass or its subclass
NSObjecAs the base class in objc, the calling of some of its methods is a necessary skill throughout the development of the whole cocoa framework, which must be learned to use.