I'm triggering a local notification from my Swift 3 app in ios10 and, while I can get the notification to fire ok, I'm trying to pass a variable (which happens to be a value returned from a function) as part of the title of the notification. Instead of displaying the contents of that variable it just shows as "(Function)".
I'm using that same variable as the text of a label within the app itself without any problems.
I've created the following class and class function to schedule the notification:
class notificationController {
class func scheduleNotification(at date: Date, header: String, body: String) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: header, arguments: nil)
content.body = body
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("error: \(error)")
}
}
}
I'm then defining the variables to pass to this function in the viewdidload()
function of the view controller associated with the main screen, and then calling the scheduleNotification
function:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy HH:mm:ss"
let strDate = "25/01/2017 21:21:00"
let notificationDate = dateFormatter.date(from: strDate)
let dayNumber = String(describing: (playerArray[0]?.calculateDays)!)
let header = "Welcome to day \(dayNumber) of your life"
var body : String = ""
if(infoArray.count > 0) {
body = (infoArray[0]?.text)!
} else {body = "Doesn't it feel great to be alive?"}
notificationController.scheduleNotification(at: notificationDate!, header: header, body: body)
PlayerArray
is an array containing one Player CoreData object. I've created the following function within the Player class to work out how many days old the player is:
public class Player: NSManagedObject {
func calculateDays() -> Int {
let currentCalendar = Calendar.current
guard let dob = currentCalendar.ordinality(of: .day, in: .era, for: self.dob as! Date) else {
return 0
}
guard let today = currentCalendar.ordinality(of: .day, in: .era, for: Date()) else {
return 0
}
let days = today - dob
return days
}
}
Any reason that the dayNumber
variable won't display? I feel like it's something obvious that I've missed. Any ideas would be magic.
you need to call the function with ()
change this:
let dayNumber = String(describing: (playerArray[0]?.calculateDays)!)
to this:
let dayNumber = String(describing: (playerArray[0]?.calculateDays())!)