我对Swift和iOS开发一般都很新。我的应用程序有一个模型可以很容易地表示为逗号分隔值(csv),所以我自然希望用户能够将数据导出为csv文件并在另一个应用程序中打开该文件。由于我在Swift中没有找到任何示例,我自己试了一下:
func ExportToCSV(delegate: UIDocumentInteractionControllerDelegate){
let fileName = NSTemporaryDirectory().stringByAppendingPathComponent("myFile.csv")
let url: NSURL! = NSURL(fileURLWithPath: fileName)
var data = "Date,Time,Count\n2014-11-21,14.00,42"
data.writeToURL(url, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
if url != nil {
let docController = UIDocumentInteractionController(URL: url)
docController.UTI = "public.comma-separated-values-text"
docController.delegate = delegate
docController.presentPreviewAnimated(true)
}
}
(delegate参数是调用函数的视图,如MyClass.ExportToCSV(self)
)
这主要是有效的,我看到以下观点:
但是,在模拟器中,我收到以下警告:
Unbalanced calls to begin/end appearance transitions for <QLRemotePreviewContentController: 0x7fcd720da800>.
以及
Unknown activity items supplied: ("<QLPrintPageRenderer: 0x7fcd73861ee0>","<UIPrintInfo: 0x7fcd714b9030>")
当我点击动作按钮时,过了一会儿
Communications error: <OS_xpc_error: <error: 0x10e032b10> {
count = 1, contents = "XPCErrorDescription"
=> <string: 0x10e032f18> { length = 22, contents = "Connection interrupted" }
}>
当我点击 Mail 时出现以下错误:
viewServiceDidTerminateWithError: Error Domain=_UIViewServiceInterfaceErrorDomain
Code=3 "The operation couldn’t be completed. (_UIViewServiceInterfaceErrorDomain error 3.)"
UserInfo=0x7fcd71631460 {Message=Service Connection Interrupted}
<MFMailComposeRemoteViewController: 0x7fcd73864aa0> timed out waiting for fence
barrier from com.apple.MailCompositionService
虽然在实际设备上一切都按计划进行,但是很多错误都让我感到很失望。有更好的解决方案吗?
答案 0 :(得分:1)
这可能是由于邮件或打印应用程序访问对象时已经解除分配的对象。尝试将docController
和url
变量声明为类属性,以便只要视图控制器存在,它们就会保持活动状态。