无法将'ReverseRandomAccessCollection< [AnyObject]>'类型的值转换为强制

时间:2016-02-27 00:32:04

标签: ios swift

使用Parse。我不得不编辑一些较旧的Swift到Swift 2,除了带错误的行外,大部分看起来都不错。可能还有其他原因导致错误,但我不确定。如果没有人对此进行修复,我可以稍后发布更多代码以查看它是否有助于解决问题。评论错误点。

同一行上有两个错误:

  

从'ReverseRandomAccessCollection< [AnyObject]>'投射(又名'ReverseRandomAccessCollection>')到不相关的类型'[PFObject]'总是失败

     

无法指定'[PFObject]'类型的值来输入'NSMutableArray'

class TimelineTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var timelineData:NSMutableArray = NSMutableArray()

override init(style: UITableViewStyle) {
    super.init(style: style)
    // Custom initialization
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
}

override func viewDidAppear(animated: Bool) {

    let footerView:UIView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
    self.tableView.tableFooterView = footerView

    //let logoutButton:UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton // legacy
    // let logoutButton = UIButton(type: UIButtonType.System) as UIButton // Swift 1.5
    let logoutButton = UIButton(type: .System) as UIButton // Swift 2 convenience init
    logoutButton.frame = CGRectMake(20, 10, 50, 20)
    logoutButton.setTitle("Logout", forState: UIControlState.Normal)
    logoutButton.addTarget(self, action: "logout:", forControlEvents: UIControlEvents.TouchUpInside)

    footerView.addSubview(logoutButton)


    if ((PFUser.currentUser() == nil)){
        self.showLoginSignUp()
    }

}

func showLoginSignUp(){
    let loginAlert:UIAlertController = UIAlertController(title: "Sign Up / Login", message: "Please sign up or login", preferredStyle: UIAlertControllerStyle.Alert)

    loginAlert.addTextFieldWithConfigurationHandler({
        textfield in
        textfield.placeholder = "Your username"
        })

    loginAlert.addTextFieldWithConfigurationHandler({
        textfield in
        textfield.placeholder = "Your password"
        textfield.secureTextEntry = true
        })

    loginAlert.addAction(UIAlertAction(title: "Login", style: UIAlertActionStyle.Default, handler: {
        alertAction in
        let textFields:NSArray = loginAlert.textFields! as NSArray
        let usernameTextfield:UITextField = textFields.objectAtIndex(0) as! UITextField
        let passwordTextfield:UITextField = textFields.objectAtIndex(1) as! UITextField

        PFUser.logInWithUsernameInBackground(usernameTextfield.text, password: passwordTextfield.text){
            (user:PFUser!, error:NSError!)->Void in
            if ((user) != nil){
                print("Login successfull")
                let installation:PFInstallation = PFInstallation.currentInstallation()
                installation.addUniqueObject("Reload", forKey: "channels")
                installation["user"] = PFUser.currentUser()
                installation.saveInBackground()

            }else{
                print("Login failed")
            }

        }

        }))

    loginAlert.addAction(UIAlertAction(title: "Sign Up", style: UIAlertActionStyle.Default, handler: {
        alertAction in
        let textFields:NSArray = loginAlert.textFields! as NSArray
        let usernameTextfield:UITextField = textFields.objectAtIndex(0) as! UITextField
        let passwordTextfield:UITextField = textFields.objectAtIndex(1) as! UITextField

        let sweeter:PFUser = PFUser()
        sweeter.username = usernameTextfield.text
        sweeter.password = passwordTextfield.text

        sweeter.signUpInBackgroundWithBlock{
            (success:Bool!, error:NSError!)->Void in
            if (error == nil){
                print("Sign Up successfull")
                let imagePicker:UIImagePickerController = UIImagePickerController()
                imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
                imagePicker.delegate = self

                self.presentViewController(imagePicker, animated: true, completion: nil)

                let installation:PFInstallation = PFInstallation.currentInstallation()
                installation.addUniqueObject("Reload", forKey: "channels")
                installation["user"] = PFUser.currentUser()
                installation.saveInBackground()


            }else{
                let errorString = error.userInfo["error"] as! NSString
                print(errorString)
            }

        }

        }))

    self.presentViewController(loginAlert, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    // let pickedImage:UIImage = info.objectForKey(UIImagePickerControllerOriginalImage) as! UIImage - old syntax
    let pickedImage:UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage

    // Scale down image
    let scaledImage = self.scaleImageWith(pickedImage, and: CGSizeMake(100, 100))

    let imageData = UIImagePNGRepresentation(scaledImage)

    let imageFile:PFFile = PFFile(data: imageData)

    PFUser.currentUser().setObject(imageFile, forKey: "profileImage")
    PFUser.currentUser().saveInBackground()

    picker.dismissViewControllerAnimated(true, completion: nil)
}

func scaleImageWith(image:UIImage, and newSize:CGSize)->UIImage{
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
    let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.loadData()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadData", name: "reloadTimeline", object: nil)

}

@IBAction func loadData(){

    timelineData.removeAll(keepCapacity: false)

    let findTimelineData:PFQuery = PFQuery(className:"Sweets")
    findTimelineData.findObjectsInBackgroundWithBlock
        {
            (objects:[AnyObject]! , error:NSError!) -> Void in
            if error == nil
            {
                self.timelineData = objects.reverse() as [PFObject] // error

                print(objects)

                self.tableView.reloadData()
            }
    }
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell:SweetTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SweetTableViewCell

    //let sweet:PFObject = self.timelineData.objectAtIndex(indexPath.row) as! PFObject
    let sweet: PFObject = self.timelineData[indexPath.row] as! PFObject

    cell.sweetTextView.alpha = 0
    cell.timestampLabel.alpha = 0
    cell.usernameLabel.alpha = 0

    cell.sweetTextView.text = sweet.objectForKey("content") as! String


    let dataFormatter:NSDateFormatter = NSDateFormatter()
    dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
    cell.timestampLabel.text = dataFormatter.stringFromDate(sweet.createdAt)

    let findSweeter:PFQuery = PFUser.query()
    findSweeter.whereKey("objectId", equalTo: sweet.objectForKey("sweeter").objectId)

    findSweeter.findObjectsInBackgroundWithBlock{
        (objects:[AnyObject]!, error:NSError!)->Void in
        if (error == nil){
            let user:PFUser = (objects as NSArray).lastObject as! PFUser
            cell.usernameLabel.text = user.username

            // Profile Image
            cell.profileImageView.alpha = 0

            let profileImage:PFFile = user["profileImage"] as! PFFile

            profileImage.getDataInBackgroundWithBlock{
                (imageData:NSData!, error:NSError!)->Void in

                if (error == nil){
                    let image:UIImage = UIImage(data: imageData)!
                    cell.profileImageView.image = image
                }

            }

            UIView.animateWithDuration(0.5, animations: {
                cell.sweetTextView.alpha = 1
                cell.timestampLabel.alpha = 1
                cell.usernameLabel.alpha = 1
                cell.profileImageView.alpha = 1
            })
        }
    }


    return cell
}
}

1 个答案:

答案 0 :(得分:1)

在发生错误的闭包的参数中,objects的类型为[AnyObject]!。因此,在没有强制向下转换的情况下,数组实际上无法转换为PFObject的数组。

将错误行更改为:

self.timelineData = objects.reverse() as! [PFObject]

timelineData应定义为:

var timelineData = [PFObject]()