《文件操作与“WhatAboutThatFile?”应用开发》
立即解锁
发布时间: 2025-08-25 02:21:27 阅读量: 2 订阅数: 8 


Mac上学习Cocoa:从零开始的全面指南
### 《文件操作与 “What About That File?” 应用开发》
在开发应用程序时,文件操作是一项基础且重要的任务。本文将介绍一些常见的文件操作方法,以及如何创建一个名为 “What About That File?” 的应用程序,该应用程序允许用户选择文件并查看文件的相关信息和内容。
#### 1. 常见文件操作类及方法
在开发中,有几个常用的类用于文件操作:
- **NSPropertyListSerialization**:用于处理属性列表格式。如果需要以通用方式解析属性列表,并进行完整的错误报告和更多控制,可以使用其类方法 `propertyListWithData:options:format:error:`。
- **NSData**:提供最通用的文件访问方式。它可以从磁盘读取任何类型的数据,并将其表示为字节数组供我们使用,是处理二进制数据的理想方式。还可以选择将文件映射到虚拟内存,示例代码如下:
```objc
NSData *myData = [NSData dataWithContentsOfFile:@"/path/to/something"
options:NSDataReadingMappedIfSafe error: &myError];
```
- **文件写入方法**:提到的类都包含 `writeToFile:atomically:` 方法,`NSString` 版本的该方法已弃用,建议使用 `writeToFile:atomically:encoding:error:`,该方法要求指定文本编码并可检查错误。`NSData` 提供了类似的 `writeToFile:options:error:` 方法。
#### 2. “What About That File?” 应用开发
##### 2.1 创建项目
使用 Xcode 创建一个新的 Cocoa 应用程序,命名为 `WhatAboutThatFile`,确保启用 ARC 并为项目分配类前缀 `WAT`。这将创建一个简单的项目,包含 `WATAppDelegate` 类和 `MainMenu.xib` 文件。
##### 2.2 代码实现
###### 2.2.1 `WATAppDelegate.h` 头文件
该文件声明了大量用于通过 Cocoa 绑定访问值的属性,以及一个让用户从菜单打开文件的操作方法。
```objc
//
// WATAppDelegate.h
//
#import <Cocoa/Cocoa.h>
@interface WATAppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (strong) NSFileWrapper *fileWrapper;
@property (strong) NSURL *fileURL;
@property (assign) NSStringEncoding chosenEncoding;
@property (readonly) NSDictionary *fileAttributes;
@property (readonly) NSString *filename;
@property (readonly) NSImage *fileIcon;
@property (readonly) NSImage *opensAppIcon;
@property (readonly) NSString *opensAppName;
@property (weak) NSString *stringEncodingName;
@property (readonly) NSString *fileStringValue;
@property (readonly) NSDictionary *encodingNames;
- (IBAction)chooseFile:(id)sender;
@end
```
###### 2.2.2 `.m` 文件
- **`chooseFile:` 方法**:使用 `NSOpenPanel` 类让用户选择要检查的文件。如果用户选择了文件,会设置实例变量,并进行错误处理。
```objc
- (IBAction)chooseFile:(id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseFiles:YES];
[openPanel setCanChooseDirectories:NO];
[openPanel setResolvesAliases:NO];
[openPanel setAllowsMultipleSelection:NO];
if ([openPanel runModal] == NSFileHandlingPanelOKButton) {
self.chosenEncoding = 0;
self.fileURL = [openPanel URL];
NSError *fileError;
self.fileWrapper = [[NSFileWrapper alloc] initWithURL:self.fileURL
options:0
error:&fileError];
if (!self.fileWrapper) {
NSRunAlertPanel(@"Couldn't access file",
[fileError localizedDescription], nil, nil, nil);
}
}
}
```
- **`filename` 和 `fileIcon` 方法**:通过 Cocoa 绑定读取,使用 `keyPathsForValuesAffectingFilename` 和 `keyPathsForValuesAffectingFileIcon` 方法确保值更新时视图自动重新加载内容。
```objc
+ (NSSet *)keyPathsForValuesAffectingFilename
{
return [NSSet setWithObjects:@"fileURL", @"fileWrapper", nil];
}
- (NSString *)filename
{
return [self.fileWrapper filename];
}
+ (NSSet *)keyPathsForValuesAffectingFileIcon
{
return [NSSet setWithObjects:@"fileURL", @"fileWrapper", nil];
}
- (NSImage *)fileIcon
{
return [self.fileWrapper icon];
}
```
- **`opensAppName` 和 `opensAppIcon` 方法**:使用 `NSWorkspace` 类获取默认打开文件的应用程序的名称和图标,同样使用 `keyPathsForValuesAffectingXxx` 方法确保值更新。
```objc
+ (NSSet *)ke
```
0
0
复制全文
相关推荐










