Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective-c static/class method definition - what is the difference between "static" and "+"?

I'm wondering if someone can explain the difference between the functions below. They are both static, but require different signature syntaxes. I'm wondering how these are handled at runtime, and why you would use one over the other?

+ (int) returnInt:(NSString *)myString1 withString2:(NSString *)myString2
{
    if ([myString1 isEqualToString:myString2])
        return 1;
    else 
        return 0;
}

vs.

static int returnInt(NSString *myString1, NSString *myString2)
{
    if ([myString1 isEqualToString:myString2])
        return 1;
    else 
        return 0;
}

Thanks!

like image 333
bpatrick100 Avatar asked Jan 31 '11 14:01

bpatrick100


People also ask

What is the difference between a static class and a static method?

A static class can only contain static members. A static method ensures that, even if you were to create multiple classB objects, they would only utilize a single, shared SomeMethod function. Technically, there's no difference, except that ClassA's SomeMethod must be static.

What is the difference between a method and a static method?

Instance method vs Static methodStatic methods can access the static variables and static methods directly. Static methods can't access instance methods and instance variables directly. They must use reference to object.

What is difference between class and static?

The difference between a static class and a non-static class is that a static class cannot be instantiated or inherited and that all of the members of the class are static in nature. To declare a class as static, you should mark it with the static keyword in the class declaration.

What is the difference between static method and dynamic method?

The Static Method Thesis states that the scientific method is unchangeable and therefore transhistorical, whereas the Dynamic Method Thesis states that the scientific method is changeable and may vary between historical episodes and communities.


1 Answers

Unlike in (say) C++, where static member functions are just ordinary functions in the class' namespace, Objective-C has proper class methods.

Since classes are objects, calling a class method is really like calling an instance method on the class. The main consequences of this are:

1) Calling a class method incurs a slight (although generally inconsequential) overhead, since method calls are resolved at runtime.

2) Class methods have an implicit 'self' argument, just like instance methods. In their case, 'self' is a pointer to the class object.

3) Class methods are inherited by subclasses.

together, 2 and 3 mean that you can do stuff like this with a class method:

+ (id) instance
{
    return [[[self alloc] init] autorelease];
}

then create a new class that inherits the method and returns a new instance of itself, rather than the superclass.

I believe that marking an ordinary c function static will just make it unavailable to files other than the one it's defined in. You'd generally do this if you wanted to make a helper function that is only relevant to one class and you wanted to avoid polluting the global namespace.

like image 164
Chris Devereux Avatar answered Oct 01 '22 22:10

Chris Devereux