Seb*_*ian 3 linux swift swift3
我目前正在将框架移植到服务器端Swift,在将字符串写入文件时偶然发现崩溃。在macOS下它正在运行。
源代码
#!/usr/bin/swift
import Foundation
let url = URL(string: "/tmp/foo")!
let line = "foobar"
print("writing to \(url)")
try line.write(to: url, atomically: true, encoding: String.Encoding.utf8)
Run Code Online (Sandbox Code Playgroud)
导致细分错误:
root@bd5d9b821031:/# ./test.swift
writing to <CFURL 0x67b66e0 [0x7ff283886840]>{string = /tmp/foo, encoding = 0, base = (null)}
0 swift 0x000000000333cb08 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1 swift 0x000000000333b2d6 llvm::sys::RunSignalHandlers() + 54
2 swift 0x000000000333d63a
3 libpthread.so.0 0x00007ff28921b330
4 libswiftCore.so 0x00007ff285c3f648 swift_getErrorValue + 8
5 libswiftCore.so 0x00007ff2896462d1 swift_getErrorValue + 60845201
6 swift 0x0000000000c161f4 llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 996
7 swift 0x0000000000c197af llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::vector<std::string, std::allocator<std::string> > const&, char const* const*) + 1215
8 swift 0x00000000007e6bff swift::RunImmediately(swift::CompilerInstance&, std::vector<std::string, std::allocator<std::string> > const&, swift::IRGenOptions&, swift::SILOptions const&) + 2367
9 swift 0x00000000007e0818
10 swift 0x00000000007dbab7 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2887
11 swift 0x00000000007a7568 main + 2872
12 libc.so.6 0x00007ff2879c4f45 __libc_start_main + 245
13 swift 0x00000000007a4f66
Stack dump:
0. Program arguments: /usr/bin/swift -frontend -interpret ./test.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -color-diagnostics -module-name test
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
我也尝试将其包装在中,do ... catch但仍会出现段错误。任何解决方法的想法都深表感谢!
我想问题出在这一行
let url = URL(string: "/tmp/foo")!
Run Code Online (Sandbox Code Playgroud)
尝试将其替换为
let url = URL(fileURLWithPath: "/tmp/foo")
Run Code Online (Sandbox Code Playgroud)
这样,您将获得正确的文件网址
| 归档时间: |
|
| 查看次数: |
653 次 |
| 最近记录: |