spandana
spandana

Reputation: 755

How to make a declaration common to both the methods of a class->Objective C

I have a class called RSTimer that has two methods called CreateTimer and KillTimer which uses the same dictionary object. I could not able to declare the variable inside the interface as well as inside the implementation. Its pointing out in the declaration and says that it is not a constant.

-(BOOL)KillTimer:(unsigned short)wTimerId
{
    stRs232Timer* pEvent;
    BOOL bReturn=FALSE;
    CFDictionaryValueCallBacks cbs = {0,NULL,NULL,NULL,NULL};
    CFMutableDictionaryRef cfdict = CFDictionaryCreateMutableNULL,0,&kCFTypeDictionaryKeyCallBacks,&cbs);
      NSLock* theLock = [[NSLock alloc]init];
      if ([theLock tryLock]) {
        if (CFDictionaryContainsKey(cfdict,&wTimerId)) {
         free(pEvent);
         bReturn = TRUE;
        }
        [theLock unlock];
    }
return bReturn;
}
-(BOOL)CreateTimer:(RS232TimerInterface*)pStack withTimerId:(unsigned short)wTimerId withPeriod:(uint8_t)uPeriod withPersistentState:(BOOL)bPersistent
{

    CFDictionaryValueCallBacks cbs = {0,NULL,NULL,NULL,NULL};
    CFMutableDictionaryRef cfdict = CFDictionaryCreateMutable(NULL,0,&kCFTypeDictionaryKeyCallBacks,&cbs);
    CFNumberRef timerId = CFNumberCreate(NULL,kCFNumberShortType,&wTimerId);
    [self KillTimer:wTimerId];
    NSLock* theLock = [[NSLock alloc]init];
    if ([theLock tryLock]) {
         CFDictionarySetValue(cfdict,&timerId,pEvent);
        [theLock unlock];
    }

}

I tried declaring it in 'init' method also. How can i make

CFDictionaryValueCallBacks cbs = {0,NULL,NULL,NULL,NULL};   
CFMutableDictionaryRef cfdict = CFDictionaryCreateMutable(NULL,0,&kCFTypeDictionaryKeyCallBacks,&cbs);

common to both the methods..

Upvotes: 0

Views: 149

Answers (1)

sergio
sergio

Reputation: 69027

Any object that is referenced both by CreateTimer and KillTimer should be declared in the "interface" part of your class (usually RSTimer.h, which should be imported by the implementation file).

Like this:

interface RSTimer {
 CFDictionaryValueCallBacks cbs;
 CFMutableDictionaryRef cfdict;
}
- (void)init;

You need to define your init method in order to initialize your members. (You have not provided any detail about which is RSTimer base class, so I assume NSObject)

Upvotes: 2

Related Questions