将数据从iOS应用程序保存到CSV文件中,但找不到文件。

huangapple go评论48阅读模式
英文:

Saving data from iOS app into CSV file but can't find file

问题

我正在尝试将从iOS应用程序生成的数据保存到CSV文件中。我的代码似乎可以正常工作,并在控制台显示消息,表示数据已保存到文件中。问题在于我无法在iPhone或我的MacBook上找到那个CSV文件

你能帮忙解决一下吗?

以下是我的代码:

```swift
let data = [
    ["name": "Sara", "age": 30],
    ["name": "John", "age": 25]
]

var csvString = "Name,Age\n"

for row in data {
    let name = row["name"]!
    let age = row["age"]!
    csvString.append("\(name),\(age)\n")
}

let fileName = "name_age_data.csv"
let docDirURL = try! FileManager.default.url(for: .documentDirectory, in: .allDomainsMask, appropriateFor: nil, create: true)
let fileURL = docDirURL.appendingPathComponent(fileName)

try! csvString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)

print("数据已保存到 \(fileName)")

如何将数据保存到CSV文件,以便我可以在我的MacBook上找到它?


<details>
<summary>英文:</summary>

I am trying to save the data generated from an iOS app into a CSV file. My code seems to be working and it shows a message in the console that the data is saved to the file. The problem is that I can&#39;t find that CSV file either on the iPhone or my MacBook.

Can you please help with this?

Here is my code:

let data = [
["name": "Sara", "age": 30],
["name": "John", "age": 25]
]

var csvString = "Name,Age\n"

for row in data {
let name = row["name"]!
let age = row["age"]!
csvString.append("(name),(age)\n")
}

let fileName = "name_age_data.csv"
let docDirURL = try! FileManager.default.url(for: .documentDirectory, in: .allDomainsMask, appropriateFor: nil, create: true)
let fileURL = docDirURL.appendingPathComponent(fileName)

try! csvString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)

print("Data saved to (fileName)")


How to save the data into CSV file that I can find on my MacBook?


</details>


# 答案1
**得分**: 1

鉴于您要求特定解决方案以从您正在测试的设备访问文件,而不是允许用户在您的应用程序中导入/导出文件的通用解决方案,您可以使用Xcode。

 - 从Xcode的*窗口*菜单中选择*设备和模拟器*
 - 通过USB连接您的设备,并从设备列表中选择它
 - 在*已安装应用程序*列表中找到您的应用程序并选择它
 - 点击底部工具栏上的带有*...*的圆圈
 - 从弹出菜单中选择*下载容器*
 - 右键单击已下载容器,选择“显示包内容”
 - 您将在*AppData*下找到您的应用程序的*Documents*目录

<details>
<summary>英文:</summary>

Given that you want a specific solution to access the file from a device that you are testing on, rather than a generic solution that allows users to import/export a file from your app, you can use Xcode.

 - From the Xcode *Window* menu, select *Devices and simulators*
 - Connect your device via USB and select it from the device list
 - Find your app in the list of *Installed apps* and select it
 - Click on the *...* in a circle on the bottom toolbar
 - Select *Download Container* from the pop up menu
 - Right click on the downloaded container and select &quot;Show Package Contents&quot;
 - You will find your app&#39;s *Documents* directory under *AppData*

</details>



huangapple
  • 本文由 发表于 2023年4月17日 16:06:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定