[英]Swift - Iterate through all cases of a nested enumeration
我有以下嵌套枚舉設置:
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
}
}
我正在嘗試通過以下代碼將“UserSettings”值傳遞給cellForRowAt
中的單元格:
let cell = settingsTableView.dequeueReusableCell(withIdentifier: "yesNoSettingsCell", for: indexPath) as! YesNoSettingsCell
let currentSetting = UserSettings.SocialSettings.allCases[indexPath.row]
cell.passedUserSetting = currentSetting
return cell
上面的代碼產生以下錯誤:
"Cannot assign value of type 'UserSettings.SocialSettings' to type 'UserSettings'"
將cellForRowAt
中的代碼更改為:
let currentSetting = UserSettings.socialSettings().allCases[indexPath.row]
產生以下錯誤:
"Missing argument for parameter #1 in call"
我錯過了什么? 如何枚舉嵌套枚舉的所有情況?
錯誤"Cannot assign value of type 'UserSettings.SocialSettings' to type 'UserSettings'"
在cell.passedUserSetting = currentSetting
上。 您正在提供上下文,但passedUserSetting
是一個UserSettings
。
但是當你這樣做時:
let currentSetting = UserSettings.SocialSettings.allCases[indexPath.row]
所以,這意味着currentSetting
是一個UserSettings.SocialSettings
。
因此,您嘗試將 B 類型的值設置為 A 類型的預期值。它不起作用。
您可以使用UserSettings.SocialSettings
UserSettings
cell.passedUserSetting = .socialSettings(currentSetting)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.