I need to know how I can make static method and static property like static method in java...
Thanks
Maxime
"In both C and Objective-C, a static variable is a variable that is allocated for the entire lifetime of a program.
As others have pointed out, static class methods are prefixed with a plus (+) in declaration, like this:
@interface MyClass : NSObject
+ (void)myClassMethod;
@end
Objective-C does not make static properties quite as simple, and you need to jump through the following hoops:
Complete example:
static NSString* _foo = nil;
@interface MyClass : NSObject
+ (NSString *)getFoo;
+ (void)setFoo;
@end
// implementation of getter and setter
+ (NSString *) getFoo {
return _foo;
}
+ (void) setFoo:(NSString*) value {
if(_foo != value) {
[_foo release];
_foo = [value retain];
}
}
// optionally, you can set the default value in the initialize method
+ (void) initialize {
if(!_foo) {
_foo = @"Default Foo";
}
}
I am not an Obj-C expert, but this seems to work well in my code. Correct me if anything here is off.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With