【问题标题】:call value variable from outside function in swift快速从外部函数调用值变量
【发布时间】: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
    }
}

Image capture link

【问题讨论】:

  • 可能的问题是 imageFileName 在不同的范围内,因此在到达您尝试使用它的部分之前它实际上已被删除。将其声明为 if 范围之外的 var。

标签: swift variables global-variables call xcode7


【解决方案1】:

imageFileName 的作用域是声明它的函数display_image,在if 之外不可见。问题不在于类中变量的访问,您的自定义单元类似乎没有声明名为imageFileName的变量

编辑

为什么不在完成闭包中设置图像?:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if (collectionView == self.collectionView1) {
        let cell = 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) {
                dispatch_async(dispatch_get_main_queue()) {
                    cell.featuredImage.image = UIImage(data: data!)
                }
            }
        }
        task.resume()

        return cell
    }
}

请注意,由于异步请求可能以未定义的顺序完成和单元格重用,您最终可能会得到不正确的单元格图像,您可以将图像 url 保存在单元格中并检查它是否与闭包完成时在闭包中捕获的那个。

【讨论】:

  • 好的。我可以在单元格中声明一个变量吗?
【解决方案2】:

如果你在函数外部声明变量,在你设置值的函数内部声明变量怎么样。然后你就可以访问变量及其值了。

您的问题绝对是您无法访问该变量,因为它只是在函数内部知道。

代码:

这样试试……

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {



if (collectionView == self.collectionView1)
{
    let cell : FeaturedCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(self.reuseIdentifierFeatured, forIndexPath: indexPath) as! FeaturedCollectionViewCell

    var imageFilename: UIImage


    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()
            {
               imageFilename = UIImage(data: data!)
            }

            dispatch_async(dispatch_get_main_queue(), display_image)
        }


    }

    task.resume()

    cell.featuredImage.image = UIImage(named: imageFilename)
    return cell
}
}

如果这对你有用,请写信给我。

【讨论】:

  • 谢谢你!现在是可见的 var :) 但是说这个错误:Cannot convert value of type 'UIImage' to expected argument type 'String' in this line code:cell.featuredImage.image = UIImage(named: imageFilename)
  • 好的,这个错误似乎很简单。您正在尝试将字符串类型的值设置为 UIImage 类型的变量。 cell.featuredImage.image那是什么类型的?
  • 我觉得还是在新的帖子里打开比较好,不然就很难理解了……你可以在这里评论下你打开的是哪个帖子,我再回复一下?
  • 好的。 cell.featuredImage.image 是一个 UIImage,imageFilename 的原始代码是这样的:让 imageFilename = "feature-(indexPath.row).jpg",故事板中的这个图像位于 Viewcontroller > View > Scroll View > Collection view > Featured单元格 > 特色图片。
  • 我想更改 URL 图像的值“feature-(indexPath.row).jpg”
猜你喜欢
  • 1970-01-01
  • 2013-11-14
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
相关资源
最近更新 更多