let filePath:String = NSHomeDirectory() + "/Documents/sample.txt"
try! json.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
let filePath:String = NSHomeDirectory() + "/Documents/sample.txt"
try! json.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
import Foundation let fullName = "First Last" let fullNameArr = fullName.components(separatedBy: " ") let name = fullNameArr[0] let surname = fullNameArr[1]
if let controller = storyboard?.instantiateViewController(withIdentifier: "ARController") as? ARViewController {
if #available(iOS 13.0, *) {
controller.modalPresentationStyle = .fullScreen
} else {
// Fallback on earlier versions
}
present(controller, animated: true, completion: nil)
}
![]() |
| 圖1、App的db.sqlite |
![]() |
| 圖2 |

let dirPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true)
let docsDir:String! = dirPaths[0]
let destPath = (docsDir as NSString).appendingPathComponent("/db.sqlite")//這裡填入你的.sqlite
let fileMgr = FileManager.default
if let path = Bundle.main.path(forResource: "db", ofType:"sqlite")
{
if !fileMgr.fileExists(atPath: destPath)//如果db不存在,那就直接複製一份
{
do {
try fileMgr.copyItem(atPath: path, toPath: destPath)
print("success")//
}
catch let error as NSError
{
print(error.localizedDescription)
}
}else{
print(".sqlite已存在")//
}
}
//CheckList.json
{
"CheckList": [
{
"CheckItem": "施工計畫書之安全衛生管理計畫",
"Major": 1,
"CheckContent": [
{
"Major": 1,
"Minor": 1,
"CheckItem": "安全衛生管理計畫各項安全檢查"
},
{
"Major": 1,
"Minor": 2,
"CheckItem": "工地潛在危險狀況分析及採取相對應之防範措施"
}
]
}
]
}
//========================================================================================================
//讀取 Json 資料
func readJson()
{
if let path = Bundle.main.path(forResource: "JsonFile/CheckList", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
if let myJSON = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any] {
//檢查項目
if let data = myJSON["CheckList"] as? [[String: Any]] {
for jsonDict in data {
var CheckItem = jsonDict["CheckItem"] as? String
print("CheckItem:" + CheckItem!)
var Major = jsonDict["Major"] as? Int
print("Major:" + String(Major!))
//檢查內容
if let CheckContent = jsonDict["CheckContent"] as? [[String: Any]] {
for CheckContentDict in CheckContent {
var CheckItem = CheckContentDict["CheckItem"] as? String
print("CheckItem:" + CheckItem!)
var Major = CheckContentDict["Major"] as? Int
print("Major:" + String(Major!))
var Minor = CheckContentDict["Minor"] as? Int
print("Minor:" + String(Minor!))
}
}
}
}
}
} catch let error {
print("parse error: \(error.localizedDescription)")
}
} else {
print("Invalid filename/path.")
}
}
