Programming in Objective-C 2.0中的部分错误修正
在阅读“Programming in Objective-C 2.0”的过程中,发现一些程序示例代码中存在着废弃的API调用。我把它修改成了新版API的版本(Leopard+),如下:
示例代码16.1 原书P380
#import <Foundation/NSObject.h> #import <Foundation/NSFileManager.h> #import <Foundation/NSDictionary.h> #import <Foundation/NSString.h> #import <Foundation/NSAutoreleasePool.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString *fName = @"/Users/venj/Desktop/testfile"; NSFileManager *fm; NSDictionary *attr; fm = [NSFileManager defaultManager]; if ([fm fileExistsAtPath:fName] == NO) { NSLog(@"File doesn't exist!"); return 1; } if ([fm copyItemAtPath:fName toPath:@"/Users/venj/Desktop/newfile" error:NULL] == NO) { NSLog(@"File copy failed!"); return 2; } if ([fm contentsEqualAtPath:fName andPath:@"/Users/venj/Desktop/newfile"] == NO) { NSLog(@"Files are not equal!"); return 3; } if ([fm moveItemAtPath:@"/Users/venj/Desktop/newfile" toPath:@"/Users/venj/Desktop/newfile2" error: NULL] == NO) { NSLog(@"File rename failed!"); return 4; } if ((attr = [fm attributesOfItemAtPath:@"/Users/venj/Desktop/newfile2" error:NULL]) == nil) { NSLog(@"Could not get file attributes!"); return 5; } if ([fm removeItemAtPath:fName error: NULL] == NO) { NSLog(@"File remove failed!"); return 6; } NSLog(@"%@", [NSString stringWithContentsOfFile:@"/Users/venj/Desktop/newfile2" encoding: NSUTF8StringEncoding error: NULL]); for(NSString *key in attr) NSLog(@"%@ => %@", key, [attr objectForKey: key]); [pool drain]; return 0; }
示例代码16.3 原书P385
#import <Foundation/NSObject.h> #import <Foundation/NSFileManager.h> #import <Foundation/NSString.h> #import <Foundation/NSAutoreleasePool.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString *dirName = @"/Users/venj/Desktop/testdir"; NSString *path; NSFileManager *fm; fm = [NSFileManager defaultManager]; path = [fm currentDirectoryPath]; NSLog(@"Current directory path is %@.", path); if ([fm createDirectoryAtPath:dirName withIntermediateDirectories:NO attributes:nil error: NULL] == NO) { NSLog(@"Could not create directory!"); return 1; } if ([fm moveItemAtPath:dirName toPath:@"/Users/venj/Desktop/newdir" error: NULL] == NO) { NSLog(@"Directory rename failed!"); return 2; } if ([fm changeCurrentDirectoryPath:@"/Users/venj/Desktop/newdir"] == NO) { NSLog(@"Change directory failed!"); return 3; } path = [fm currentDirectoryPath]; NSLog(@"Current directory path is %@.", path); NSLog(@"All operations were successful!"); [pool drain]; return 0; }
示例代码16.6
#import <Foundation/NSObject.h> #import <Foundation/NSFileManager.h> #import <Foundation/NSString.h> #import <Foundation/NSArray.h> #import <Foundation/NSAutoreleasePool.h> #import <Foundation/NSPathUtilities.h> #import <Foundation/NSProcessInfo.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSFileManager *fm; NSString *source, *dest; BOOL isDir; NSProcessInfo *proc = [NSProcessInfo processInfo]; NSArray *args = [proc arguments]; fm = [NSFileManager defaultManager]; if ([args count] != 3) { NSLog(@"Usage %@ src dest", [proc processName]); return 1; } source = [args objectAtIndex:1]; dest = [args objectAtIndex:2]; if ([fm isReadableFileAtPath:source] == NO) { NSLog(@"Can't read %@", source); return 2; } [fm fileExistsAtPath:dest isDirectory: &isDir]; if (isDir == YES) { dest = [dest stringByAppendingPathComponent:[source lastPathComponent]]; } [fm removeItemAtPath:dest error:NULL]; if ([fm copyItemAtPath: source toPath: dest error:NULL] == NO) { NSLog(@"Copy failed!"); return 3; } NSLog(@"Copy of %@ to %@ successed!", source, dest); [pool drain]; return 0; }
page revision: 1, last edited: 25 May 2010 13:57