简体   繁体   English

Swift - 遍历嵌套枚举的所有情况

[英]Swift - Iterate through all cases of a nested enumeration

I have the following nested enumeration setup:我有以下嵌套枚举设置:

enum UserSettings:Equatable {
    case emailSettings(EmailSettings), notificationSettings(NotificationSettings), socialSettings(SocialSettings), gameSettings(GameSettings)

    enum EmailSettings:CaseIterable {
        case enableEmails, allowPromotionalEmails
    }

    enum NotificationSettings:CaseIterable {
        case enableAppNotifications, getNewFollowerNotifications, getTaggedInPostNotifications
    }

    enum SocialSettings:CaseIterable {
        case allowChallengeInvites, allowCrewRequests, allowLeagueInvites, allowPostTagging, allowUsersToFollow, appearOffline, crewOnlyMessaging, matchesArePublic, profileIsPublic
    }

    enum GameSettings:CaseIterable {
        case syncNewMatchesOnStartup
    }
}

I'm attempting to pass a "UserSettings" value to a cell in cellForRowAt via the following code:我正在尝试通过以下代码将“UserSettings”值传递给cellForRowAt中的单元格:

let cell = settingsTableView.dequeueReusableCell(withIdentifier: "yesNoSettingsCell", for: indexPath) as! YesNoSettingsCell
let currentSetting = UserSettings.SocialSettings.allCases[indexPath.row]
cell.passedUserSetting = currentSetting
return cell

The above code produces the following error:上面的代码产生以下错误:

"Cannot assign value of type 'UserSettings.SocialSettings' to type 'UserSettings'"

Changing the code in cellForRowAt to:cellForRowAt中的代码更改为:

let currentSetting = UserSettings.socialSettings().allCases[indexPath.row]

produces the following error:产生以下错误:

"Missing argument for parameter #1 in call"

What am I missing?我错过了什么? How do I enumerate through all cases of a nested enumeration?如何枚举嵌套枚举的所有情况?

The error "Cannot assign value of type 'UserSettings.SocialSettings' to type 'UserSettings'" is on cell.passedUserSetting = currentSetting .错误"Cannot assign value of type 'UserSettings.SocialSettings' to type 'UserSettings'"cell.passedUserSetting = currentSetting上。 You are giving context, but passedUserSetting is a UserSettings .您正在提供上下文,但passedUserSetting是一个UserSettings

But when you do:但是当你这样做时:

let currentSetting = UserSettings.SocialSettings.allCases[indexPath.row]

So, it means currentSetting is a UserSettings.SocialSettings .所以,这意味着currentSetting是一个UserSettings.SocialSettings

So you are trying to set a value of type B to expected value of type A. It won't work.因此,您尝试将 B 类型的值设置为 A 类型的预期值。它不起作用。

You can have with you enum a UserSettings with a UserSettings.SocialSettings :您可以使用UserSettings.SocialSettings UserSettings

cell.passedUserSetting = .socialSettings(currentSetting)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM