0

I have this piece of code, I have a problem trying to create an array of users.

if I use "var userArray: [Users] = FakeData" gives me the following error "Can not convert value of type 'User_TableViewController -> () -> [Users]' to specified type '[Users]'"

if I use "var userArray: [Users] = FakeData()" gives me the following error "Missing argument for parameter # 1 in call"

if I use "var userArray: [Users] = [FakeDate]" I get the following error "Use of unresolved identifier 'FakeDate'"

What is the problem? I ran out of ideas xD

class User_TableViewController: UITableViewController {

    var userArray:[Users] = FakeData()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return userArray.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let Cell = tableView.dequeueReusableCellWithIdentifier("userCell") as! UserCell_TableViewCell
        Cell.userImage.image = userArray[indexPath.row].getImage()
        Cell.userDescription?.text = userArray[indexPath.row].getFirstName()

        return Cell
    }

    func FakeData() -> [Users]{

        let Person1 = Users(firstName: "Usuario 1", lastName: "Apellido 1", email: "[email protected]", password: "1234567", age: "18", gender: "Male", image: UIImage(named: "Apple48x48")!)
        let Person2 = Users(firstName: "Usuario 2", lastName: "Apellido 2", email: "[email protected]", password: "1234567", age: "18", gender: "Male", image: UIImage(named: "Apple48x48")!)
        let Person3 = Users(firstName: "Usuario 3", lastName: "Apellido 3", email: "[email protected]", password: "1234567", age: "18", gender: "Male", image: UIImage(named: "Apple48x48")!)

        return [Person1, Person2, Person3]

    }

}
3
  • 1
    what is FakeData? The usual syntax is supposed to be var userArray = [Users](), the annotation is not needed. Commented Oct 20, 2015 at 14:18
  • FakeData is a method in OPs class, it's in the question. Commented Oct 20, 2015 at 14:25
  • Yes, thanks, I thought it was a class (weird spelling) Commented Oct 20, 2015 at 14:28

1 Answer 1

2

You should be initializing the array inside a method, like viewDidLoad or init. For the property declaration either set

var userArray:[Users]!

or

var userArray:[Users] = []

Then inside (for example) viewDidLoad:

userArray = FakeData()

(Also convention is to name methods starting with lowercase letters, so fakeData(), in one of your attempts you wrote FakeDate instead of FakeData, and also variable names should also start with a lowercase letter i.e. cell not Cell)

Sign up to request clarification or add additional context in comments.

1 Comment

Thx for all the coments!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.