0
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = self.collectionView?.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PictureCell
    picArray[indexPath.row].getDataInBackground{(data:Data?, error:Error?) in
        if error == nil{
            cell.*picImg*.image = UIImage(data: data!)
        }else{
            print(error?.localizedDescription as Any)
        }
    }
    return cell
}

The above part funs well, and then...

override func awakeFromNib() {
    super.awakeFromNib()
    let width = UIScreen.main.bounds.width
// I got an error in the following line:
    picImg.frame = CGRect(x:0, y:0, width: width/3, height: width/3)
}

I got an error in the above mentioned line: " use of unresolved identifier 'picImg'. I declared " picImg " in another Cocoa Touch Class of this project. The code is as following:

import UIKit
class PictureCell: UICollectionViewCell {
    @IBOutlet weak var picImg: UIImageView!
    override func awakeFromNib() {
        super.awakeFromNib()
        let width = UIScreen.main.bounds.width
        picImg.frame = CGRect(x:0, y:0, width: width/3, height: width/3)
    }
}

The question is why Xcode didn't report error at the first time I used " picImg " (in collectionView function) but report error in the other. How could I solve this problem ? Thanks for your help.

Wilson
  • 49
  • 6
  • 1
    Possible duplicate of ['Use of Unresolved Identifier' in Swift](http://stackoverflow.com/questions/28996730/use-of-unresolved-identifier-in-swift) –  Apr 04 '17 at 03:27
  • I just copied your `PictureCell` class into Xcode I do not get any error. – rmaddy Apr 04 '17 at 03:27
  • The PictureCell class has no problem, error occurred in the above mentioned class " awakeFromNib " – Wilson Apr 04 '17 at 03:31
  • If I delete the " awakeFromNib " class, the project runs well. – Wilson Apr 04 '17 at 03:33
  • 1
    What "awakeFromNib" class? There is no such class. There is the `awakeFromNib` method posted in the middle of your question. Is that different from the `awakeFromNib` function on your `PictureCell` class? If so, where is it? And why would you expect it to know anything about a property from another class? – rmaddy Apr 04 '17 at 03:38
  • Sorry, it is awakeFromNib method. It is the same as the function in PictureCell class. If it doesn't know the property from the PictureCell class, then why it doesn't report error in the collectionView method ? Actually I also use the property in collectionView method. And the awakeFromNib method is just below the collectionView method and they belong to one class. – Wilson Apr 04 '17 at 03:47
  • In the `cellForItemAt` method you are accessing the `picImg` from `cell` which is an instance of `PictureCell`. In the `awakeFromNib` function from your collection view controller class, your are trying to access a `picImg` variable on the collection view controller. There is no such variable. More importantly, why are you trying to do this in the collection view controller's `awakeFromNib` function? – rmaddy Apr 04 '17 at 03:55
  • Thank you. Now I understand. How could I declare a property (like picImg) which I just created in another Cocoa Touch Class (pictureCell) ? – Wilson Apr 04 '17 at 04:05

0 Answers0