Tuesday, September 24, 2013

About OBJC static variables

Just learning OBJC, see OBJC plus function with C + + static function, like,
Ask, static functions can only be called a static variable, OBJC have static variables do not. How to use.
Baidu less, only to ask for advice.
Reply:
objective-c static variables seems to be in class. m files declare a static local variable, and then return via an instance method or operation. Unlike C + + has seemingly static members
Reply:
static, add their own ah. . . There can not be instantiated static function call, you need class called directly. . .
Reply:
Error, OBJC STATIC class member variables can not be used.
Reply:
You can use static, defined in the realization of the inside,. M in the
Reply:
That in the class interface declarations do not
Reply:
So I think objective-c is not a static member of the class. It is through the implementation file in a class declare a static local variables to achieve the static C + + member functions. Generally used as a single case of this model, as follows.

classA.h

@ Interface classA: NSObject
{
}
+ (ClassA *) share;

classA.m

static classA * gClassAObj = nil;
@ Implementation classA
+ (ClassA *) share
{
if (gClassAObj == nil)
{
gClassAObj = [[classA alloc] init];
}
return gClassAObj;
}

@ End
Reply:
Lane is also possible in the interface declaration.
Such as:
classA.h
static classA * gClassAObj = nil;
@ Interface classA: NSObject
{
}
+ (ClassA *) share;

classA.m


@ Implementation classA
+ (ClassA *) share
{
if (gClassAObj == nil)
{
gClassAObj = [[classA alloc] init];
}
return gClassAObj;
}

@ End
This. M where also can be used directly.
Reply:
Did not see, Halo, it should be:
# Import
static int sTier_count = 0;
@ Interface Tier: NSObject
{
@ Private
int tier_num;
}
@ End
@ Implementation Tier
- (Id) init
{
if (self = [super init]) {
sTier_count + +;
tier_num = sTier_count;
}
return self;
}

Reply:
That is equivalent to in the definition file is a global variable, and not in the class package in the. I do not know what this is designed to.

No comments:

Post a Comment