Files
UUVPN/iOS-SwiftUI-Code/Library/Database/Profile+RW.swift
T
2025-01-28 12:28:03 +08:00

32 lines
1.0 KiB
Swift

import Foundation
public extension Profile {
func read() throws -> String {
switch type {
case .local, .remote:
return try String(contentsOfFile: path)
case .icloud:
let saveURL = FilePath.iCloudDirectory.appendingPathComponent(path)
_ = saveURL.startAccessingSecurityScopedResource()
defer {
saveURL.stopAccessingSecurityScopedResource()
}
return try String(contentsOf: saveURL)
}
}
func write(_ content: String) throws {
switch type {
case .local, .remote:
try content.write(toFile: path, atomically: true, encoding: .utf8)
case .icloud:
let saveURL = FilePath.iCloudDirectory.appendingPathComponent(path)
_ = saveURL.startAccessingSecurityScopedResource()
defer {
saveURL.stopAccessingSecurityScopedResource()
}
try content.write(to: saveURL, atomically: true, encoding: .utf8)
}
}
}