jjzjj

NSFileManager

全部标签

ios - 将代码从 Swift 1.2 转换为 Swift 2.0 以进行错误处理

swift1.2代码:varerror:NSError?=nilif(fileManager.removeItemAtPath(exportPathasString,error:&error)){//Error-handleifrequried}当我使用try和catchblock时,我无法将此代码编译为Swift2.0。Swift2.0代码do{check=tryfileManager.removeItemAtPath(exportPathasString)if(//somecondition){//whatever}}catch{check=nil} 最

ios - 如何将 UIPickerView Controller 中的图像或视频保存到文档目录?

处理选定的图像或视频:funcimagePickerController(picker:UIImagePickerController,didFinishPickingMediaWithInfoinfo:[String:AnyObject]){print("ok")ifletimage=info[UIImagePickerControllerOriginalImage]as?UIImage{//whattodotosavethatimage}else{//howtogetthevideoandsave}}保存到文档目录:letpath=try!NSFileManager.default

swift - 从 .plist 读取总是返回 nil

我所有从plist读取的尝试都返回了一个nil值,我在Xcode6和Xcodebeta7上以多种方式尝试过这个。此外,还有相当多的堆栈上有几个类似的问题,我已经尝试了很多,但没有一个能解决这个问题。我通过点击添加了我的words.plist:{myproject}>targets>buildphases>copyBundleResources然后我在我的ViewController中尝试了以下代码的几种变体:overridefuncviewDidLoad(){super.viewDidLoad()//Doanyadditionalsetupafterloadingtheview,typ

ios - 无法使用 Today Extension 的共享应用组共享数据

我正在尝试创建一个今天的扩展,它通过使用共享应用组容器显示来自父应用的数据,然后将持久存储添加到上下文中。添加今日扩展目标为父应用和扩展开启应用组并选择相同的组将TodayExtension添加为数据模型和实体的目标成员将持久存储添加到上下文中获取对象我没有收到任何错误,但扩展程序似乎没有获取任何结果。有人对我可能出错的地方有什么建议吗?这是我在TodayViewController扩展中所做的事情classTodayViewController:UIViewController,NCWidgetProviding{varcontext:NSManagedObjectContext!@

swift - FinderSync 扩展运行时错误 : The file couldn’t be opened because you don’t have permission to view it

我正在编写一个FinderSync扩展,将一个项目放在上下文菜单中。右键单击时,我希望菜单项的标题根据所选文件的内容进行更改。但是,当我尝试使用Data(contentsOf:selectedUrl)读取内容时,它会抛出以下错误:ErrorDomain=NSCocoaErrorDomainCode=257"Thefile“SomeFile.png”couldn’tbeopenedbecauseyoudon’thavepermissiontoviewit."UserInfo={NSFilePath=/Users/Shared/MySyncExtensionDocuments/SomeFi

swift - 'fileExists' 产生 'Bool' ,而不是预期的上下文结果类型 'Bool'

这行代码:if!FileManager.fileExists(atPath:documentDirectory.appendingPathComponent("newname.pdf"))出现错误提示'fileExists'produces'Bool',nottheexpectedcontextualresulttype'Bool'在Xcode9.2上。我错过了什么? 最佳答案 您需要在FileManager的实例上调用fileExists。它不是类方法。FileManager提供在大多数情况下使用的默认实例。if!FileMana

swift - 能够写入/读取文件但无法删除文件 SWIFT

我将.jpg图像存储在iOS文档目录中。我可以写入文件和读取文件,但是在删除它们时它说没有这样的文件但那不可能是因为我可以使用相同的url读取它。阅读:letpaths=NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)letpath=NSURL(fileURLWithPath:paths[0]asString)letfullPath=path.appendingPathComponent(info["pi"]as!String)letdata=NSData(contentsOf:fu

ios - fileManager.createFileAtPath 总是失败

我尝试调用fileManager.createFileAtPath方法,但总是失败。我的变量success始终为false。我在这里查看了一些类似的帖子,但没有一个完全符合我的需要。这是我的代码:pListPath=NSURL(fileURLWithPath:String(reportsPath)).URLByAppendingPathComponent("myReports.plist",isDirectory:false)letdata:NSData=NSData()varisDir:ObjCBool=falseiffileManager.fileExistsAtPath(Stri

swift - 使用 Swift 获取子目录

我试图只获取子目录。这是我的数据结构。根/一个/1.txt2.txtb/3.txt4.txt我想获取特定文件夹(根)中的目录。我在根文件路径中使用枚举器:letenumerator=NSFileManager.defaultManager().enumeratorAtPath(filePath)forelementinenumerator!{print(element)}但是,它会遍历根目录中的每个文件。我怎样才能得到子目录(a&b)? 最佳答案 不需要使用深度枚举。只需获取contentsOfDirectoryAtURL并过滤返回

swift - 文件 “xxx” 无法打开,因为没有这样的文件

这个问题在这里已经有了答案:UIImage(contentsOfFile:)returningnildespitefileexistingincachesdirectory[duplicate](1个回答)NSFileManager.defaultManager().fileExistsAtPathreturnsfalseinsteadoftrue(2个答案)关闭5年前。我想打开一个.txt文件并将内容保存为字符串。我试过这个:letpath=FileManager.default.urls(for:.documentDirectory,in:.userDomainMask)[0].a