swift 向沙盒写日志

guard let rootPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first else {
            return
        }
        let dir = "\(rootPath)/Test"
        let filePath = "\(dir)/hjj.txt"
        if !FileManager.default.fileExists(atPath: dir) {
            try? FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true, attributes: nil)
        }
        if !FileManager.default.fileExists(atPath: filePath) {
            let isSuc = FileManager.default.createFile(atPath: filePath, contents: nil, attributes: nil)
            print("cg == \(isSuc)")
        }
        print("filePath == \(filePath)")
        if let handle = FileHandle.init(forWritingAtPath: filePath) {
            defer{
                handle.closeFile()
            }
            let test = "\(Date.init()): test === \n"
            handle.seekToEndOfFile()
            if let data = test.data(using: String.Encoding.utf8) {
                handle.write(data)
            }
            
        }

你可能感兴趣的:(swift 向沙盒写日志)