我正在我的应用程序中创建一个计数器。我想在发生特定更改时增加它。计数器将它的值从 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;
}
}
}