在swift中调用类方法

时间:2015-03-23 01:17:17

标签: ios class swift methods

我创建了一个小程序,其中包含人名,性别和竞技场点数,并根据用户的性别/评级显示消息。

在我调用我的方法之前一切都很好,我可以看到kemkoi已经收到了值"kemkoi", "male" and "2500"但是每当我调用我的方法时它只显示"()"

以下是代码:

class Player {
    var name : String
    var gender : String
    var arenaPoints : Int

    // method
    func getPlayerStats() {

        if(gender != "male" || gender != "female") {
            println("Please choose a gender!")
        } else if(arenaPoints < 2000) {
            println("Seems like you're too unexperienced \(arenaPoints) rating is not enough, come back when you have over 2.2k!")
        } else {
            println("Hello, \(name). You've chosen to play as a \(gender) and your arena points are equal to: \(arenaPoints)")
        }
    }

    // initializer
    init(name : String, gender : String, arenaPoints : Int) {
        self.name = name
        self.gender = gender
        self.arenaPoints = arenaPoints
    }

}

var kemkoi = Player(name: "kemkoi", gender: "male", arenaPoints: 2500)

println( kemkoi.getPlayerStats() )

1 个答案:

答案 0 :(得分:0)

通过执行println( kemkoi.getPlayerStats() ),您将打印kemkoi.getPlayerStats()的结果,该结果为零。

尝试简单地调用kemkoi.getPlayersStats()


另请注意,该方法将始终输出Please choose a gender!,因为您正在执行OR,您应该使用AND代替:

if(gender != "male" && gender != "female")