歡迎加入QQ討論群258996829
匿名 頭像
蘋果0袋
0
匿名

http數據獲取

發(fā)布時間:2018-08-12 14:49  回復:2  查看:12168   最后回復:2019-11-19 17:50  
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看名稱就是個本地文件路徑啊。
您還未登錄,請先登錄

熱門帖子

最新帖子

?