Notifications - Objective C vs Swift

Team , I’m working on a new version of checklist , but now using swift , in my old version in Objective C i did something to show 2 informations , my list and the item , like this :

  • (void)scheduleNotification
    {
    UILocalNotification *existingNotification = [self notificationForThisItem];
    if (existingNotification != nil) {
    NSLog(@“Found an existing notification %@”, existingNotification);
    [[UIApplication sharedApplication] cancelLocalNotification:existingNotification];
    }

    if (self.shouldRemind && [self.dueDate compare:[NSDate date]] != NSOrderedAscending) {

      UILocalNotification *localNotification = [[UILocalNotification alloc] init];
      localNotification.fireDate = self.dueDate;
      localNotification.timeZone = [NSTimeZone defaultTimeZone];
      **localNotification.alertBody = [NSString stringWithFormat:@"%@, %@", self.checklist.name, self.text];**
      localNotification.soundName = UILocalNotificationDefaultSoundName;
      localNotification.userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:self.itemId] forKey:@"ItemID"];
    
    
      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    
      NSLog(@"Scheduled notification %@ for ItemID %d", localNotification, self.itemId);
    

    }
    }

so , this is the code in swift , I don’t know how to do that !!!

func scheduleNotification() {
removeNotification()

if shouldRemind && dueDate > Date() {
  
  let content = UNMutableNotificationContent()
  content.title = "Reminder:"
  **content.body = text**
  content.sound = UNNotificationSound.default()
  
  let calendar = Calendar(identifier: .gregorian)
  let components = calendar.dateComponents(
    [.month, .day, .hour, .minute], from: dueDate)
  
  let trigger = UNCalendarNotificationTrigger(
    dateMatching: components, repeats: false)
  let request = UNNotificationRequest(
    identifier: "\(itemID)", content: content, trigger: trigger)
  
  let center = UNUserNotificationCenter.current()
  center.add(request)
  
  print("Scheduled notification \(request) for itemID \(itemID)")
}

}

What is it that you don’t know how to do in Swift? It’s not immediately clear from those snippets alone.

Sorry , my point is how to show two informations in the same notification

this is my code in Objective C
localNotification.alertBody = [NSString stringWithFormat:@“%@, %@”, self.checklist.name, self.text];

In swift I have content.body = text , I’m following Matthijs 's guide

Try this:

localNotification.alertBody = String(format:"%@, %@", self.checklist.name, self.text)

thanks , works for Objectve C , but not for swift i have this message below

That is not really a Swift vs Obj-C issue, though. You’re asking ChecklistItem for its checklist property, which it doesn’t have.

yes , thanks again , I will try to find the right thing.

quick comment , I was trying to do something like this : var checklist: Checklist! , but not work

Matthijs , in Obj C we just use #import Checklist.h to work , but talking about swift i did a call like i had mentioned before “var checklist: Checklist!” , but i have the same error message , i think because they don’t know the value of this field.

not work, always the value of checklist.name is null , this is the reason of the error , I’m trying to create a var called checklist : Checklist( )…

After some changes the error persist , I don’t know how can I do this call in Swift

The compiler errors is that ChecklistItem does not have a property called checklist, so you can’t ask it for self.checklist.name.

If you want ChecklistItem to know the name of the Checklist it belongs to, you’ll have to make that property first and fill it in with the correct Checklist object,