What is objc's ->? [duplicate] - objective-c

This question already has answers here:
Dot (".") operator and arrow ("->") operator use in C vs. Objective-C
(5 answers)
Closed 7 years ago.
What does objc's -> stand for ?
Is there any difference from just dot chain?
e.g. self.delegate , self->delegate

-> in Objective-C is the same as -> in C. It is a field access operator that lets you dereference a pointer (as opposed to dot . operator, which requires a struct).
What's confusing about it in Objective-C is the dot syntax on pointers for accessing properties. So the rules for choosing a dot vs. -> become a little confusing:
Use dot . for accessing Objective-C properties on Objective-C objects, which are always accessed through pointers
Use arrow -> for accessing Objective-C instance variables on Objective-C objects, and for accessing fields on C structures through pointers
Use dot . for accessing fields on C structures.

Related

Create Objective-C literal object like NSString [duplicate]

This question already has answers here:
Can the new Clang Objective-C literals be redirected to custom classes?
(2 answers)
Closed 6 years ago.
I want to know if it's possible to create Objective-C literals like NSString, where instead of [[Object alloc] init], and then assigning you can just assign a value to it, such as #"A string".
Obviously NSString is an object because it has methods to manipulate the data in addition, so in theory there should be a way to do it yourself, but I'm not sure where to even go about finding stuff like this.
Objective-C is C. The primitive (what I would call scalar) data types are all numbers and are completely defined by the language; you cannot add to them (though you can rename them using typedef. The corresponding literals, such as 1 and "hello", are also part of C.
Similarly, literals like #"howdy" and #[#"howdy"], though defined by Objective-C rather than C, are part of the language and you cannot change or add to them, as the literal syntax is built into the language.

difference beetween string.length and [string length] in objective C [duplicate]

This question already has answers here:
Dot notation vs. message notation for declared properties [closed]
(19 answers)
Closed 9 years ago.
Can someone explain to me why in Objective C
when declaring a string with
NSString *string;
I can use both string.length and [string length] to return the length of the string?
in the .h there is only the method
-(NSUInteger *)length
So why can I use the (dot) notation?
It's just syntactic sugar, they are both the same. Dot notation came in with #property but behind the scenes it's converted into method calls for you. Indeed, any #property Definition you do have will generate associated accessor methods and they are what is actually called. Again, you can call the method names rather than using dot notation.
Try to use the notation which makes the most sense, both to you and for the context. Dot notation can't be used with methods that take any parameters, but also only use it for methods without side effects.
Interesting article on the topic at the big nerd ranch.

Can someone explain . and -> in objective c?

Help me :( i dont know why?
#interface RootViewController : UITableViewController {
BOOL isSearchOn;
}
self->isSearchOn = NO; ( no error)
self.isSearchOn = NO; ( error)
Dot operator . is used to access property.
Arrow operator -> is used to acces instance variable.
So you use
self->isSearchOn = NO; // which is an instance not a property
You can also use this way by skipping self->
isSearchOn = NO;
-> is the traditional C operator to access a member of a structure referenced by a pointer. Since Objective-C objects are (usually) used as pointers and an Objective-C class is a structure, you can use -> to access its members, which (usually) correspond to instance variables.
or
When applied to pointer arrow operator is equivalent to applying dot operator to pointee (ptr->field is equivalent to (*ptr).field)
or
pSomething->someMember
is equivalent to
(*pSomething).someMember
. is used when you have a object and want to reference a attribute or a method, while you must use -> when holding the pointer only. instead of using -> you could aswell go for *. but I guess that you should in any case read about pointers in C.
There are essentially three operators here, two of which are inherited from C and the third which is from Objective-C.
In C, . and -> do the same thing—they retrieve a value from a struct—but - automatically dereferences a pointer to a struct, so the following two expressions are equivalent:
(*a).b
a->b
The second operator is simply syntactic sugar. Importantly, however, . doesn't make any sense when applied to a pointer—you have to use ->.
Objective-C allows the use of . with an object pointer (and only with a pointer, since all Objective-C objects are pointers) to reference a property value, a much higher level construct than the C operators provide.
Therefore, you may use . on objects to retrieve properties defined in the interface with #property, but you will need to use -> to retrieve ivars (instance variables) directly, which isSearchOn is in your example. However, it is usually better practice to expose variables using properties in Objective-C rather than refer to them directly using C's -> operator.

What does `->` symbol represent in objective-c [duplicate]

This question already has answers here:
What does this ' ->' mean in c/objective-c?
(7 answers)
What is the difference between '->' (arrow operator) and '.' (dot operator) in Objective-C?
(3 answers)
Dot (".") operator and arrow ("->") operator use in C vs. Objective-C
(5 answers)
Closed 10 years ago.
I have been looking at some code and come across the symbol -> being used like obj->method(argument); I have done a little bit of research and found it basically is the same as [obj method:argument]; but I am unsure what -> actually is or does.
So my question is, what does the -> symbol mean in objective-c?
It means the same as the struct dereference operator does in C, which is used to access fields within the struct via a pointer:
struct mystruct
{
int field;
};
struct mystruct *mystruct = ...;
printf("field=%d\n", mystruct->field);
In Objective-C it can also be used to access fields within Objective-C objects:
#interface MyObj : NSObject
{
#public
int field;
}
#end
MyObj *myObj = [[MyObj alloc] init];
NSLog(#"field=%d", myObj->field);
Note that you can only access these fields externally if they are declared #public.
I have been looking at some code and come across the symbol -> being
used like obj->method(argument); I have done a little bit of research
and found it basically is the same as [obj method:argument]; but I am
unsure what -> actually is or does.
So my question is, what does the -> symbol mean in objective-c?
Exactly the same thing it means in C; it is for accessing an item in a C structure. Way back in the days of yore, Objective-C was implemented purely as a C preprocessor extension + a runtime. Classes were nothing more than concatenated C structures and the preprocessor turned each ivar access into self->ivar.
I.e. ivar and self->ivar do the same thing (in a method of class).
Now, you can use -> to poke at some other object's (#public) ivars. But don't. That breaks encapsulation exactly because Objective-C's line of encapsulation is drawn at the method interface. Always use the setters/getters such that behavior can be either observed or overridden.
Finally, no, there is nothing like obj->method(argument) anymore. There was, once, in a failed experiment called Modern Syntax, but it was abandoned because it was a pointless waste of time. You can't use -> to invoke methods.

Dot (".") operator and arrow ("->") operator use in C vs. Objective-C

I'm trying to wrap my head around some of the differences in usage and syntax in C vs. Objective-C. In particular, I want to know how (and why) the usage differs for the dot operator and the 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 is the same in both programs (i.e. it is a pointer to a Fraction object or struct), why are they using different syntax when accessing properties? In particular, in C, the numerator property is accessed with frac->numerator, but with Objective-C, it is accessed using the dot operator, with frac.numerator. Since frac is a pointer in both programs, why are these expressions different? Can anyone help clarify this for me?
frac is actually not the same in both programs.
A C Fraction is a struct, which is a base type with no overloaded operators and is only really able to be constructed and destructed by default. If you define functions or fields on the struct, the way to access those properties in C is with the dot (.) operator. Objective-C maintains this operator when you use structs. For convenience, you can perform a dereference-and-dot operation using the arrow (->) operator (the two equivalent expressions you mention). Objective-C also preserves this when accessing structs.
An Objective-C Fraction in your example, however, is probably (one would assume) a pointer of at least type id, which is simply a classname and pointer to the instance of that class under the hood. It's also very likely to be a subclass of NSObject or NSProxy. These Objective-C classes are special in that they have a whole layer of predefined operations on top of just a C struct (if you really want to dig into it then you can take a look at the Objective-C Runtime Reference). Also important to note, an Objective-C class is always a pointer.
One of the most basic operations is objc_msgSend. When we operate on these types of objects, the Objective-C compiler interprets a dot (.) operator or the square bracket syntax ([object method]) as an objc_msgSend method call. For more detailed info about what actually happens here, see this series of posts by Bill Bumgarner, an Apple engineer who oversees the development of the Obj-C runtime.
The arrow (->) operator is not really supposed to be used on Objective-C objects. Like I said, Objective-C class instances are a C struct with an extra layer of communication added, but that layer of communication is essentially bypassed when you use the arrow. For example, if you open up Xcode and type in [UIApplication sharedApplication]-> and then bring up the method completion list, you see this:
Here you can see a bunch of normal fields which we generally access with square bracket syntax (like [[UIApplication sharedApplication] delegate]). These particular items, however, are the C fields that store the values of their respective Objective-C properties.
So, you can roughly think of it like this:
Dot operator on a C object
(at run time) Return value of the field
Arrow operator on a C object (pointer)
Dereference pointer
Return value of the field
Dot operator/square brackets on an Objective-C object (pointer)
(at compile time) Replace with call to objc_msgSend
(at run time) Look up Obj-C class definition, throw exception if something went wrong
Dereference pointer
Return value of the field
Arrow operator on an Objective-C object (pointer)
(at run time) Dereference pointer
Return value of the field
Now I'm definitely oversimplifying here, but to summarise: the arrow operators appear to do basically the same thing in both cases, but the dot operator has an extra/different meaning in Objective-C.
Dot-notation is a design choice. Since we always deal with pointers to objc instances, I'd guess the designers wanted something familiar, which also would not break existing programs. It was introduced in ObjC 2 - just a few years ago. Before that, you always had to use brackets for messaging.
Dot notation makes a difference though - it is not direct access, but a message.
That is:
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 a pointer to object's members (if visible).
In your first example, Fraction is a struct.
In your second example, Fraction is an Objective-C class (and in iOS would likely be a subclass of NSObject).
C++ does not allow overloading of operator .. Therefore without additional information you can deduce that the dot notation you're seeing is an additional language construct integrated into Objective-C, rather than a C/C++ defined or overloaded operator.
As it happens, the dot notation is simply a design feature the implementors chose as shorthand for property access, entirely equivalent to the square bracket getter:
myObjCVar.prop == [myObjCVar prop];
The dot operator on objects is a special syntax for accessing objects' properties. It calls the property's getter or setter behind the scenes. So, for example, [#"hello" length] and #"hello".length are equivalent*. For all other types, the dot is the same as the C dot, and the arrow is always the same.
* Note: The accessor method won't always be named the same as the property. If it's a declared property and the declaration designates a special getter or setter method, that one will be used instead.
The dot and arrow notation are equally the same in C as it is in Objective-C (strict superset of ). I think the fundamental difference that needs to be distinguished is the difference between a struct and an Objective-C object.
The dot notation used for objects in Objective-C are used for properties that was introduced in Objective-C 2.0. However, with structs, the -> and dot notation between Objective-C and C are the same.