Ios 找不到CSV文件?
我正试图建立一个应用程序,这是一个调查,保存文本填充数据到一个CSV文件,然后可以在以后的日期通过电子邮件发送。 我在网上学习了一些教程,并开发了一个可以运行的应用程序,但是有一些问题,请参见下面我的代码 在视图中Ios 找不到CSV文件?,ios,objective-c,cocoa-touch,csv,Ios,Objective C,Cocoa Touch,Csv,我正试图建立一个应用程序,这是一个调查,保存文本填充数据到一个CSV文件,然后可以在以后的日期通过电子邮件发送。 我在网上学习了一些教程,并开发了一个可以运行的应用程序,但是有一些问题,请参见下面我的代码 在视图中 #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize first; @synthesize seconds; @synthe
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize first;
@synthesize seconds;
@synthesize tel;
@synthesize resultview;
- (IBAction)retractkeyboard:(id)sender{
[self resignFirstResponder];
}
-(IBAction)saveinfo:(id)sender{
NSString *resultLine=[NSString stringWithFormat:@"%@,%@,%@\n",
self.first.text,
self.seconds.text,
self.tel.text];
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//resultview.text = docPath;
NSString *surveys=[docPath stringByAppendingPathComponent:@"results.csv"];
if (![[NSFileManager defaultManager] fileExistsAtPath:surveys]) {
[[NSFileManager defaultManager]
createFileAtPath:surveys contents:nil attributes:nil];
}
NSFileHandle *filehandle = [NSFileHandle fileHandleForUpdatingAtPath:surveys];
[filehandle seekToEndOfFile];
[filehandle writeData:[resultLine dataUsingEncoding:NSUTF8StringEncoding]];
[filehandle closeFile];
self.first.text=@"";
self.seconds.text=@"";
self.tel.text=@"";
NSLog(@"info saved");
}
-(IBAction)retrieveinfo:(id)sender{
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
//resultview.text = docPath;
NSString *surveys=[docPath stringByAppendingPathComponent:@"results.csv"];
if([[NSFileManager defaultManager] fileExistsAtPath: surveys])
{
NSFileHandle *filehandle = [NSFileHandle fileHandleForReadingAtPath:surveys];
NSString *surveyresults=[[NSString alloc] initWithData:[filehandle availableData] encoding:NSUTF8StringEncoding];
[filehandle closeFile];
self.resultview.text=surveyresults;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
并且在视图中。h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *first;
@property (strong, nonatomic) IBOutlet UITextField *seconds;
@property (strong, nonatomic) IBOutlet UITextField *tel;
-(IBAction)saveinfo:(id)sender;
-(IBAction)retrieveinfo:(id)sender;
-(IBAction)retractkeyboard:(id)sender;
@property (strong, nonatomic) IBOutlet UITextView *resultview;
@end
我的问题是:应用程序告诉我数据已保存到.csv文件,但我似乎在任何地方都找不到它?我甚至进入了mac的library文件夹等,但找到了除文件之外的所有东西,我知道它正在保存它,但不知道在哪里!加载函数也不起作用,但我觉得这是因为它无法加载文件,因为它不在那里。有什么帮助吗?您是否通过模拟器在Mac上运行此功能?你说是iOS。如果您在Sim卡上运行,您将在~/Library/Application Support/iPhone Simulator/X.X/Applications/XXX中找到这些文件/
其中,X.X是sim卡上运行的iOS版本,XXX是sim卡用来放置其文件的哈希值。您必须在文件夹中查找应用程序。如果您使用手机,请查看以下内容: 在plist中,必须添加一个属性: 应用程序支持iTunes文件共享
然后您可以从iTunes查看该文件。输入一些NSLog语句以查看文件的确切位置:
NSLog(@"surveys path: %@", surveys);
以下是错误:
Incorrect: `NSDocumentationDirectory`
Correct: `NSDocumentDirectory`
对于文件路径来说,surveys是一个糟糕的名称,它会导致混乱,像surveysPath这样的名称会更好
您可以使用调试器添加断点,跟踪执行并检查变量的值。调试是学习和精通的必要技能,除非您第一次能够100%地编写正确的代码
提示:最好的做法是不要重复自己,创建一个方法来返回路径,将文件名作为参数。当然,它只有几行,但只有一个地方可以更新
示例代码:
- (NSString *)documentPathForFileName:(NSString *)fileName {
NSString *documentFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentFolderPath stringByAppendingPathComponent:fileName];
return filePath;
}
是的,我在mac上使用i-phone模拟器,我已经找到了我的文件夹和应用程序等,但是我仍然找不到文本文件?其他的都在那里吗。。。?我以为它会出现在那里的文件里还是什么?这不是我的代码的错误吗?文档文件夹应该是:NSSearchPathForDirectories IndomainsDocument。。。您正在使用DocumentationFolder Documentation谢谢我找到了保存csv的位置,即2014-08-28 16:54:16.179测试[11669:60b]调查路径:/Users/davemckenzie/Library/Application Support/iPhone Simulator/7.1/Applications/993A4C49-705B-49CC-945D-B76C479F4B0D/Library/Documentation/results.csv但是我到了图书馆,却没有文档文件夹?只是缓存和首选项?