The Dot operator (".") And the arrow ("->") are used in C vs. Objective-c
I'm trying to bow my head to some differences in usage and syntax in C vs. Objective-C. In particular, I want to know how (and why) the usage is different for the point operator and arrow operator in C vs. Objective-C. Here is a simple example.
C Code:
// declare a pointer to a Fraction struct Fraction *frac; ... // reference an 'instance' variable int n = (*frac).numerator; // these two expressions int n = frac->numerator; // are equivalent Objective-C Code:
// declare a pointer to a Fraction Fraction *frac = [[Fraction alloc] init]; ... // reference an instance variable int n = frac.numerator; // why isn't this (*frac).numerator or frac->numerator?? So, seeing how frac same in both programs (i.e. it is a pointer to an object or fraction structure), why do they use different syntax when accessing properties? In particular, in C, the numerator property is accessed using frac->numerator , but with Objective-C it accesses the dot operator using frac.numerator . Since frac is a pointer in both programs, why are these expressions different? Can someone help clarify this for me?
frac is actually not the same in both programs.
AC Fraction is a struct that is a basic type without overloaded operators and can truly be constructed and destroyed by default. If you define functions or fields in a structure, the way to access these properties in C is through the point operator ( . ). Objective-C supports this statement when you use struct s. For convenience, you can perform the dereference operation and points using the arrow operator ( -> ) (the two equivalent expressions mentioned). Objective-C also saves this when accessing struct s.
An Objective-C Fraction in your example, however, it is likely (one would assume) a pointer of at least type id , which is simply the name of the class and a pointer to an instance of this class under the hood. It will also most likely be a subclass of NSObject or NSProxy . These Objective-C classes are distinguished by the fact that they have a whole level of predefined operations on top of the C structure (if you really want to delve into it, then you can take a look at the Objective-C Runtime Reference ). It is also important to note that the Objective-C class is always a pointer.
One of the main operations is objc_msgSend . When we work with these types of objects, the Objective-C compiler interprets the dot operator ( . ) Or the square bracket syntax ( [object method] ) as a call to the objc_msgSend method. For more on what's really going on here, see this series of posts by Bill Bumgarner, an Apple engineer who oversees the development of Obj-C at runtime.
The arrow operator ( -> ) should not actually be used for Objective-C objects. As I said before, instances of the Objective-C class are C-structures with an added extra level of communication, but this level of communication is significantly circumvented when you use the arrow. For example, if you open Xcode and enter [UIApplication sharedApplication]-> , and then open the method completion list, you will see the following:

Here you can see a bunch of normal fields, which we usually refer to with square bracket syntax (for example, [[UIApplication sharedApplication] delegate] ). However, these specific elements are C fields that store the values ββof their respective Objective-C properties.
So you can roughly think of it this way:
Dot operator on object C
- (at run time) Return value of the field
Arrow operator on object C (pointer)
- Dereference pointer
- Field return value
Point operator / square brackets on an Objective-C object (pointer)
- (at compile time) Replace with
objc_msgSendcall - (at runtime) Look at the Obj-C class definition, throw an exception if something goes wrong.
- Dereference pointer
- Field return value
Arrow operator on an Objective-C object (pointer)
- (at run time) Difference indicator
- Field return value
Now, I definitely simplify here, but summarize: the arrow operators seem to do the same thing in both cases, but the point operator has an extra / different meaning in Objective-C.
Spot recording is a design choice. Since we always deal with pointers to objc instances, I would suggest that the designers wanted something familiar, which would also not violate existing programs. It was introduced in ObjC 2 - just a few years ago. Before that, you always had to use parentheses for messaging.
Point notation matters, although this is not direct access, but a message.
I.e:
obj.property = val; // is the same as: [obj setProperty:val]; // and not: obj->property = val; val = obj.property; // is the same as: val = [obj property]; // and not: val = obj->property; You can still write obj->ivar to access the pointer to the members of the object (if visible).
In your first example, Fraction is a structure. In the second example, Fraction is the Objective-C class (and on iOS, there will probably be a subclass of NSObject ).
C ++ does not allow operator . overloading operator . . Therefore, without additional information, you can conclude that the point designation that you see is an additional language construct integrated into Objective-C, and not with a specific or overloaded C / C ++ operator.
As it happens, point notation is just a design feature that developers have chosen as a shorthand for accessing properties that is completely equivalent to an element with a square bracket:
myObjCVar.prop == [myObjCVar prop]; The point operator on objects is a special syntax for accessing the properties of objects. It calls the getter or setter property backstage. So, for example, [@"hello" length] and @"hello".length equivalent to *. For all other types, the point coincides with the point C, and the arrow is always the same.
* Note. An access method will not always be called the same as a property. If this declared property and declaration designates a special getter or setter method, that will be used instead.
Point and arrow notations are the same for C, as in Objective-C (strict superset). I think the main difference that needs to be distinguished is the difference between a struct object and Objective-C.
The dot notation used for objects in Objective-C is used for properties introduced in Objective-C 2.0. However, with structs, β and the dot notation between Objective-C and C are the same.