自定义对象数组 - Swift

时间:2015-10-20 14:14:47

标签: ios swift swift2

我有这段代码,我在尝试创建一组用户时遇到了问题。

如果我使用" var userArray:[Users] = FakeData "给我以下错误"无法转换类型' User_TableViewController - >的值() - > [用户]'指定类型' [用户]'"

如果我使用" var userArray:[Users] = FakeData()"给出了以下错误"在调用"

中缺少参数#1的参数

如果我使用" var userArray:[Users] = [FakeDate] "我收到以下错误"使用未解析的标识符' FakeDate'"

有什么问题?我用完了想法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: "mail1@hotmail.com", password: "1234567", age: "18", gender: "Male", image: UIImage(named: "Apple48x48")!)
        let Person2 = Users(firstName: "Usuario 2", lastName: "Apellido 2", email: "mail2@hotmail.com", password: "1234567", age: "18", gender: "Male", image: UIImage(named: "Apple48x48")!)
        let Person3 = Users(firstName: "Usuario 3", lastName: "Apellido 3", email: "mail3@hotmail.com", password: "1234567", age: "18", gender: "Male", image: UIImage(named: "Apple48x48")!)

        return [Person1, Person2, Person3]

    }

}

1 个答案:

答案 0 :(得分:2)

您应该在方法内初始化数组,例如$folder1 = "<external server>" $folder2 = "<local path>" # Get all files under $folder1, filter out directories $firstFolder = Get-JFSChildItem $folder1 | Where-Object { -not $_.PsIsContainer } $firstFolder | ForEach-Object { # Check if the file, from $folder1, exists with the same path under $folder2 If (!(Test-Path($_.FullName.Replace($folder1, $folder2)))) { $fileSuffix = $_.FullName.TrimStart($folder1) Write-Host "$fileSuffix is only in folder1" Receive-JFSItem $fileSuffix -destination $folder2 } } viewDidLoad。对于属性声明,请设置

init

var userArray:[Users]!

然后在里面(例如)var userArray:[Users] = []

viewDidLoad

(同样的惯例是命名以小写字母开头的方法,所以userArray = FakeData() ,在你尝试编写FakeDate而不是FakeData的一次尝试中,变量名也应该以小写字母开头,即cell不是Cell