Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift NSURL在有效URL上给出错误_Ios_Swift_Nsurlsession_Nsurl_Nsurlrequest - Fatal编程技术网

Ios Swift NSURL在有效URL上给出错误

Ios Swift NSURL在有效URL上给出错误,ios,swift,nsurlsession,nsurl,nsurlrequest,Ios,Swift,Nsurlsession,Nsurl,Nsurlrequest,请看以下代码: let url = NSURL(string: physicst.image as String) if let url = url { let request = NSMutableURLRequest(URL: url) let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in

请看以下代码:

let url = NSURL(string: physicst.image as String)
        if let url = url {
            let request = NSMutableURLRequest(URL: url)
            let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
                if let data = data {
                    cell.imageView?.image = UIImage(data: data)
                }
            })
            task.resume()
        }else {
            print ("nill URL \(physicst.image)")
        }
所以我有一个字符串,这个字符串是一个url,我想加载它

该代码位于表视图单元格中,因此每个单元格都会调用该代码

如您所见,我正在检查url是否为零,如果不是,我将制作一个
print
语句。几乎所有的URL都不是零,以下URL除外(完全有效)

您可能会争论的第一件事是对url进行编码,这就是我所做的:

var image = oneBinding["p"]!!["value"] as! NSString
                        image = image.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!
但是,即使是正常工作的URL也停止了工作。我错过了什么

更新 以下是我的UITableViewController的全部代码(很简单)


你应该使用的方法是

stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
从操场拍摄:

NSURL(string:"http://commons.wikimedia.org/wiki/Special:FilePath/Kai_Manne_Börje_Siegbahn.jpg?width=300".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)
给出了结果

http://commons.wikimedia.org/wiki/Special:FilePath/Kai_Manne_B%C3%B6rje_Siegbahn.jpg?width=300
哪个是有效的:)

根据其提供的代码进行调整:

let url = NSURL(string: (physicst.image as String).stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()!)

当心强行拆开包装

您应该使用的方法是

stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
从操场拍摄:

NSURL(string:"http://commons.wikimedia.org/wiki/Special:FilePath/Kai_Manne_Börje_Siegbahn.jpg?width=300".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)
给出了结果

http://commons.wikimedia.org/wiki/Special:FilePath/Kai_Manne_B%C3%B6rje_Siegbahn.jpg?width=300
哪个是有效的:)

根据其提供的代码进行调整:

let url = NSURL(string: (physicst.image as String).stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()!)

当心强行拆开包装

URLHostAllowedCharacterSet
仅包含此
“#%/?@^
{|}

并且您的URL字符串包含
。因此,请对此进行自定义设置

 let yourString = "http://commons.wikimedia.org/wiki/Special:FilePath/Kai_Manne_Börje_Siegbahn.jpg?width=300"
 let customSet =  NSCharacterSet(charactersInString:"=\"#%/:<>?@\\^`{|}").invertedSet
 let finalString = yourString.stringByAddingPercentEncodingWithAllowedCharacters(customSet)   
让您的字符串=”http://commons.wikimedia.org/wiki/Special:FilePath/Kai_Manne_Börje_Siegbahn.jpg?宽度=300“
让customSet=NSCharacterSet(charactersInString:“=\”\\\\\\%/:?@\\\^`{124;}”).inversedset
让finalString=yourString.stringByAddingPercentEncodingWithAllowedCharacters(自定义集)

更多信息。选中此项

URLHostAllowedCharacterSet
仅包含此“#%/?@\ ^{124;}

并且您的URL字符串包含
。因此,请对此进行自定义设置

 let yourString = "http://commons.wikimedia.org/wiki/Special:FilePath/Kai_Manne_Börje_Siegbahn.jpg?width=300"
 let customSet =  NSCharacterSet(charactersInString:"=\"#%/:<>?@\\^`{|}").invertedSet
 let finalString = yourString.stringByAddingPercentEncodingWithAllowedCharacters(customSet)   
让您的字符串=”http://commons.wikimedia.org/wiki/Special:FilePath/Kai_Manne_Börje_Siegbahn.jpg?宽度=300“
让customSet=NSCharacterSet(charactersInString:“=\”\\\\\\%/:?@\\\^`{124;}”).inversedset
让finalString=yourString.stringByAddingPercentEncodingWithAllowedCharacters(自定义集)


更多信息。检查此项

您遇到的错误是什么?使用异步方法时,不要忘记从mainqueue@TheRohanSanap没有错误,但else语句在提到的URL中执行,虽然它们是100%有效的URL,但只需使用图像视图即可extension@LeoDabus是的,但这与手头的问题无关:)您得到的错误是什么?在使用异步方法时,不要忘记从mainqueue@TheRohanSanap没有错误,但else语句在提到的URL中执行,虽然它们是100%有效的URL,但只需使用图像视图即可extension@LeoDabus是的,但这与手头的问题无关:)有趣的是,else Station根本没有被调用。但是,您的代码使用的是一个不推荐使用的函数。有其他选择吗?您需要为urlBtw的每个部分使用不同的字符集。我认为如果您将其转换为NSString+1,则该方法仍然可用。如果我得到另一个答案,但代码未被压缩,我将进行检查,否则,我将接受您的答案,感谢efforts@sarah你说得对,我编辑了代码。这并不完美,因为我们应该拆分查询字符串和主机字符串,但无论如何它都应该可以工作。但是,您的代码使用的是一个不推荐使用的函数。有其他选择吗?您需要为urlBtw的每个部分使用不同的字符集。我认为如果您将其转换为NSString+1,则该方法仍然可用。如果我得到另一个答案,但代码未被压缩,我将进行检查,否则,我将接受您的答案,感谢efforts@sarah你说得对,我编辑了代码。这并不完美,因为我们应该拆分查询字符串和主机字符串,但无论如何它都应该可以工作。您的代码既不会执行else,也不会让我看到下载的图像。@sarah这与allowedCharacterSet相同,只是在集合中添加了一些自定义字符。。。所以url编码与自定义字符集。。。我试过了…它对我有效…加上你的努力,我正在尝试它你的代码既不执行else,也不让我看到下载的图像。@sarah这与allowedCharacterSet相同,但只是在集合中添加一些自定义字符。。。所以url编码与自定义字符集。。。我试过了…它对我有效…加上你的努力,我正在尝试