In object-oriented languages such as java/c#, access to a method can be controlled through public/private/protected to control its access rights. In OC, however, the method does not have access modifiers. So do we have a way to make it private?
1. If a method is not declared in the header file, then this method sends a message to the object in the form of [receiver MethodName] at compile time, and the compiler will have a warning telling you that the method could not be found, but the actual operation will still work.
Cause: At compile time, even if this method is not declared in the header file, the compiler will still compile the signature of the method into the list of methods of the class, and when the message is sent, the message list will be automatically found, and will be triggered if a message of the same name is located)
2. If a method is defined in an anonymous taxonomy, then at compile time, the compiler sends a message to the object via [receiver MethodName], and the compiler will still have a warning telling you that the method is not found, but it still works when it actually runs
Reason: Ibid.
Conclusion: In OC, there is actually no private method in real sense.
Do you have a real private property?
1. If a property is defined in the anonymous classification of a method, the compiler will prompt for an error at compile time by Obj.prop to the property, and the compilation does not pass. In this way, it seems that you can define a private property.
But....
We know that all the method calls in OC are passed through the message, and even if you use the dot syntax of Obj.prop to assign a value to an object property, the compiled code is still a call to convert to a message.
Obj.prop actually sends an SetProp method to the object, which is equivalent to [obj SetProp].
According to the above explanation, if you use [obj SetProp] to assign a value to this so-called "private property", you can still assign a value that is successful and works as expected.
In fact, there is no real private method and private property in OC. But in actual use, we should abide by the rules, cannot call the method, do not call.
Objective-c object-oriented, does the class have a real private method and a private property?