Swift Error Anyobject does not have a member named 'Generator'

时间:2015-05-24 22:01:49

标签: ios swift parse-platform

Okay. I have absolutely no idea what i am doing wrong. I am creating a social networking app and i am trying to declare a for statement that says "For object in objects {....." but the code does not work. I have scoured forums and nothing is helping. I am using parse if that matters.

Here is my code

//
//  usersCell.swift
//  CaastRun
//
//  Created by Computer on 5/23/15.
//  Copyright (c) 2015 Caast. All rights reserved.
//

import UIKit

class usersCell: UITableViewCell {


    @IBOutlet weak var imgView: UIImageView!

    @IBOutlet weak var profileLbl: UILabel!

    @IBOutlet weak var usernameLbl: UILabel!

    @IBOutlet weak var followBtn: UIButton!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code


        let theWidth = UIScreen.mainScreen().bounds.width

        contentView.frame = CGRectMake(0, 0, theWidth, 64)
        imgView.center = CGPointMake(32, 32)

        imgView.layer.cornerRadius = imgView.frame.size.width / 2
        imgView.clipsToBounds = true
        profileLbl.frame = CGRectMake(70, 10, theWidth-75, 18)
        followBtn.center = CGPointMake(theWidth-50, 42)

    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }


    @IBAction func followBtn_click(sender: AnyObject) {

        let title = followBtn.titleForState(.Normal)
        if title == "Follow" {

            var followObj = PFObject(className: "follow")
            followObj["user"] = PFUser.currentUser()!.username
            followObj["userToFollow"] = usernameLbl.text
            followObj.save()
            followBtn.setTitle("Following", forState: UIControlState.Normal)
        }else{
            var query = PFQuery(className: "follow")
            query.whereKey("user", equalTo: PFUser.currentUser()!.username!)
            query.whereKey("userToFollow", equalTo: usernameLbl.text!)





            var objects = query.findObjects()
            for object in objects {
                object.delete()


}
followBtn.setTitle("follow", forState: UIControlState.Normal)

        }
        }
}

1 个答案:

答案 0 :(得分:2)

问题是findObjects()正在返回AnyObject类型,并且您无法在for循环中使用它而不先将其强制转换。 Parse Documentation表示findObjects()返回PFObject数组,因此我建议:

if let objects = query.findObjects() as? [PFObject] {
    for object in objects {
        object.delete()
    }
}