views:

75

answers:

2

Anyone know how I can declare a static method for an xcode class and then use it in my project?

I want to create a Common.h class and then do something like this in one of .m file

Common.MyStaticMethod();

I dont want to have to instantiate and instance of Common

+3  A: 

You will declare a class level method in Objective-C by using "+" before the method declaration.

// in Common.h
+ (void)myStaticMethod;

// call is from anywhere
[Common myStaticMethod]

That is, a "-" before method declaration means instance methods and "+" means class level method which are not related with any particular instance of the class.

taskinoor
Class methods are not static in Objective-C; they're resolved dynamically just like instance methods. The Objective-C equivalent of a static method is a C function, rather than a class method.
jlehr
Ya, class is also an object in obj-c. Thats why in my answer I have specified them as class method, not static method. By I guess this is what Vilasack is wanting.
taskinoor
A: 

Thanks! That is what I was looking for.

Vilasack