2011-12-15 2 views
0

を比較するファイルは、それのサイズを変更した場合、私は(私の場合、私はタイマーを選んだ)ループを確認したいです。 は、私は次のようにしますそうするには、次のNSStringのは、私は、現在のMac OS Xに</p> <p>を比較する文字列に苦しんだNSTimer「ループ」に

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    fileSize = [[NSString stringWithFormat:@""] retain]; 
    fileSizeSrc = [[NSString stringWithFormat:@""] retain]; 

    fileManager = [NSFileManager defaultManager]; 
    fileAtts = [fileManager attributesOfItemAtPath:@"~/Desktop/test.txt" error:NULL]; 
    fileSizeSrc = [NSString stringWithFormat:@"%llu", [fileAtts fileSize]]; 
    [self startWatching:nil]; 
} 
- (void)startWatching:(id)sender 
{ 
    timer = [NSTimer scheduledTimerWithTimeInterval:2 
              target:self 
              selector:@selector(checkForUpdates) 
              userInfo:nil 
              repeats:YES]; 
    [timer fire]; 
} 
- (void)checkForUpdates 
{ 
    fileAtts = [fileManager attributesOfItemAtPath:@"~/Desktop/test.txt" error:NULL]; 
    fileSize = [NSString stringWithFormat:@"%llu", [fileAtts fileSize]]; 
    if([fileSize isEqualToString:fileSizeSrc]) 
    { 
     NSLog(@"%@", fileSize); 
     fileSizeSrc = [NSString stringWithFormat:@"%@", fileSizeSrc]; 
    } 
} 
- (void)applicationWillTerminate:(NSNotification *)notification 
{ 
    [timer invalidate]; 
    timer = nil; 
    [fileSize release]; 
    [fileSizeSrc release]; 
} 

私はちょうどのサイズを一度に確認することができます。 私のコードの実行時(セレクタが二度目の発射があったとき)だから、それは私のプログラムがEXC_BAD_ACCESS

で終了し、私はif(![fileSize isEqualToString:fileSizeSrc])に変更している場合()文を反転しようとすると、後で私に出力し、2秒を与えます起動直後に終了します。

問題が最初に発生した後、私はそれが私に起こったので、私はこれをしなければならないので、文字列を保持してから解放しました。

誰かが私を助けてくれることを願っています! ありがとう、 Greets、Julian。

答えて

0

ます(iOS5をで利用可能)カウンタを自動保持して使用しない場合:

あなたは(簡単に)あなたの情報を保持するか、それを自分が保持されますプロパティを作成する必要があります。

self.fileSizeSrc = [NSString stringWithFormat:@"%llu", [fileAtts fileSize]]; 

とさらに2つの事を::

  1. dont'tがあれば-(void) dealloc
  2. にnilにプロパティを設定するのを忘れ、あなたが書く必要があるプロパティを設定するために、

    @interface ... 
    { 
        NSString* _fileSizeSrc; 
    } 
    
    @property (retain)NSString* fileSizeSrc; 
    
    @end 
    
    @implementation ... 
    @synthesise fileSizeSrc = _fileSizeSrc; 
    
    
    ... 
    
    
    @end 
    

    プロパティを作成します。変数を直接使用しないでください。

関連する問題