iosswiftparse-platformcastingpffile

Conditional cast from PFFile toPFFile always succeed


I get a warming of "Conditional cast from PFFile toPFFile always succeed" which highlighted if let tempProductPicture = self.askProductImageArray[0] as? PFFile. What is the best way to solve it? Thanks

var askProductImageArray = [PFFile]()
override func viewDidLoad() {
    let query = PFQuery(className: "products")
    query.whereKey("title", equalTo: labelTitleText)
    query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error:NSError?) -> Void in
            if error != nil {
            }
            for object in objects! {
                self.askProductImageArray.append(object.objectForKey("detailsImage") as! PFFile)
                self.askTable.reloadData()
            }
            if let tempProductPicture = self.askProductImageArray[0] as? PFFile {
                tempProductPicture.getDataInBackgroundWithBlock { data, error in
                    if data == nil {

                    } else if error != nil {
                    } else {
                        self.productPicture.image = UIImage(data: data!)
                    }
                }
            }
}

Solution

  • Your self.askProductImageArray is an array of type PFFIle so there is no need to do as? PFFile because swift is strongly typed language and it will hold only PFFile so remover as? PFFile at the end.

    In your case you don't need to do if let at all, you just need to check if item already exist.

    you can do something like this

    if self.askProductImageArray.count > 0 {
    let tempProduct = self.askProductImageArray[0]
    // and do rest 
    }