Il parametro object
nella funzione che si sta utilizzando è il mittente, l'oggetto che invia la notifica, non il parametro. Controlla i documenti here.
Si dovrebbe mettere il valore enum che si desidera inviare come parametro nel dizionario informazioni utente e utilizzare il seguente metodo:
func postNotificationName(_ aName: String,
object anObject: AnyObject?,
userInfo aUserInfo: [NSObject : AnyObject]?)
Nel tuo caso:
let userInfo = ["RuleError" : RuleError.Create.rawValue]
NSNotificationCenter.defaultCenter().postNotificationName("RuleFailNotification",
object: self,
userInfo:userInfo)
E per gestire il notifica, prima registrarsi per questo:
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "handleRuleFailNotification:",
name: "RuleFailNotification",
object: nil)
Quindi gestirlo:
func handleRuleFailNotification(notification: NSNotification) {
let userInfo = notification.userInfo
RuleError(rawValue: userInfo!["RuleError"] as! String)
}
Questa è una sfortunata necessità. Le notifiche costringono enumerazioni e strutture in uno stato di seconda classe. Ritengo che questa sia una grave lacuna in Swift, dal momento che gli Enum sono spesso solo un Int o String limitato, eppure questi vanno bene come valore. – BaseZen