检查数组是否包含特定的枚举类型

时间:2018-02-08 13:34:53

标签: ios swift enums

确定枚举类型数组是否包含特定枚举类型的最佳方法是什么,kicker是enum case具有关联类型

例如,使用以下数据结构,我将如何获得第一个视频

let sections = [OnDemandSectionViewModel]

public struct OnDemandSectionViewModel: AutoEquatable {
    public let sectionStyle: SectionHeaderStyle
    public let sectionItems: [OnDemandItemType]
    public let sectionType: SectionType
}

public enum OnDemandItemType: AutoEquatable {
    case video(VideoViewModel)
    case button(ButtonViewModel)
    case game(GameViewModel)
    case collectionGroup(CollectionGroupViewModel)
    case clip(ClipViewModel)
}

我正在尝试找到第一个视频,目前,我正在做以下事情,但是如果有更好的方法则很好奇

for section in sections {
    for item in section.sectionItems {
        switch item {
        case .video(let video):
            print("This is the first video \(video)")
            return
        default: break
        }
    }

3 个答案:

答案 0 :(得分:3)

您可以使用Sequence.first(where:),这与contains(where:)非常相似,但不是简单地返回一个bool,而是返回满足闭包中条件的第一个元素,如果有&nil #39;没有这样的元素。

let firstVideo = sections.sectionItems.first(where: { item in
    if case .video = item {
        return true
    }
    return false
})

答案 1 :(得分:1)

如果您不需要枚举中包含的VideoViewModel,则可以输入

if section.sectionItems.contains(where: { item in 
    if case .video = item {
        return true
    }

    return false
}) {
    // Your section contains video
}

答案 2 :(得分:0)

使用过滤器功能示例:

<Menus>
  <Menu guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenu" priority="0x0100" type="Menu">
    <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenuGroup" />
    <Strings>
      <MenuText>Config Encryptor</MenuText>
      <ButtonText>Config Encryptor</ButtonText>
      <CommandName>Config Encryptor</CommandName>
    </Strings>
  </Menu>
</Menus>

<Groups>      
  <Group guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenu" priority="0x0200">
    <Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS" />
  </Group>
  <Group guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenuGroup" priority="0x0100">
    <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenu" />
  </Group>
</Groups>

<Buttons>
  <Button guid="guidEncryptConfigCommandPackageCmdSet" id="cmdidEncryptConfigCommand" priority="0x0100" type="Button">
    <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenuGroup" />
    <Strings>
      <ButtonText>Encrypt Mail Settings</ButtonText>
    </Strings>
  </Button>
  <Button guid="guidEncryptConfigCommandPackageCmdSet" id="cmdidDecryptConfigCommand" priority="0x0100" type="Button">
    <Parent guid="guidEncryptConfigCommandPackageCmdSet" id="ConfigEncryptorMenuGroup" />
    <Strings>
      <ButtonText>Decrypt Mail Settings</ButtonText>
    </Strings>
  </Button>
</Buttons>

<GuidSymbol name="guidEncryptConfigCommandPackageCmdSet" value="{2c763b06-e83f-4c03-8fc6-3a00416b361e}">
  <IDSymbol name="ConfigEncryptorMenu" value="0x1010" />
  <IDSymbol name="ConfigEncryptorMenuGroup" value="0x1020" />
  <IDSymbol name="cmdidEncryptConfigCommand" value="0x0100" />
  <IDSymbol name="cmdidDecryptConfigCommand" value="0x1021"  />
</GuidSymbol>

return section.filter({ $0.sectionType == .video }).first != nil