枚举if-else比较

时间:2018-03-22 15:50:05

标签: swift if-statement enums

如何在以下代码中执行正确的比较检查?

    [
{
"_id": "5aaf99c981db2e1d38c84fb8",
"gameName": "Pac Man",
"gameCategorie": "arcade",
"gameImg": "https://image.ibb.co/f5h4AH/4.jpg",
"gamePath": "yok",
"gameTags": "arkade",
"editor": "sometext",
"__v": 0
},
{
"_id": "5aaf9a7281db2e1d38c84fb9",
"gameName": "SuperMario",
"gameCategorie": "yok",
"gameImg": "https://image.ibb.co/c9nRPc/11.jpg",
"gamePath": "yok",
"gameTags": "yok",
"editor": "merhaba",
"__v": 0
},
{
"_id": "5aafb3913af3be3c64f78aca",
"gameName": "Winged Bullet",
"gameCategorie": "arcade",
"gameImg": "https://cdn4.kongcdn.com/game_icons/0001/0716/thumb100x75.jpg?i10c=img.resize(width:93)",
"gamePath": "<embed width=\"550\" height=\"400\" base=\"https://external.kongregate-games.com/gamez/0000/6057/live/\" src=\"https://external.kongregate-games.com/gamez/0000/6057/live/embeddable_6057.swf\" type=\"application/x-shockwave-flash\"></embed>",
"gameTags": "sometext",
"editor": "sometext",
"__v": 0
},
{
"_id": "5ab1269939800f0e0ce2efc3",
"gameName": "super mario 2",
"gameCategorie": "yok",
"gameImg": "https://image.ibb.co/c9nRPc/11.jpg",
"gamePath": "sometext",
"gameTags": "sometext",
"editor": "sometext",
"__v": 0
}
]

所以有一个结构,其中一个属性是枚举enum Location { case ontop case inside case underneath } struct Item { var location: Location? func checkStuff(currentLocation: Location?) { if case .ontop = currentLocation { // DO SOME STUFF } } } // currentLocation is optional, and initially nil var currentLocation: Location? var item1 = Item(location: .ontop) item1.checkStuff(currentLocation: currentLocation) currentLocation = item1.location var item2 = Item(location: .inside) item2.checkStuff(currentLocation: currentLocation) ,因此它只能有3个值中的1个。

如果实例的Location属性与作为Location的当前状态的相同类型的外部提供的值(来自同一对象类型的另一个实例)相同,则struct有一个方法可以采取操作

我无法获得正确的语法来进入location语句的正确部分。

我也试过展开可选的if

currentLocation

4 个答案:

答案 0 :(得分:4)

重要的是要注意currentLocation不是Location,而是Location?(a.k.a。Optional<Location>)。因此,您必须首先针对Optional的案例进行模式匹配,而不是Location。然后,在Optional个案例的模式中,您可以匹配Location的各种情况。

这是语法糖的进展,从最详细的开始,到达最简洁,最常见的方式:

  • if case Optional.some(.ontop) = currentLocation { ... }
  • if case .some(.ontop) = currentLocation { ... }
  • 最后,首选方式:if case .ontop? = currentLocation { ... }

if case只有在你想检查大量案例的一小部分时才真正理想。如果您需要检查多个案例,最好使用switch。案例模式是相同的:

switch currentLocation {
    case .onTop?: // ...
    case .inside?: // ...
    case .underneath?: // ...
    case nil: // ...
}

答案 1 :(得分:4)

您可以像这样优雅地编写它,最好使用switch语句而不是if条件。

enum Location {
    case ontop
    case inside
    case underneath
}

struct Item {
    var location: Location?

    func checkStuff(currentLocation: Location?) {
        switch currentLocation {
        case .ontop?:
            print("on top")
        case .inside?:
            print("inside")
        case .underneath?:
            print("underneath")

        case .none:
            print("Location is nil")
        }
    }
}

答案 2 :(得分:2)

尝试使用switch声明:

switch self.currentLocation {
case .ontop?:
    break
case .inside?:
    break
case .underneath?:
    break
case nil:
    // In this case, `self.currentLocation` is `nil` rather than one of the cases of `Location`
    break
}

在switch语句中的每个案例之后,您需要?,因为self.currentLocationLocation?。切换案例中的值必须与self.currentLocation的类型相匹配。通过简单地写.ontop.inside等,它们的类型为Location,而写.ontop?.inside?等等则会生成类型{ {1}},这是正确的类型。

答案 3 :(得分:0)

如果您希望使用if参数进行单Location?次测试,则可以添加?

func checkStuff(currentLocation: Location?) {
    if case .ontop? = currentLocation {
        // DO SOME STUFF
    }
}

或者只使用==测试(可用于将可选项与文字进行比较):

func checkStuff(currentLocation: Location?) {
    if currentLocation == .ontop {
        // DO SOME STUFF
    }
}

我认为后一种方法会产生更自然的代码,但如果您的枚举具有关联值(它不具有相关价值),则if case方法非常有价值。

显然,如果您想要测试其他Location枚举的可能性,那么您可以使用switch语句(在各种情况后再次使用?),如下所示其它:

func checkStuff(currentLocation: Location?) {
    switch currentLocation {
    case .ontop?:
        // DO SOME STUFF IF ONTOP
    case .inside?:
        // DO SOME OTHER STUFF IF INSIDE
    case .underneath?:
        // DO YET SOME OTHER STUFF IF UNDERNEATH
    case nil:
        // DO YET SOME OTHER STUFF IF NIL
    }
}