0
好吧,我正在抓取一些网页的基本数据。我想将我的代码重构为另一个类,并从我检索到的字符串中返回一个字符串,但对于异步函数很难,而我用swift创建新字符串。从swift中的网页抓取功能返回字符串
我现在认识到这个函数不能返回字符串,但我不能完全弄清楚如何配置完成处理程序以及如何使用完成处理程序从主类中调用该函数。
任何帮助将不胜感激,谢谢。
func getNameFromProfileUrl(profileUrl: NSURL) -> String {
var playerName = ""
let task = NSURLSession.sharedSession().dataTaskWithURL(profileUrl, completionHandler: { (data, response, error) -> Void in
if error == nil {
var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding) as NSString!
var urlContentArray = urlContent.componentsSeparatedByString("<title>")
var statusArray = urlContentArray[1].componentsSeparatedByString("</title>")
playerName = statusArray[0] as! String
}
})
task.resume()
return playerName
}
谢谢!今天晚上我会拍一拍,看起来就像我想象的那样! – siegs