Don't use Swift enums to box magic strings! Read the blog post: http://www.chibicode.org/?p=16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum NotificationNames: String { | |
case UserDataChanged: "UserDataChangedNotificationName" | |
case ReceivedAlert: "ReceivedAlertNotificationName" | |
case PeanutButterJellyTime: "ItsPeanutButterJellyTimeNotificationName" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct NotificationNames { | |
static let userDataChanged = "UserDataChangedNotificationName" | |
static let receivedAlert = "ReceivedAlertNotificationName" | |
static let peanutButterJellyTime = "ItsPeanutButterJellyTimeNotificationName" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using the enum: | |
NSNotificationCenter.defaultCenter().postNotificationName(NotificationNames.UserDataChanged.rawValue, object:nil) | |
// Using the struct | |
NSNotificationCenter.defaultCenter().postNotificationName(NotificationNames.userDataChanged, object:nil) | |
// If you need switching, remember that Swift can switch on just about any comparison type: | |
func handleNotification(note: NSNotification) { | |
switch note.name { | |
case NotificationNames.userDataChanged: print("User data changed!") | |
case NotificationNames.receivedAlert: print("WHAT!? An alert!") | |
case NotificationNames.peanutButterJellyTime: print("Where ya at? Where ya at? Where ya at?") | |
default: print("I have no idea what's going on.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment