0

我编写了以下与 Apple 的 Swift Programming Language 相同的代码,但出现意外错误: type'()' does not conform to protocol 'boolean type' ,其位置显示在以下代码中:

class Person1 {
    var residence:Residence?
}

class Residence {
var rooms=[Room]()
var numberOfRooms:Int{
return rooms.count
}
subscript(i:Int)->Room{
    get{
        return rooms[i]
    }
    set{
        rooms[i]=newValue
    }
}
func printNumberOfRooms(){
    println("the number of rooms is \(numberOfRooms)")
}
var address:Address?
}
class Room {
var name:String
init(name:String){
    self.name=name
}
}
class Address {
var builingNumber:String?
var buildingName:String?
var street:String?
func buildingIdentifier()->String?{
    if buildingName!=nil {return buildingName}  Error:type'()' does not conform to   protocol 'boolean type'
    else if builingNumber!=nil {return builingNumber}
    else {return nil}
}
}
4

1 回答 1

2

您需要在!=运算符周围添加空格。

改变:

if buildingName!=nil

至:

if buildingName != nil

Swift 将解析buildingName!=nilbuildingName! = nil,因此请通过在布尔运算符周围添加空格来帮助它。

于 2014-12-14T14:59:10.567 回答