Reputation: 2799
I am an absolute beginner in objective-c and just read an overview of the language on cocoadevcentral.
The first section briefly discusses syntax for calling methods and gives two examples; one for calling a method on an object and the second is a method with input being called on an object,
[object method];
[object methodWithInput: input];
Can anyone explain the difference to me, possibly with a simple example?
Upvotes: 0
Views: 101
Reputation: 12113
There is no huge difference between the two and all depends on what you are doing.
Method 1
[object method];
There are two parts to this method.
object
this is either an instance of a class or is a class itself all depends on the type of method you are calling whether it be an instance method or a class method. Each are declared differently.
A Class method is declared like + (void)myClassMethod;
and would be called like [NSString myClassMethod];
An Instance method would be declared like - (void)myInstanceMethod;
and would be called like [myStr myInstanceMethod];
(Where myStr
is an instace of NSString
)
method
The second part is the actual method that you are calling this all that this will do when you call something like [myStr myInstanceMethod];
it will call the implementation of that method so it would call
- (void)myInstanceMethod
{
NSLog(@"We called our instance method");
}
Method 2
[object methodWithInput: input];
The only difference here is that we are passing in an argument. So here we have three parts the same first two from method 1 and the argument
input
All this is, is the value that you are passing into the method to be used within it.This type of method would be declared something like - (void)myInstanceMethodWithArgument:(NSString *)str;
. Here are just saying that we have an argument of type NSString
so when we call this like [str myInstanceMethod:@"Some Random String I want to pass in"];
it will run the following implementation code
- (void)myInstanceMethod:(NSString *)str
{
NSLog(@"My str value is : %@", str);
}
Method 3
[object methodWithInput1:input1 andInput2:input2];
Just throwing this in because you my get a little confused later when dealing with multiple arguments. This is exactly the same as method 2 except it has two arguments and not one. This would be declared like - (void)myInstanceMethodWithInput1:(NSString *)str1 andInput2:(NSString *)str2;
. Does exactly the same is method 2 except it has multiple arguments that's it nothing to be scared of.
I would recommend that you have a read of the Apple Coding Guidelines for Cocoa. Best of look with learning as it's probably not the easiest language to learn.
Upvotes: 1
Reputation: 100642
It's just the difference between saying "I wait" and "I eat an omelette". In some cases you can say what you mean with just a verb. In some cases a sentence needs an object in order to communicate its meaning.
The same thing applies in programming. Sonetimes you're going to need to specify more than just the action. But not always.
Upvotes: 1
Reputation: 597
When you call method without input data it means that method will work with already existing class's properties.
- (void)someMethod {
self.var_1 = self.var_2 + self.var_3; //or any other implementation
}
You will call this method like this
[self someMethod];
When you call method with some input data it means that this data will be used in method's implementation
- (void)someMethodWithInputData:(NSInteger)inputData {
self.var_1 = self.var_2 * inputData;
}
You will call it like this
[self someMethodWithInputData:10];
Upvotes: 1
Reputation: 4728
Try substituting 'input' for 'argument'.. [object someMethod:(CGFloat )floatArgument];
The type should be there in the brackets, with a dereference operator (*) eg (NSObject *)theArgument if that argument is a pointer.
So basically some methods supply one or more arguments, and some do not, just as with C
Upvotes: 1