Swift 写入MacOS上的/etc/hosts

Swift 写入MacOS上的/etc/hosts,swift,macos,permissions,hosts,Swift,Macos,Permissions,Hosts,我想在MacOS上更改我的/etc/hosts文件 let url = URL(fileURLWithPath: "/etc/hosts"); newContentString.write(to: url, atomically: true, encoding: String.Encoding.utf8); 我得到了这个错误,这并非完全出乎意料;) 我可以阅读该文件,只想知道如何将字符串保存到/etc/hosts文件 马科斯 沙箱关闭了 迅捷 使用“/Users/myna

我想在MacOS上更改我的
/etc/hosts
文件

let url = URL(fileURLWithPath: "/etc/hosts");
newContentString.write(to: url, atomically: true, encoding: String.Encoding.utf8);
我得到了这个错误,这并非完全出乎意料;)

我可以阅读该文件,只想知道如何将字符串保存到
/etc/hosts
文件

  • 马科斯
  • 沙箱关闭了
  • 迅捷
  • 使用“/Users/myname/Desktop/hosts”时有效

如果从命令行执行此操作,则需要使用sudo,这样在从应用程序执行此操作时,您就没有正确的访问权限。可能会很有用
Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “hosts” in the folder “etc”." UserInfo={NSFilePath=/etc/hosts, NSUserStringVariant=Folder, NSUnderlyingError=0x60000112c630 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}