【问题标题】:Take NSMutableString and convert to CSV file for email获取 NSMutableString 并转换为 CSV 文件以发送电子邮件
【发布时间】:2015-10-07 13:22:15
【问题描述】:

我正在尝试从UITableView(即var toDoItems:[String] = [])获取我的数据。我正在尝试获取这些数据并将其转换为 CSV 文件以发送电子邮件。我在 Xcode 7 和 Swift 2 中找不到任何有关这方面的最新信息。我发现的所有内容都是 Objective C。我是编程新手,所以尝试将 Objective C 转换为 Swift 2 对我来说很难。我和我的一个朋友谈过,他说要把我的UITableView 数据转换成NSMutableString。然后将我的NSMutableString 转换为NSData。他说NSData 可以写入 CSV 文件。

一段代码是:

// Helps to have it APPEAR when adding new items **
override func viewWillAppear(animated: Bool) {

    // Checks to see if there are any past items to boot up with
    if let storedtoDoItems : AnyObject = savedDefaults.objectForKey("toDoItems") {

        //If there are, it sets the array to nothing
        toDoItems = []

        for var i = 0; i < storedtoDoItems.count; ++i {
            toDoItems.append(storedtoDoItems[i] as! String)

            // Convert stored tableView data to string
            incomingString = String(toDoItems)
        }
    }
    tasksTable.reloadData()
}

我声明了以下变量:

var toDoItems:[String] = []
var convertString: NSString!
var convertMutable: NSMutableString!
var incomingString: String = ""
var datastring: NSString!

这是我的导出按钮操作:

// PDF Button
@IBAction func pdfExport(sender: AnyObject) {

    // Convert tableView String Data to NSMutableString if needed
    convertMutable = NSMutableString(UTF8String: incomingString)
    print("NSMutableString: \(convertMutable)")

    // Convert tableView String Data to NSString if needed
    convertString = String(incomingString)
    print("NSString: \(convertString)")

    // Convert above NSString to NSData
    let data = convertString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    if let d = data { // Unwrap since data is optional and print
        print("NSData: \(d)")
    }
    // Convert NSData back to NSString to verify NSData was actual data and test
    datastring = NSString(data:data!, encoding:NSUTF8StringEncoding) as! String
    print("Back to NSString: \(datastring)")

}

有人可以帮我举例说明如何使用 Swift 2 做到这一点吗?谢谢。

【问题讨论】:

    标签: uitableview swift2 xcode7 nsmutablestring


    【解决方案1】:

    所以做了一些研究并弄清楚了。

    我放的@IBAction func的代码:

    // CSV Export Button
    @IBAction func csvExport(sender: AnyObject) {
        // Convert tableView String Data to NSMutableString
        convertMutable = NSMutableString();
        for item in toDoItems
        {
            convertMutable.appendFormat("%@\r", item)
        }
    
        print("NSMutableString: \(convertMutable)")
    
        // Convert above NSMutableString to NSData
        let data = convertMutable.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        if let d = data { // Unwrap since data is optional and print
            print("NSData: \(d)")
        }
    
        //Email Functions
        func configuredMailComposeViewController() -> MFMailComposeViewController {
            let mailComposerVC = MFMailComposeViewController()
            mailComposerVC.mailComposeDelegate = self
            mailComposerVC.setSubject("CSV File Export")
            mailComposerVC.setMessageBody("", isHTML: false)
            mailComposerVC.addAttachmentData(data!, mimeType: "text/csv", fileName: "TodoList.csv")
    
            return mailComposerVC
        }
    
        // Compose Email
        let mailComposeViewController = configuredMailComposeViewController()
        if MFMailComposeViewController.canSendMail() {
            self.presentViewController(mailComposeViewController, animated: true, completion: nil)
        } else {
            self.showSendMailErrorAlert() // One of the MAIL functions
        }
    }
    

    我还为邮件添加了这两个功能:

    // Mail alert if user does not have email setup on device
    func showSendMailErrorAlert() {
        let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail.  Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
        sendMailErrorAlert.show()
    }
    // MARK: MFMailComposeViewControllerDelegate Method
    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
    

    它工作得很好,创建了 CSV 文件并将其附加到电子邮件中,打开邮件应用程序,准备好接收“收件人”地址。这适用于 Swift 2 和 Xcode 7。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-15
      • 2014-03-22
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多