доступ к свойству другого класса из объекта NSOperation - PullRequest
0 голосов
/ 30 января 2011

Уважаемое сообщество. У меня есть подкласс NSOperation со свойством:

@property(readwrite, getter=isCancelled) BOOL cancelled;

откуда я был создан объект подкласса NSObject (из init):

database = [[[MySQLIXC alloc] initWithQuene:iQuene andCarrier:startForCarrier withState:cancelled] retain];

В этом пользовательском объекте я пытаюсь объявить локальный iVar:

@interface MySQLIXC :  NSObject {
       BOOL _currentQueueStatus;

В init:

- (id)initWithQuene:(NSUInteger)quene andCarrier:(NSString *)carrierName withState:(BOOL)currentQueueStatus;
    _currentQueueStatus = currentQueueStatus;

Но currentQueueStatus всегда null. Может кто-нибудь предложить проблемное местоположение?

1 Ответ

1 голос
/ 30 января 2011

1) вам следует избегать повторного объявления реализованного интерфейса подкласса для ваших дополнений (например, -[NSOperation isCancelled] существует)

2) очень необычно начинать с двух оставшихся отсчетов:

database = [[MySQLIXC alloc] initWithQuene:iQuene andCarrier:startForCarrier withState:cancelled];
[otherThingThatHoldsAReference setDatabase:database];

вместо:

database = [[[MySQLIXC alloc] initWithQuene:iQuene andCarrier:startForCarrier withState:cancelled] retain];

3) _currentQueueStatus - это не null, это BOOL, то есть signed char. должно быть 0 (NO) или 1 (YES).

4) что такое currentQueueStatus? больше кода поможет вам получить более конкретные ответы.

РЕДАКТИРОВАТЬ: обновлено для уточнения в ответ на комментарии

/* things would look different if you subclass MONOperation */
@interface MONOperation : NSOperation
{
@private
    MySQLIXC * sqlIxc;
    BOOL didCancelDatabaseRequest;
}

/*
do not use isCancelled for your name - NSOperation declares this method, and
its implementation is well defined. this would override the implementation
and likely cause runtime errors.

specifically, NSOperation/NSOperationQueue uses cancel and isCancelled in its
interface and state. if you must cancel, then call cancel from cancelDatabaseRequest.
you may override cancel, but call [super cancel] in your implementation.
*/
@property (readonly) BOOL didCancelDatabaseRequest;
@property (retain) MySQLIXC * sqlIxc;

@end

@implementation MONOperation

/* ... */

- (BOOL)didCancelDatabaseRequest
{
    return didCancelDatabaseRequest;
}

- (void)cancelDatabaseRequest /* in this example, there is no sense making cancelDatabaseRequest publicly visible */
{
/* for example */
    assert(self.sqlIxc);
    self.sqlIxc = nil;
    didCancelDatabaseRequest = YES;
    [self cancel]; /* do this rather than overriding NSOperation's cancellation interfaces in your subclass */
}

- (void)main
{
    NSAutoreleasePool * pool = [NSAutoreleasePool new];
    assert(self.sqlIxc);
    self.sqlIxc.doStuff;
    if (self.sqlIxc.didTimeout || self.sqlIxc.couldNotAccessFile) {
        [self cancelDatabaseRequest];
    }
    else {
    /* use the result */
    }
    [pool release];
}

@end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...