以下代码在我使用swift 2.2发布和更新的应用程序中运行良好.我只是迁移到swift 3,现在我得到以下编译时错误; "使用appendingPathComponent"与行的模糊使用:
let PDFPathFileName = documentDirectory.appendingPathComponent(fileName as String)
Run Code Online (Sandbox Code Playgroud)
在这:
func returnPDFPath() -> String {
let path:NSArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
let documentDirectory: AnyObject = path.object(at: 0) as AnyObject
let PDFPathFileName = documentDirectory.appendingPathComponent(fileName as String)
return PDFPathFileName
}
@IBAction func reveiwPDFSendCliked(_ sender: AnyObject) {
let pdfPathWithFileName = returnPDFPath()
generatePDFs(pdfPathWithFileName)
}
Run Code Online (Sandbox Code Playgroud)
此代码负责将文件路径返回到documentDirectory,该文件路径将在用户单击查看并保存PDF按钮时用于保存PDF文件.任何建议将不胜感激.
appendingPathComponent方法是一种方法NSString,而不是AnyObject.
改变这一行:
let documentDirectory: AnyObject = path.object(at: 0) as AnyObject
Run Code Online (Sandbox Code Playgroud)
至:
let documentDirectory = path.object(at: 0) as! NSString
Run Code Online (Sandbox Code Playgroud)
但是你应该尝试尽可能多地使用适当的类型.
试试这个:
func returnPDFPath() -> String {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentDirectory = path.first! as NSString
let PDFPathFileName = documentDirectory.appendingPathComponent(fileName as String)
return PDFPathFileName
}
Run Code Online (Sandbox Code Playgroud)
此代码假定path至少有一个值(应该是).
| 归档时间: |
|
| 查看次数: |
4057 次 |
| 最近记录: |