Class methods and self keyword in objective-c [duplicate] - objective-c

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
“Invalid use of 'this' in non-member function” in objective-c context?
If I use the keyword self inside a class method:
+ (id) myMetho
{
self ...
}
Does it refer to the class or to the current instance ?

In short, it represents the class. For a longer answer, have a look here

self just means "the class you are in"

Related

Is there an ObjC macro or function to get all the argument values received by a method? [duplicate]

This question already has answers here:
Pass all arguments of a method into NSLog
(2 answers)
Closed 8 years ago.
I know that NSStringFromSelector(_cmd) gives the name of the current method. I use that in a debugging macro to print what method I'm in. I would like to also get the method's arguments as strings so I can print them too. I'm thinking of something like *argv[] in C.
Is there any built-in facility for that?
Yes, c++ (and Objective-C) support va_list.
Do something like this:
- (void)do:(va_list)args
{
// do something with all your args
}
And use it like this:
[self do:#"foo", #"bar"]

How to call a method in Objective-C? [duplicate]

This question already has answers here:
How can I call a method in Objective-C?
(8 answers)
Closed 9 years ago.
I am just starting out in ios and am trying to make user defined functions...
my code:
-(void) testFunction{
//has self. calls
}
-(IBAction)button:(id)sender {
testFunction();
}
I get the warning: Implicit declaration of function is invalid in C99
What am I doing wrong? Or what do I have to add to my code?
In Objective-C you cant write testFunction(). You send 'messages' and you can do this via
[self testFunction]

objective c method asterisk [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Asterisk in parenthesis in Objective-C… What does it mean?
does anyone can find some resource or detail explanation about following code
-(void) add:(Cal *)c;
I am very new to obj-c.what does asterisk means in that code?
Thanks.
It means that the argument for the method is a pointer to an instance of a "Cal" object.

plus (+) versus minus (-) in objective-c [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What do the plus and minus signs mean in Objective C next to a method?
What's the difference between using a plus or minus in Objective-C?
For example, most of the time code starts -(void)somethingSomethingelse, but sometimes it will be +(void)somethingSomethingelse
Thanks!
- functions are instance functions and + functions are class (static) functions.
So let's say you have a class called Person, and the following functions
-(void)doSomething;
+(void)doSomethingElse;
You would invoke these functions with the following:
Person *myPerson = [[Person alloc] init];
[myPerson doSomething];
[Person doSomethingElse];
This is more of a syntax description, assuming you understand the concept of class vs instance.
edit:
just to add: In objective-C, you can actually invoke a class function on an instance, but the effect is no different than invoking it on the class itself (essentially compiles to the same thing).
So you can do
[myPerson doSomethingElse]
Generally, you wouldn't do this as it is confusing and misleading to read. I am pointing it out so you won't be surprised if you come across code like this somewhere.
In short, (+) is a class method and (-) is an instance method
See this answer for a full explanation
What is the difference between class and instance methods?
member and public functions respectively.
Such that
id object = [[NSObject alloc] init];
+ (id)alloc;
- (id)init;
Where NSObject is a Class and id is an object
If you have ever used C++, a + is equivalent to static

Beginner Objective C syntax observation [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does #synthesize window=_window do?
How come when you #synthesize something like buttonPressed , You need to do this:
#synthesize buttonPressed = buttonPressed_;
I've been following some tutorials and this keeps coming up. Why?
You do not have to do it that way.
By default, #synthesize variableName does work, if your synthesized accessors shall have the same name as your instance variable.
In your example, the instance variable is called buttonPressed_ but your accessor methods will omit the _ and thus just be called setButtonPressed and buttonPressed.