What is the diffrence between these two lines of code:
[cmController currentPageNo];
self.cmController.currentPageNo;
There is actually a functional difference - the second line is equivalent to:
[[self cmController] currentPageNo];
Using the -cmController property getter method to access the cmController ivar may have behaviours that make it different to accessing the ivar directly. These might include lazy initialisation or atomic thread locking, or a range of other behaviours. Unless you have a good reason to, you should (in general) use the accessor methods for properties, rather than accessing ivars directly.
So to clarify:
[cmController currentPageNo]; // 1
cmController.currentPageNo; // 2
[[self cmController] currentPageNo]; // 3
self.cmController.currentPageNo; // 4
1 and 2 are functionally identical to each other, but use different syntax. 3 and 4 are also functionally identical to each other, but use different syntax. You should use version 4, or version 3 if you have an aversion to dot-syntax.
Generally, dot notation is just syntactical sugar.
But in this case, there actually is some difference.
[cmController currentPageNo];
This will use the class's instance variable directly to get your cmController. It will then send the currentPageNo to it.
self.cmController.currentPageNo;
This, on the other hand, will use the current class's property definition to get the cmController via the class's ivar.
This means, basically, the the first is slightly more efficient than the second. Not because you used dot notation, but because you used cmController directly instead of self.cmController. [whatever currentPageNo] is the same as whatever.currentPageNo.
These lines are equivalent except for the syntax used:
[cmController currentPageNo];
cmController.currentPageNo;
On the other hand, these lines are equivalent except for the syntax used:
self.cmController.currentPageNo;
[[self cmController] currentPageNo];
I hope that's clear.
You can read about dot notation here.
There's no difference, functionally; objective C 2.0 just introduced the dot notation, giving those who aren't as into the brackets a way out.
EDIT:
As the comments pointed out, of course this is wrong. I was responding to the (unasked) question of "what's the difference between [cmController currentPageNo]; and self.cmController.currentPageNo;?"
Nick's answer gives the true answer, which is that the first line directly accesses the variable, while the second line uses the variable's getter method. The two lines also differ in that the first uses typical bracket notation, while the second does indeed use the new dot notation from objective C 2.0.
Related
I am working on a project that deals with lots of atomic operations. Till now I didn’t knew about atomic_load() and was only relying on assignment operator to get value of an atomic type and I haven’t seen an error except of so much of testing. Those atomic types are changed by multiple processes and threads as well by atomic_compare_exchange_strong_explicit(), so they will need an old value every time, and that’s where I always did oldValue = <Atomic_ type_variable> and it always works fine.
Is that just by chance? Should I prefer using atomic_load()?
foo = atomic_var is just a shortcut syntax for foo = atomic_load(&atomic_var);
Which itself is a shortcut for foo = atomic_load_explicit(&atomic_var, memory_order_seq_cst); That has a use-case when you want to use an ordering weaker than the default seq_cst.
The main reason for using atomic_load explicitly in your source code is probably to remind human readers that a variable or pointer is atomic. Or maybe as a part of a macro, using atomic_load(&(macro_input)) would create a compile-time error for a non-atomic pointer.
As a "generic" function, you can't take a normal function-pointer to it.
Its existence may be just to make it easier to write the language standard, and explain everything in terms of functions.
It's not the actual assignment that's key here, it's evaluating the atomic variable in an rvalue context (reading it's value as part of an expression, like you typically find on the right-hand side of an =). printf("%d\n", my_atomic_var); is also equivalent to atomic_load.
And BTW, the same thing holds for atomic_var = foo; being exactly the same as atomic_store_explicit with mo_seq_cst. Here it is assignment that's key.
Other kinds of lvalue references to an atomic variable are different, like read-modify-write atomic_var++ is equivalent to atomic_fetch_add.
I'm aware that the official scala style guide states for no-argument methods we should omit parenthesis when calling them if they have no side effects, so string.length instead of string.length(). The same would be for number.toChar instead of number.toChar() I assume.
When looking up the read methods in scala.io.StdIn._ many examples in tutorials seem to retain the parenthesis when invoking the read methods, e.g.
val x = readInt()
val y = readLine()
..even though they omit them on other examples as stated above. So I'm wondering if this is because these methods do indeed have side effects? I've seen a couple of examples of people just writing readInt, i.e. without parenthesis, so I want to know which is the correct style in this case.
Obviously, any method that works with input/output has side-effect as it changes the state of an external system and every time you call it the result is different.
Also, you can check how these methods are defined in Scala sources on github, and they all defined with parenthesis so I would insist you should you use parenthesis as well.
Methods like toChar don't have side effects as they are not changing any state. And they are defined without parenthesis see Source of class Int
This question already has answers here:
Dot (".") operator and arrow ("->") operator use in C vs. Objective-C
(5 answers)
Closed 9 years ago.
While reading some tutorials i found for the first time the "structure operator", I read that it's used to refer of an instace variable.
-(void)setNumber:(int)n{
self.number = n;
}
-(void)setNumber:(int)n{
self->number = n;
}
My doubt is: what it doing exactely? When do i use it? Why should i prefer it to the dot notation?
Dot syntax in Objective-C is just syntactic sugar which is replaced by the compiler with a method invocation. -> dereferences the pointer and accesses the member directly, with no method invocation.
In an Objective-C method call, you don't need to say self->foo, if your class has an instance variable named foo the compiler will infer the self-> portion of it. Because of this, while it's not completely unheard of, uses of -> with an Objective-C object are comparatively rare.
The arrow -> operator accesses instance variables directly, which you don't want in general, since it violates encapsulation and circumvents accessor methods (that can be a potential source of problems, like KVO not working or some desired side effects not taking place).
Instead, you should almost always use ., because it accesses the properties using getters and setters.
I have seen in many iOS header's that (^) is utilized, and I have never come across the reasoning as of why that sign is being used. Would anyone might like to enlighten into this?
Thanks.
Those often indicate "blocks". See the Blocks Programming Topics.
Alternatively, if you watch the beginning of WWDC 2012 session 712, they also walk you through blocks with a touch of historical context.
It signifies a block. A block is a syntax that allows you to create a callback function, and pass it into a method as a parameter. In other languages this is similar to a closure, a lambda, or an anonymous class.
For example a parameter that lists:
void(^)(NSString *myStr)
is expecting you to pass in a block/function that returns void, and takes in an NSString pointer.
You can create a block, based on the expected parameters declared in the method, with this syntax:
^(<Parameters>) { <Body> }
For example, a method that is expecting a block parameter might look like this:
-(void)doSomething:(void(^)(NSString *myStr))theBlock;
...and could be called like this:
[self doSomething:^(NSString *myStr) { NSLog(#"The String is: %#", myStr); }];
Your block will be called back from doSomething: just like a function, using the parameter name:
-(void)doSomething:(void(^)(NSString *myStr))theBlock {
theBlock(#"Hello!");
}
...which would display:
The String is: Hello!
The ^ character is used for blocks, in particular, block parameters.
If you're asking why the character '^' for use in blocks, it's because there's relatively few characters left that:
Are available on all typical keyboards.
Could be used at all - i.e. aren't already significant in the language and would conflict.
Don't look stupid.
That actually narrows it down to only two or three, and of those '^' was chosen because, well, because.
There's probably a record of this on the llvm.org mailing lists and so forth, if you want to pore over the discussion in detail.
You could also look at the minutes from the C++11 committee meetings on lambdas, which went through basically the same process.
It is possible to have a method in this manner:
[obj mergeObjs:obj1,obj2,obj3,nil];
Or have a method in this manner:
[obj mergeObjs:obj1,obj2...obj(n),nil usingBlocks:blk1,blk2,blk3....blk(m),nil];
where n may or may not be equal to m.
Basically multiple variable argument lists in a single method declaration. ?
This is not a potential answer:
[obj merge:[NSArray arrayWithObjects:[NSArray arrayWithObjects:...,nil],[NSArray arrayWithObjects:...,nil]...,nil]];
Thanks in advance.
Here is the link i found for Single Variable Argument Lists:
http://developer.apple.com/library/mac/#qa/qa1405/_index.html
How to create variable argument methods in Objective-C
You can't achieve this with a variable length argument list, but have you considered just passing two arrays?
[obj mergeObjs:(NSArray*)objs usingBlocks:(NSArray*)blocks];
Modern versions of clang (the Objective C compiler used by recent Xcode releases) even support NSArray literals
[instance mergeObjs:#[obj1, obj2, obj3] usingBlocks:#[^{}, ...]];
(Of course, making sure to copy your blocks appropriately for insertion into an NSArray).
No. Message dispatch boils down to a call to objc_msgSend() (or one of its variants). That follows the C calling convention and there's no way to express the multiple variable argument lists in that convention.
Concluding based on the responses & a serious loss of hair -
It it not possible to have 2 variable argument lists in a method declaration.
The purpose of having mulit-Variable-argument-list was to provide more readability in my context.
Will make do with Arrays... Sigh!!.