2011-01-30 6 views
0

親愛なるコミュニティ。 私は、プロパティとNSOperationのサブクラスがあります。私は、サブクラスのオブジェクトを作成したところからNSOperationオブジェクトの別のクラスのプロパティへのアクセス

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

をNSObjectの(INITから):このカスタムオブジェクトで

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

私は地元のIVAR宣言しよう:

をINITで
@interface MySQLIXC : NSObject { 
     BOOL _currentQueueStatus; 

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

しかし、currentQueueStatusは常にnullです。 誰かが問題のある場所を提案できますか?

+0

に応じて明確化のための更新?追加したコードを表示してください。 –

+0

時間のためのTnx、私はアップデートの質問でした – Alex

+0

あなたはまだあなたがそれを宣言しているコードを表示していません。そのコードを表示してください。 –

答えて

1

1)あなたはカウントを保持2で開始することは非常にまれである)

2(例えば-[NSOperation isCancelled]が存在している)あなたの追加の再宣言実装サブクラスインタフェースを回避する必要があります。

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です。 0NO)または1YES)である必要があります。

4)とは何ですかcurrentQueueStatus?より多くのコードは、より具体的な回答を得るのに役立ちます。

EDIT:コメントは具体的には、あなたが `_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 
+0

あなたの答えのためのThxを助言してください:1)これは新しい宣言です、私はそれが正しい@プロパティ(readwrite)BOOLキャンセル希望です。 2)私のMySQLIXC *データベースは、サブクラスNSOperationの@interfaceで宣言されました。 MySQLIXC * database2の他のプロパティ(retainまたはassign?)を宣言し、uの例で[self setDatabase2:database]を作成する必要がありますか? 3)私はnullではないことを理解していますが、NSLog(@ "MYSQL:queue status:%@"、_ currentQueueStatus)で表示されます。 4)私は更新に関する質問でした。 – Alex

+0

あなたはようこそ1)それはNSOperationの実装を置き換えます。あなたは 'isDatabaseRequestCancelled'のような別の名前を選択する必要があります。これは、(NSOperation/NSOperationQueueの実装は非常に混乱する)2)それを保持する3)あなたのNSLogの呼び出しは、コンパイラの警告を生成する必要が発生するのを待ってクラッシュです。そうでなければ、コンパイラの警告を出す - %@書式指定子を介してobjcオブジェクトとして署名されたcharを渡すことは、起こるのを待っている悪いアクセスです。 – justin

+0

1)このNSOperationサブクラスの宣言は、私が意味しています。私はまだ別の名前を選ぶ必要がありますか?それが本当なら、私はこの属性(開始、キャンセル、実行、一時停止、終了)を設定できません。もしあなたがサブクラスを意味するなら、私は現在の状態をキャンセルしようとしました。クラスでは、私はこの状態のためのinitで別の宣言を持っています:_currentQueueStatus = currentQueueStatus;私が編集した例を見てもわかるように。 – Alex

関連する問題