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

http數(shù)據(jù)獲取

發(fā)布時(shí)間:2018-08-12 14:49  回復(fù):2  查看:12167   最后回復(fù):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)
}
報(bào)錯(cuò)

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
看錯(cuò)誤是找不到文件,可能是你代碼URL傳參數(shù)弄錯(cuò)了,let Url = URL (fileURLWithPath: UrlString), fileURLWithPath看名稱就是個(gè)本地文件路徑啊。
您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?