0

I wrote the following piece of code which is identical to Swift Programming Language by Apple and got an unexpected error saying : type'()' does not conform to protocol 'boolean type' , its location is shown in the following code:

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}
}
}
user3783574
  • 127
  • 9
  • Check out this style guide, it will make your code a lot more readable and prevent it from not working in some cases https://github.com/raywenderlich/swift-style-guide#spacing – Ian Dec 14 '14 at 15:18

1 Answers1

2

You need to add spaces around your != operators.

Change:

if buildingName!=nil

To:

if buildingName != nil

Swift is parsing buildingName!=nil as buildingName! = nil, so help it out by adding spaces around the boolean operator.

vacawama
  • 150,663
  • 30
  • 266
  • 294