【发布时间】:2016-05-07 17:44:03
【问题描述】:
我在从外部函数调用类中的变量时遇到问题。
Swift 给我以下错误:Use of unresolved identifier 'imageFilename'
我该如何解决?我应该如何获取Filename 变量的值?
我的代码:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if (collectionView == self.collectionView1)
{
let cell : FeaturedCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierFeatured, forIndexPath: indexPath) as! FeaturedCollectionViewCell
let imgURL: NSURL = NSURL(string: "http://localhost:9001/feature-0.jpg")!
let request: NSURLRequest = NSURLRequest(URL: imgURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request){
(data, response, error) -> Void in
if (error == nil && data != nil)
{
func display_image()
{
let imageFilename = UIImage(data: data!)
}
dispatch_async(dispatch_get_main_queue(), display_image)
}
}
task.resume()
cell.featuredImage.image = UIImage(named: imageFilename)
return cell
}
}
【问题讨论】:
-
可能的问题是 imageFileName 在不同的范围内,因此在到达您尝试使用它的部分之前它实际上已被删除。将其声明为 if 范围之外的 var。
标签: swift variables global-variables call xcode7