最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中如何创建文件夹

Swift crifan 2800浏览 0评论

写成:

fileManager.createDirectoryAtPath(url_conversationItemList.path!, withIntermediateDirectories: true, attributes: nil)

但是出错:

Call can throw, but it is not marked with ‘try’ and the error is not handled

改为:

            let createDirOK = try fileManager.createDirectoryAtPath(url_conversationItemList.path!, withIntermediateDirectories: true, attributes: nil){
            }

结果:Extra argument ‘attributes’ in call

搜:

swift NSFileManager createDirectoryAtPath

ios – Using NSFileManager and createDirectoryAtPath in Swift – Stack Overflow

【总结】

写成:

            do{
                print("try create dir \(url_conversationItemList.path!)")
                try fileManager.createDirectoryAtPath(url_conversationItemList.path!, withIntermediateDirectories: true, attributes: nil)
            }catch let error as NSError{
                print("create dir \(url_conversationItemList.path!) error \(error.localizedDescription)")
                return
            }

即可。

其中:

            do{
                try SomeThingThatThrowable
            }catch let error as NSError{
                print("error \(error.localizedDescription)")
            }

转载请注明:在路上 » [已解决]swift中如何创建文件夹

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.186 seconds, using 22.12MB memory