2012-05-10 8 views
-1

私は自分のアプリでカウンタを作成しています。特定の変更が発生したときに増分したい。カウンタは値を0から1にインクリメントし、1に固定してさらにインクリメントしません。これは私のコードです。私はこれをやった。NSintegerのアドレスはインクリメントして変更されます

@interface URLCacheConnection : NSObject { 
    int      counter; 

}

との.mにそれが

-(void) connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response { 
if ([response isKindOfClass:[NSHTTPURLResponse self]]) { 

    printf("[(NSHTTPURLResponse *)response allHeaderFields] = %s\n", [[(NSHTTPURLResponse *)response suggestedFilename] cStringUsingEncoding: 1]); 
    NSDictionary *dict = [(NSHTTPURLResponse *)response allHeaderFields]; 
    NSLog(@"allHeaderFields = %@\n", [dict description]); 

    if(downloadItem.downloadStatus != DownloadStatus_resumed) 
    { 
     if([[NSFileManager defaultManager] fileExistsAtPath: downloadItem.fileItem.filePath]) { 
      counter = counter + 1; 
      NSLog(@"the value of counter is %d", counter); 

      NSString * fileExtension = [downloadItem.fileItem.fileName pathExtension]; 

      NSString *fileName = [downloadItem.fileItem.fileName stringByDeletingPathExtension]; 

      fileName = [fileName stringByAppendingFormat:@"-(%d)", counter]; 
      fileName = [fileName stringByAppendingString:[NSString stringWithFormat:@".%@",fileExtension]]; 

      downloadItem.fileItem.fileName = fileName; 

      downloadItem.fileItem.filePath = [NSString stringWithFormat:@"%@/%@", downloadItem.fileItem.folderPath, downloadItem.fileItem.fileName]; 
     } 
     else { 
      counter = 0; 
     } 
     BOOL fileCreatedFlag = [[NSFileManager defaultManager] createFileAtPath: downloadItem.fileItem.filePath contents:nil attributes:nil]; 
     downloadItem.fileItem.fileHandle = [NSFileHandle fileHandleForWritingAtPath: downloadItem.fileItem.filePath]; 
     fileCreatedFlag = fileCreatedFlag; 

    } 

} 

}

+0

あなたは、NSLog(@のカウンタのアドレスは%p "、&self.counter); ' – keety

+0

を削除しましたが、機能しません。最初にNSLogを実行すると、[カウンタのアドレスは0x919e81c]になり、2度目は[カウンタのアドレスは0x85a8b2c] –

+0

になります**すべての**コード、好ましくはスタンドアロンの関数またはクラス私たちは自分自身を試すことができる。今はコードの一部だけを提供しており、実際に何をしているのかを推測しようとすると混乱しています。 –

答えて

3

ジェームズを持って、それは別のアドレスを出力しますので、self.counterが値ではなく、変数に戻りますので、これがあります。
上記のコメントにKeetyのように正確なログとログを使用してください。

+0

を与えました。コード –

+0

をいくつか書きました。 –

0

これを試してみてください:ダウンロードステータスが再開される

NSInteger counter = 0; 
counter = counter+1; 
NSLog(@"the address of counter is %p",counter); 
+2

これは、そのアドレスではなく、「カウンタ」の**値**を表示します。 –

+0

'NSInteger'はObjective-Cオブジェクトではありません。 [documentation](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#//apple_ref/doc/uid/20000018-SW16)を確認してください。 。あなたは 'NSNumber'を考えています。 –

1

ならば、あなたは0にカウンターをリセットし、引数のために、各ダウンロードが少なくとも一度再開され、場合、カウンタは常に1ときになりますそれをログに出力します。

+0

同じファイルが複数回ダウンロードされた場合、別の名前で保存されるようにカウンタ値にファイル名を追加するので、カウンタを使用しています。 –

関連する問題