Tag: 文件

Bundle.main.path(forResource:ofType:inDirectory :)返回nil

尽量不要大笑或者哭泣 – 我只是在20年之后才开始重新编码。 我花了超过4个小时看引用和尝试代码片断来获取Bundle.main.path打开我的文本文件,所以我可以读取我的应用程序的数据(我的下一步是适当地parsing它)。 if let filepath = Bundle.main.path(forResource: "newTest", ofType: "txt") { do { let contents = try String(contentsOfFile: filepath) print(contents) } catch { print("Contents could not be loaded.") } } else { print("newTest.txt not found.") } 结果是:“找不到newTest.txt”。 不pipe我如何将文件拖放到项目中,在Xcode中创build文件或使用文件 – >添加文件到…菜单项。

iOS:无法将文件保存到“应用程序支持”文件夹,但可以将“文档”

我可以使用自定义名称下载并保存二进制文件到“文档”文件夹中。 如果我只是将URL更改为“应用程序支持”文件夹,而不是“文档”文件夹,则无法写入该URL,表示该URL不存在。 以下是url构build代码: – ( NSURL * ) getSaveFolder { NSURL * appSupportDir = nil; NSURL * appDirectory = nil; NSArray * possibleURLs = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSAllDomainsMask]; if ( [possibleURLs count] >= 1 ) { appSupportDir = [possibleURLs objectAtIndex:0]; } if ( appSupportDir != nil) { NSString * appBundleID = [[NSBundle mainBundle] bundleIdentifier]; appDirectory […]

获取iOS应用程序的“〜/ Library”path

这个苹果技术说明: http://developer.apple.com/library/ios/#qa/qa2010/qa1699.html build议将“内部”用户文档存储在~/Library的子目录中。 但我无法find一个预先创build的search域,这将使我这个。 什么是最好/最正确/最不可能打破这条道路的方式? 谢谢。

iPhone:获取资源文件夹子文件夹内的文件path

我是iPhone编程新手。 我想读取位于资源文件夹的子文件夹中的文本文件的内容。 资源文件夹结构如下所示: 资源 文件夹1 —-> DATA.TXT 文件夹2 —-> DATA.TXT Folder3 —-> —- Folder1中> DATA.TXT 有多个名为“Data.txt”的文件,所以如何访问每个文件夹中的文件? 我知道如何阅读文本文件,但如果资源结构类似于上述结构,那么我怎么能得到的path? 例如,如果我想从Folder3访问“Data.txt”文件,如何获取文件path? 请build议。

NSURLConnection下载大文件(> 40MB)

我需要从服务器下载大文件(即大于40MB)到我的应用程序,这个文件将是ZIP或PDF。 我使用NSURLConnection实现它,如果文件较小,则工作正常。否则,下载部分填充并停止执行。 例如我试图下载36MB的PDF文件,但只下载了16MB。 请帮我知道原因? 如何解决它? FYI:实施文件 #import "ZipHandlerV1ViewController.h" @implementation ZipHandlerV1ViewController – (void)dealloc { [super dealloc]; } – (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } – (void)viewDidLoad { UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)]; [mainView setBackgroundColor:[UIColor darkGrayColor]]; UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [downloadButton setFrame:CGRectMake(50, 50, 150, 50)]; [downloadButton setTitle:@"Download File" forState:UIControlStateNormal]; [downloadButton addTarget:self action:@selector(downloadFileFromURL:) forControlEvents:UIControlEventTouchUpInside]; […]

iOS:如何find文件的创builddate?

我试图find一个文件的创builddate(不修改date)。 创builddate似乎不在文件的属性中,尽pipe修改date是。 我正在使用这个代码 NSFileManager* fm = [NSFileManager defaultManager]; NSString* path = [PathHelpers pathInDocumentsFolderWithFilename:FILE_NAME]; NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil]; if (attrs != nil) { return (NSDate*)[attrs objectForKey: NSFileCreationDate]; } else { return nil; } 这总是返回零。 在debugging器中键入“po attrs”以获取NSDictionary中的键/值对列表返回以下内容.. NSFileGroupOwnerAccountID = 20; NSFileGroupOwnerAccountName = staff; NSFileModificationDate = 2010-01-21 11:47:55 +0000; NSFileOwnerAccountID = 501; NSFileOwnerAccountName = ben; NSFilePosixPermissions […]

如何在swift下载文件?

我刚刚开始学习iOS的苹果swift编程来自android。 我现在基本上可以读取和操作swift代码,并且学习了一些在iOS Swift编程中使用的常用类,但仍然对语法和一切都有一些困惑。 我正试图下载文件。 就像我们刚刚说的,来自这个URL var url = "http://www.mywebsite.com/myfile.pdf" 在button点击。 也许还有视觉上的进步 通过在这里searchstackoverflow,我偶然发现了Alamofire。 我可能会尝试,但我不知道这是否是我做这件事的最好方法。 所以,我想问一下我的select(iOS7和iOS8)在实现我的目标方面是怎样的,什么是我的select。 此外,利弊将是可怕的!

在iOS上写一个文件

我如何在iOS上编写文件? 我试图用下面的代码做,但我做错了什么: char *saves = "abcd"; NSData *data = [[NSData alloc] initWithBytes:saves length:4]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyFile"]; [data writeToFile:appFile atomically:YES]; 我在资源上创build了MyFile.txt。