简单的写入文件对我来说不起作用
我无法将这个简单的字符串写入文件。 请看我的代码。这是一个带有 NSTextField
和一个按钮的简单笔尖。日志显示我得到的字符串值很好,但它没有写入。
//Quick.h
#import <Foundation/Foundation.h>
@interface Quick : NSObject
{
IBOutlet NSTextField * aString;
}
-(IBAction)wButton:(id)sender;
@end
//Quick.m
#import "Quick.h"
@implementation Quick
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
-(IBAction)wButton:(id)sender{
NSString * zStr = [aString stringValue];
NSString * path = @"data.txt";
NSLog(@"test String %@", zStr);
[zStr writeToFile:path
atomically:YES
encoding:NSASCIIStringEncoding
error:nil];
}
@end
I cannot get this simple string to write to a file.
Please look at my code. This is a simple nib with an NSTextField
and a button. The log shows I am getting the string value fine, but it does not write.
//Quick.h
#import <Foundation/Foundation.h>
@interface Quick : NSObject
{
IBOutlet NSTextField * aString;
}
-(IBAction)wButton:(id)sender;
@end
//Quick.m
#import "Quick.h"
@implementation Quick
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
-(IBAction)wButton:(id)sender{
NSString * zStr = [aString stringValue];
NSString * path = @"data.txt";
NSLog(@"test String %@", zStr);
[zStr writeToFile:path
atomically:YES
encoding:NSASCIIStringEncoding
error:nil];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须提供很可能是 Documents 目录的完整文件路径,而不仅仅是文件名。
You have to provide an entire file path to most likely the Documents directory, not just a file name.
您需要提供绝对路径。
此代码来自 另一个答案< /a> 为您提供应用程序文档目录的路径:
You need to provide an absolute path.
This code from another answer gives you the path to the documents directory of your app: