In Objective-C what do we call for "[ob method]" syntax? - iphone

In Objective-C i saw that we can use dot operator to set and get a value and for the same task i saw something like "[ob method]" inside square braces method call, what do we call for this kind of syntax?

Bracket notation: A staple of the small talk language, and now a lovely visage of the ObjC language.
Brackets were SmallTalk's way of saying "You there, take this message and do something with it", and so that's how they were implemented in Objective C. We send a message to the first part [Object] and state the message in the second part [Object Message];
Of course, they also serve a similar function with properties. Properties in most languages are written in dot notation (Object.property), but with Objective-C and the modern runtime's support for non-ivar-backed properties, and the #synthesize directive, properties automatically generate getters named the same. Sounds complicated? It isn't. If I have the property example, then I can access it in one of two ways:
self.example;
Or
[self example];
Easy!
But the #synthesize directive doesn't stop there. We get a getter, and a setter as well. The setter can be accessed the same number of ways as a getter.
self.example = foo;
is the equivalent of
[self setExample:foo];
Bracket notation is in fact so important, that the compiler optimizes most dot notation out to bracket notation at compile time.

This is called messaging, or message sending, you are sending a message method to the object ob
Its similar to calling a method in java or C++
So the equivalent in java would be
ob.method();

[ob method]
It's the way you call methods in objective-c. The same way you would call myObject.method(), in Java por example.
ob.myProperty
Is how you access ivars by using it's getter/setter method, for example:
Get method -> NSLog(#"%#",ob.myProperty);
Set method -> ob.myProperty = #"Hello World";
Notice that you also use the set method like this:
[ob setMyProperty:#"Hello World"];
Or use the get method like this:
NSLog(#"%#",[ob myProperty]);
In objective-c you normally will not create manually the setter and getter, because you have the opportunity to create them using #property and #synthesize.

This is known as function calling..
like you call function in other programing language like in java or c#:
ob.method() // where ob is object and method is the function name..
similarly if you want to call a function in objective c the syntax is calling function is like this :
[ob method];

Related

arrow operator in swift

I am trying to adopt a SDK written in objective-c in a swift project. The objective-c way to initialize the SDK is as follows:
#implementation ViewController
nokeSDK *nokelock;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//INITIALIZE NOKE SDK
nokelock = [[nokeSDK alloc] init];
nokelock->cmd = [[nokecommand alloc] init];
I don't believe there is an equivalent to the arrow operator in swift, so is it possible to still initialize? I can't seem to find any info about his particular subject.
In Objective-C, a property is merely syntactic sugar for accessor methods; if, as is usually the case, these are a front for an instance variable, you can, with the right privacy access, bypass the accessor methods (or not have them in the first place) and get/set the instance variable directly. That's what your code is doing.
But Swift doesn't draw that distinction. A variable declared at the top level of a type declaration, in Swift, is a property; behind the scenes, a stored property has accessor methods, and you are passing through those automatically when you get/set the property. There is no separate instance variable to get/set.
To see what I mean, make a hybrid Swift / Objective-C app with class Thing whose interface looks like this:
#interface Thing : NSObject {
#public
NSString* s;
}
Now create the generated interface. You will see that, in Swift, s is nowhere to be seen.
Presumably, therefore, to rewrite your code, you'd need to turn cmd into a property, or at least provide a setter method. Otherwise, Swift will never be able to set the Objective-C cmd instance variable. Of course, if it is also up to you to rewrite nokeSDK in Swift, your problems are over, as you can now do whatever you like.

ios - creating functions

I know what a function is, but I am trying to write a few into my project. I have just watched a video series on Objective-C from Lynda.com where I got the idea from.
In the video is it is explained that I can write a function like this:
void declareWebView ()
However if I write it like that into my code, the error comes up and says that my _webView and self (as self.view) are not available.
if I write it like this:
-(void) declareWebView
Then I do not have an issue.
Any ideas on how to get the first one right?
As far as I can tell, I cannot set any parameters with the second way of writing.
The first is as you said called a function. It is part of the C part of Objective-C, and is not connected to objects or classes, so the variable self or any instance variables of an object don't have any meaning. You can pass it variables that are objects though like so:
void my_func(NSString *string, id someObject, int someInt);
void my_func(NSString *string, id someObject, int someInt)
{
NSLog(#"string = %#, someObject = %#, someInt = %d",string,someObject,someInt);
}
The second is a method, and in a method you can access self and, within instance methods, access instance variables. Replacing the "-" in front of the method with a "+" makes it a class method. In a class method you can't access instance variables and self refers to the class itself, not an instance.
Hope this helps!
The first way is written in the language C. The second way is written in Objective-C. Objective-C methods must be declared the second way. You cannot access your object's private members and properties from within a C function directly, but you could use accessor methods on the Objective-C object and call them from a C function.

vs [mpk5 weaponAttachments]

I'm able to make the method for the call [self weaponAttachments:mpk5] but I don't like having to call self. I think [mpk5 weaponAttachments] is more natural and is easier to read.
The problem I'm having is I need to pass in the weapon (mpk5) in order to use it, which I can do with the first method but not with the second one. Does this mean that I need to subclass NSDictionary in order to be able to use a statement like [mpk5 weaponAttachments]? If so, how do I get ahold of the caller "mpk5" so that I can use it inside the method?
EDIT
I apologize for not putting this in the first time but my objective is to have [mpk5 weaponAttachments] return an NSDictionary or NSArray. Right now I have NSDictionary *attachments = [self weaponAttachments:mpk5]; which works but it just doesn't seem like the best approach.
So firstly, your two calls are a little mixed up:
[self weaponAttachments:mpk5] calls the weaponAttachments method, passing in the variable mpk5.
But [mpk5 weaponAttachments] is either asking the mpk5 object to return the weaponAttachments property or is asking the mpk5 object to run a method called weaponAttachments (I'm simplifying here - it's always a method, but if you're using properties you probably won't realise this as Objective-C will create them for you).
These are fundamentally different things.
On to the brunt of your question:
I don't like having to call self
...unfortunately, if you're working in an object-oriented language you're going to have to get used to this. Say I have a class called mySimpleClass, and a method inside that class called doSomething. Writing this:
[mySimpleClass doSomething] would be what we call a static method. Whereas calling [self doSomething] from within an instance of mySimpleClass would be an instance method.
If you're unsure of the difference between static and instance methods you should probably step back and take a look at some of the basic guides out there.

Is it wrong to use the dot syntax as a getter?

I know that the . is a shortcut for a setter. Sometimes, I use that kind of code:
cell.textLabel.text = [NSString stringWithFormat:#"this is row %i", indexPath.row];
This works as expected, but I was wondering, is it better (or more correct maybe?) to write
cell.textLabel.text = [NSString stringWithFormat:#"this is row %i", [indexPath row]];
Or, in other words, should I use the dot syntax only with the = operator, like
aTextField.text = #"whatever";
Any links/docs are welcome, thanks :)
PS. In case you didn't see the tag, I'm talking about iOS here.
Dot (.) is not only a shortcut for setter, it's shortcut for getter too. You can use dot for getter too. There is no problem, neither this is bad practice. From Obj-C 2.0 programming guide, "You can use the dot syntax to invoke accessor methods using the same pattern as accessing structure elements. The dot syntax is purely “syntactic sugar”". Note that, it is saying about accessor method, not only setter.
It's a matter of taste.
I prefer not to use the dot syntax for various reasons:
When using dot syntax, it's much harder to find only the places in your code where you set an value. Search for setValue: is much easier than searching for .value
As a long time C programmer, my brain is wired to associate the dot syntax with accessing struct members. I find it rather hard to get used to the dot syntax in a different scope.
The setXY: syntax close follows the natural language much closer. Makes reading someone else's code so much easier.
"." is a shortcut for accessing a #property (which may, by the way, be readonly). From the syntax point of view whether this is a getter or a setter depends on the operand position:
self.enabled = NO; // setter
BOOL isEnabled = self.enabled; // getter
self.selected = self.enabled = NO; // this is OK too
It's coding style so neither is better.
I would note two things though.
As a long time Objective C code I prefer the [indexPath row] as it is consistent with the rest of the code and for a set I would use [aTextField setText:#"whatever"]
But if you need to use the . notation for keypaths the accessing the same variable via method notation in the same piece of code will seem odd.
Apple documentation says
Objective-C provides a dot (.) operator that offers a compact and convenient syntax you can use as an alternative to square bracket notation ([]s) to invoke accessor methods.
and
myInstance.value = 10;
printf("myInstance value: %d", myInstance.value);
The dot syntax is purely “syntactic
sugar”—it is transformed by the
compiler into invocation of accessor
methods (so you are not actually
accessing an instance variable
directly). The code example above is
exactly equivalent to the following:
[myInstance setValue:10]; printf("myInstance value: %d", [myInstance value]);
Using the dot syntax is not coding style or a matter of taste!
The dot syntax is for accessing properties. The message sending syntax is for dispatching methods. They are conceptually two different things. Legacy and backwards compatibility to Objective-C 1.0 unfortunately makes them interchangeable, which has caused allot of confusion.
Weather to user the dot-syntax or not is dead simple:
If a public header declares something as property, then access it as a property using the dot-syntax, as the author of the interface explicitly intended!
If a public header declares something as a method, then access it using the message sending syntax, as the author of the interface explicitly intended!
Make NO exceptions.
The hard question is, should you declare something as a property, and thus tell your clients that doit-syntax should be used, or should you declare a method? The simple answer is:
Use properties for states (is something).
Use methods for behaviors (do/calculate something).
Another rule of thumb is that properties should be self-contained, there should be no other way to change the value of the property but to set the property itself. This is Not a hard rule, use common sense, many sensible exceptions exist. For example a hidden property that can be changed with setHidden:animated: method.

What does this -> symbol mean?

I'm a little bit confused now after I've seen a code snippet for iPhone SDK which makes use of -> rather than dot notation. It looks a lot like PHP but it does work on the iPhone. Can someone explain what's up with ->, is that some deep C-secret I should know about?
Example:
- (void)setFileURLs: (NSArray*)elements {
if (self->fileURLs != elements)
fileURLs is an instance variable or property, like so:
#property(nonatomic, retain) NSArray *fileURLs;
and there's an #synthesize for fileURLs.
Now what I think this is: Because this is the setter method for fileURLs, it would be bad to use dot notation to access the instance variable. In fact, when I do it, the application crashes. That is because it calls itself over and over again, since the dot notation accesses the accessor method and not the ivar directly. But -> will access the ivar directly.
If that's right, the question changes a little bit: Why then write "self->fileURLs" and not just "fileURLs"? What's the point of adding that self-> overhead in front of it? Does it make sense? Why?
a->b is just another way for writing (*a).b. This is a way for accessing fields of a structure or instance variables of an object that are referenced by a pointer.
See section "Other operators" at:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
Since self is a pointer, you have to use -> and not . to access its members. Prepending the self reference to fileURLs is probably just a coding style used by the author (equivalent to writing this.member).