私は自分のアプリでカウンタを作成しています。特定の変更が発生したときに増分したい。カウンタは値を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;
}
}
}
あなたは、NSLog(@のカウンタのアドレスは%p "、&self.counter); ' – keety
を削除しましたが、機能しません。最初にNSLogを実行すると、[カウンタのアドレスは0x919e81c]になり、2度目は[カウンタのアドレスは0x85a8b2c] –
になります**すべての**コード、好ましくはスタンドアロンの関数またはクラス私たちは自分自身を試すことができる。今はコードの一部だけを提供しており、実際に何をしているのかを推測しようとすると混乱しています。 –