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

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.

Related

Why would we use a pointer in an NSInteger but not in an NSNumber? [duplicate]

This question already has answers here:
What's the difference between NSNumber and NSInteger?
(5 answers)
Closed 6 years ago.
I have an NSInteger that doesn't use a pointer and an NSNumber that does use a pointer. Can someone explain to me why this is the case? All my teacher said was the NSInteger is being used as a type alias but I'm not familiar with that either yet.
This question asked a direct question asking for the reason there was no pointer in NSInteger; not asking for all the differences.
Bear in mind that Objective-C is C. Thus:
An NSInteger is a scalar, a built-in C data type (an integer). [The actual size of this integer depends on the architecture, 32-bit vs. 64-bit. But it is still some form of C integer.]
An NSNumber is an object; Objective-C object references are represented as C pointers.

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.

What is objc's ->? [duplicate]

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.

Understanding objective-c dot notation

I am learning objective-c, and as part of my learning I am trying to convert dot notation where I see it, back to bracket notation (I prefer bracket notation for now)
So my current view of the world is:
Objective-c prior to 2.0 had dot notation for structs only?
Objective-c 2.0 introduced dot notation for properties. This allows accessing setters / getters via dot notation?
Messages cannot be sent to structs?
In a book I'm reading, I came accross the below code, and on my first few attempts, I failed miserably to convert it to bracket notation.
...
CGRect switchViewFrame = switchView.frame;
switchViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height;
Having dug a bit deeper, I came to the following conclusions...
switchViewFrame.origin.y cannot be converted to bracket notation because switchViewFrame is defined as a CGRect, which is a struct in apples documentation, therefore the dot notation is necessary?
The rvalue on the second line is a combination of brackets and dots because shareApplication is a class method of UIApplication, and therefore a message is sent, but statusBarFrame is defined as a property of CGRect i.e. a struct and therefore dot notation is required?
Any help to clarify these points would be much appreciated.
If I have completely misunderstood and you can in fact convert it to bracket notation, could you please explain why.
Thanks
1) Objective-c prior to 2.0 had dot notation for structs only? Yes, as far as I know.
2) Objective-c 2.0 introduced dot notation for properties. This allows accessing setters / getters via dot notation? Yes.
3) Messages cannot be sent to structs? Correct, sending a message is equivalent to calling a method in the object the message is sent to.
4) switchViewFrame.origin.y cannot be converted to bracket notation because switchViewFrame is defined as a CGRect, which is a struct in apples documentation, therefore the dot notation is necessary? Yes.
5) The rvalue on the second line is a combination of brackets and dots because shareApplication is a class method of UIApplication, and therefore a message is sent, but statusBarFrame is defined as a property of CGRect i.e. a struct and therefore dot notation is required? No: statusBarFrame is a property of an UIApplication object. But statusBarFrame is a struct with components origin and size, which are also structs.
So you were nearly everywhere right.
https://stackoverflow.com/a/10444012/2449268
Explains it really well, hope it answers your question.
Also you may use bracket notation on frame as below:
CGRect switchViewFrame = switchView.frame;
switchViewFrame.origin.y += [[UIApplication sharedApplication] statusBarFrame].size.height;

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.