An important syntax knowledge in objective-c is summarized here.
Class method, also known as static method, refers to the method modified with the static keyword. This method is a method of the class and does not belong to an instance (object) of the class ). Instance variables cannot be directly used in class methods. There are three calling methods: direct call, class name, method name, Object Name, and method name. The instance method is a method without modifying the static keyword. Each instance object has its own instance method, which is independent of each other and does not share one. The call method can only be the object name. method name.
The method declared by modifier static is a static method, and the method declared by modifier static is an instance method. The static method of the class can be used regardless of whether the class is generated or the object is not generated. The format is class name. Static Method Name. The static method can only use the static data member and static method of the class where the static method is located. This is because when a static method is used, the class of the static method may not have any objects, even if an object exists, because the class name is used. the static method name method calls the static method. The static method does not have the this pointer to store the object address, and the data member of the object to be accessed cannot be determined. After the class creates an object, the instance method can be used in the format of Object Name. instance method name. The instance method can use all static members and instance members of the class to which the method belongs.
When are static methods and instance methods used?
First, let's talk about the instance method. When you write a method to a class, if the method needs to access the member variables of an instance, it is defined as an instance method. A class-one instance usually has some member variables, which contain the status information of the instance. This method needs to change these statuses. This method must be declared as an instance method.
The static method is the opposite. It does not need to access the member variables of an instance and does not need to change the status of an instance. We define this method as a static method.
Some misunderstandings about static methods and instance methods.
I. Static methods are resident in memory, and the instance method is not. Therefore, static methods are highly efficient but occupy the memory.
In fact, the methods are the same. In terms of loading time and memory usage, the static method is the same as the instance method, and is loaded when the type is used for the first time. There is basically no difference in the call speed.
2. The static method allocates memory on the stack, and the instance method is on the stack.
In fact, it is impossible for all methods to allocate memory on the heap or stack. As code, the method is loaded to a special code memory area, which cannot be written.
3. instance methods can be called only after an instance is created. Static methods are not needed and simple.
In fact, if a method has nothing to do with its instance type, it should be static, and no one will write it as an instance method. Therefore, all instance methods are related to instances. Since they are related to instances, creating an instance is an inevitable step. In practice, you can write all the instance methods as static ones, and pass the instance as a parameter.
Some methods seem unrelated to the instance, such as IComparer. compare method, but in fact every class that implements this interface will only be responsible for comparing its own type instances. This is C #1. the x specification does not have any historical issues caused by generics.
Most static methods are related to the instance of the class, such as various Parse methods. The reason why the static method is made is that it does not have an instance as a parameter. Most of the rest are for semantic or other purposes.
The first method is to declare the instance and call the instance method.
When a class has multiple instances, such as student A, student B, and student C, we use the first method. In the case of multiple threads, as long as each thread creates its own instance, the first method is generally thread-safe.
The second method is to call the instance method through a static instance.
This situation is special. It is usually the only instance of this class in the whole program. We change the status of this instance by calling the instance method of this instance. This instance is thread-safe in the case of multiple threads. Unless we lock this instance. Prevent other threads from accessing the instance.
Method 3: directly call the static method
In this case, the static method does not need to change the status of an instance. You only need to obtain a few parameters to complete the tasks. For example, to determine whether a file exists, you only need to provide the file path and file name to know whether the file exists.
Reprinted 2:
To call a static method of a class, do you need release? (Memory Management) (interview questions)
In my opinion, static member methods do not require objects or release.
Note: in fact, the static member method also has an object, called a Class Object. However, this is the first time that the system loads the static member into the memory, in addition, such objects are released only when the program is closed, and are not controlled by the program itself.
There are three concepts to distinguish: Class, instance and object, the class itself is loaded into the memory (may be the memory of the code segment, not the heap memory, or the stack memory), can be called an object; create a class instance. This instance can be called an object, so there are class objects and instance objects. class objects are a drawing, the instance object is the first floor created by class object. It is actually something that actually exists, so it is called an object. It only has different properties. One is a drawing, and the other is a building created by drawing.
In fact, I also want to know which company's interview questions are not rigorous.
In this sentence, there can be two types of understanding: one is whether the objects returned in the static method of the class need to be release; the other is whether the instance method needs to be called to be release? That is to say, to call release, you must first use alloc + init to call the method of the instance object. If the method is used up, you must call the static method of the class directly, whether the release class object is required.
From the above analysis, we can draw the following conclusion: the company has just got in touch with objectiv-c. The interviewer of the company is not really familiar with objective-c, and is also the first guy to get started. Finally, it may be because the interviewer is wrong, or the person who posted the question is wrong.
Reprinted 3:
I want to mark this good post. Today, the static method didn't respond at once. You have helped me a lot, I was inspired by the phrase "No need for release for static methods because there are no objects". Sometimes I learned more and easily forgot the most basic idea.
Note: The same is true. In the early years, whether it was learning c ++, objective-c, or java, it took a long time to get used to something, I don't know what it means, so it's easy to digest it.
It's like learning five strokes and using five strokes. After N years, a lot of words won't be split. However, it's a habit to make them work normally, instead of forgetting the rules.
For example, if you want to teach a child to add and subtract less than 10, you can see how to teach it, or do you still remember how to learn it at that time ?!
What about addition and subtraction within 20? I used 10 methods and 10 methods. Have you ever learned it? I have never learned this before. I just remember that no one taught these rules at that time. addition is positive, subtraction is inverted, and so on, that is, the oil seller and hander.