Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. // Option 1: if..elseif + optional unwrapping conditions
  2.  
  3. private func handleNotificationAction(id: String?, userInfo: [NSObject: AnyObject], responseInfo: [NSObject: AnyObject]?, completion: () -> Void) {
  4. let taskId = userInfo["extra"]?["task_hash"] as? String
  5. let projectId = userInfo["extra"]?["project_hash"] as? String
  6. let comment = responseInfo?["UIUserNotificationActionResponseTypedTextKey"] as? String
  7.  
  8. if id == "comment", let task = taskId, comment = comment where !comment.isEmpty {
  9.  
  10. } else if id == "invitation_accept", let project = projectId {
  11.  
  12. } else if id == "invitation_decline", let project = projectId {
  13.  
  14. } else if id == "star", let task = taskId {
  15.  
  16. } else if id == "complete", let task = taskId {
  17.  
  18. } else if id == "show" || id == "comment" || id == "delegate", let task = taskId {
  19.  
  20. } else {
  21. // assertionFailure
  22. }
  23. }
  24.  
  25. // Option 2: switch + force unwrap
  26.  
  27. private func handleNotificationAction(id: String?, userInfo: [NSObject: AnyObject], responseInfo: [NSObject: AnyObject]?, completion: () -> Void) {
  28. let taskId = userInfo["extra"]?["task_hash"] as? String
  29. let projectId = userInfo["extra"]?["project_hash"] as? String
  30. let comment = responseInfo?["UIUserNotificationActionResponseTypedTextKey"] as? String
  31.  
  32. switch id {
  33. case "comment"? where taskId != nil && comment != nil && !comment.isEmpty:
  34.  
  35. case "invitation_accept"? where projectId != nil:
  36.  
  37. case "invitation_decline"? where projectId != nil:
  38.  
  39. case "star"? where taskId != nil:
  40.  
  41. case "complete"? where taskId != nil:
  42.  
  43. case "show"?, "comment"?, "delegate"? where taskId != nil:
  44.  
  45. default:
  46. // assertionFailure
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement