アプリでカウンターを作成しています。特定の変更が発生したときにインクリメントしたい。カウンターはその値を0から1に増やします。その後、1に固執し、さらに増加しません。これは私のコードです。.hi でこれを行いました。
@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;
}
}
}