欢迎加入QQ讨论群258996829
匿名 头像
苹果0袋
0
匿名

http數據獲取

发布时间:2018-08-12 14:49  回复:1  查看:385   最后回复:2018-08-13 20:07  
func getDataFrom(UrlString : String) -> String?{
    let Url = URL (fileURLWithPath: UrlString)
    do{
        let _ = try Data(contentsOf: Url)
        return "success"
    }catch{
        print(error)
        return "fail"
    }
}

if let data = getDataFrom(UrlString: "http://www.baidu.com"){
    print(data)
}
報錯

Error Domain=NSCocoaErrorDomain Code=260 "The file “www.baidu.com” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Users/xxx/Library/Developer/Xcode/DerivedData/DownloadWallpaper-fhscltsqxkthirfdumcybcadbkna/Build/Products/Debug/http:/www.baidu.com, NSUnderlyingError=0x100e072e0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

fail

Program ended with exit code: 0


Swift 头像
苹果5袋
5
Swift   2018-08-13 20:07
看错误是找不到文件,可能是你代码URL传参数弄错了,let Url = URL (fileURLWithPath: UrlString), fileURLWithPath看名称就是个本地文件路径啊。
您还未登录,请先登录

热门帖子

最新帖子