2

I don't have enough reputation to post images

The problem is when I did the following

for object in objects { eg: ....

// [AnyObject] does not have a member named generator - It shows me this error

}

And after my search in stackoverflow and some other sites I did this:

if objects?.count > 0 {

for object in objects! {

self.resultsUsernameArray.append(object.username) -> The error is "Cannot invoke append with an argument list of type (String?!)"

}

} else {

}

[AnyObject] does not have a member named generator || cannot invoke append with an argument list of type (String?!)

import UIKit

var userName = ""

class UsersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var resultsTable: UITableView!

var resultsUsernameArray = [String]()
var resultsProfileNameArray = [String]()
var resultsImageFiles = [PFFile]()

override func viewDidLoad() {
    super.viewDidLoad()

    let theWidth = view.frame.size.width
    let theHeight = view.frame.size.height

    resultsTable.frame = CGRectMake(0, 0, theWidth, theHeight - 64)

    userName = PFUser.currentUser()!.username!

}



override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    let predicate = NSPredicate(format: "username !="+userName+"'")
    var query = PFQuery(className: "_User", predicate: predicate)

    var objects = query.findObjects()

    for object in objects { // [AnyObject] does not have a member named generator

        self.resultsUsernameArray.append(object.username)
        self.resultsProfileNameArray.append(object["profileName"] as! String)
        self.resultsImageFiles.append(object["photo"] as! PFFile)

        resultsTable.reloadData()

    }

}

 func tableView(tableView: UITableView, numberOfRowsInSection eg..... {
}

Help me please

Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
Suprem Vanam
  • 73
  • 11

2 Answers2

0

Try this

if let username = object.username as String! {
  self.resultsUsernameArray.append(username)
}
r4id4
  • 5,877
  • 8
  • 46
  • 76
0

query.findObjects() produces a [AnyObject]? (i.e. an optional which could be [AnyObject]). So, if the result from query.findObjects() is a nil it does not conform to the Generator protocol. So, first check whether your result is a nil and then unwrap it.

Your other problem with respect to self.resultsUsernameArray.append(object.username) is that object is now an AnyObject and that it does not have a property name username. You first need to cast object to PFUser and then use the username property.

var objects = query.findObjects()
if objects != nil {  // or use objects.count > 0
    for object in objects! {
        if let user = object as? PFUser {
            resultsUsernameArray.append(user.username!)
            ...
        }
    }
}
Santhosh
  • 691
  • 4
  • 12