-2

In the Reminders app on iOS you can add a new reminder then it will add all the info you input to the reminders list. So I know a very long and inefficient method of doing this and I thinking there must be an easier way to do this.

Here's The Long and inefficient way

var reminder1: String?
var reminder2: String?
// and so on and so on...

if (reminder1 != nil) && (reminder2 != nil) {
// if user has inputed data show the reminders
// if not show nothing
}

But how can I do this so I don't have to create hundreds of variables?

if it helps what I need for each reminder here it is: Title, subtitle, description, reminderIcon, reminder icon colour, startTime, endTime, group (as in what folder/collection of reminder this is), group icon and group icon colour

Ash
  • 81
  • 9
  • 2
    As a rule of thumb, if you're numbering individual variables, you are definitely in need of list-type data structures a la an [`Array`](https://developer.apple.com/documentation/swift/array) of objects holding all the data points about each of your user's reminders. – esqew Oct 18 '22 at 18:05

1 Answers1

1

Use an Array instead:

var reminders: [Reminder] = []

if !reminders.isEmpty {
    // if user has inputed data show the reminders
} else {
    // if not show nothing
}
esqew
  • 42,425
  • 27
  • 92
  • 132
  • how can users add to the array would it be += ... – Ash Oct 18 '22 at 18:11
  • 1
    @Ash Have you tried referring to the linked documentation? How about the [`append` method](https://developer.apple.com/documentation/swift/array/append(_:)-1ytnt)? `reminders.append(myReminderObject)`? [The documentation also indicates the `+=` operator should work.](https://developer.apple.com/documentation/swift/array/+=(_:_:)-676ib) – esqew Oct 18 '22 at 18:12
  • would this save when the user leaves the app. if not how would I do that – Ash Oct 18 '22 at 18:21
  • 1
    @Ash No, and that would constitute an entirely separate question altogether, and would be closed as a duplicate of https://stackoverflow.com/questions/28628225/how-to-save-local-data-in-a-swift-app – esqew Oct 18 '22 at 18:24
  • How would I store it in a database such as firebase as I cant get user defaults to work – Ash Oct 18 '22 at 19:20