0

I can't figure out how to let the user choose the repeat interval of a local notification. I have linked to two images where the problems are described in details.

This is the image of the file where I need to insert the choose from the segmented control

And this is the image of the file where the segmented control is placed.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253

1 Answers1

0

I've looked through your code and one solution could be to add the value of your segmentedControl as a parameter to your TodoItem as you already do now in the gentag parameter.

Then in your addItem method you "just" need to convert from the Int value you have now to a NSCalendarUnit which you can hand to your notification.repeatInterval.

To keep this pretty you could create a new Enum who knew how to convert from Int to NSCalendarUnit values, and then your gentag parameter could be of this type.

So in the file where you have your TodoSchedulingViewController you could write something like this in the top of the file:

import UIKit

enum RepeatInterval: Int {
    case None = 0
    case Daily = 1
    case Weekly = 2

    func toCalendarUnit() -> NSCalendarUnit {
        switch self {
        case .None:
            return NSCalendarUnit(rawValue: 0)
        case .Daily:
            return NSCalendarUnit.Day
        case .Weekly:
            return NSCalendarUnit.Weekday
        }
    }
}

class TodoSchedulingViewController: UIViewController {
...

Which you could then use like this:

In your ViewController:

let todoItem = TodoItem(deadline:....,
                        title:....,
                        gentag: RepeatInterval(rawValue: segmentedControl.selectedSegmentIndex)!,  //You can force unwrap here, because you know the value is always one you get from your segmentedControl
                        UUID......)

In your TodoList class:

...
notification.userInfo = ["title" : item.title, "UUID" : item.UUID]
notification.repeatInterval = item.gentag.toCalendarUnit()

Hope that makes sense.

Oh...and next time, please post your actual code instead of images, it makes it easier to quickly see what is going on and way faster to copy code snippets from your code into an answer :)

pbodsk
  • 6,787
  • 3
  • 21
  • 51
  • Where to place the first part of your code. That starts with `enum RepeatInterval` – Mksteffensen Jun 22 '16 at 13:19
  • Hello again, I get a fail more when i use the code you provided me. Here is the code where the fail are: `gentag: RepeatInterval (rawValue: segmentedControl.selectedSegmentIndex)!,` here is the fail `Cannot convert value of type 'RepeatInterval' to expected argument type 'String'` – Mksteffensen Jun 25 '16 at 13:37
  • It is because your `gentag` parameter is a `String`. You need to change that to be of type `RepeatInterval`. That is of course assuming that you don't use that parameter somewhere else. If you do, then maybe you should add a new parameter of type `RepeatInterval` to your `TodoItem`. Hope that makes sense. – pbodsk Jun 25 '16 at 14:31
  • Not really. Now i get a new error _Contextual type 'AnyObject' cannot be used with dictionary literal_ – Mksteffensen Jun 30 '16 at 14:42