Tarvo Mäesepp Tarvo Mäesepp - 1 year ago 50
Swift Question

Why I am getting nil while unwrapping an Optional value when I eliminated all nil_s?

Hey I have problem placing variables.
How can I use variables those that I use lets say in function "A" in another function "B". In another word use variables from Function "A" in function "B"

I have function("A") to set notification text:

let sentence = textField.text! + " Some beautiful text!"

let firstWord = sentence.characters.split(" ").first.map(String.init)

LocalNotificationHelper.sharedInstance().scheduleNotificationWithKey("someText", title: "see options(left)", message: sentence, date: deadlinePicker.date, userInfo: userInfo)

Now I gotta call the
inside another function("B") in that variable here:

let predicate = CNContact.predicateForContactsMatchingName(firstWord)

I tried to add those first variables
and outside of class but for nothing. I keep getting error
"found nil while unwrapping"

I call the function where is variable
when user tap notification action button like this.

NSNotificationCenter.defaultCenter().addObserver(self, selector: "functionWhereIsPredicateVariable:", name: IDENTIFICATOR, object: nil)

Do I have to store the
or do I have to do it in another way??

Answer Source

you can save your name to userinfo

let userInfo = ["url" : "Hello","name":nameField.text!]

//Set notification
                LocalNotificationHelper.sharedInstance().scheduleNotificationWithKey("TempCo", title: "see options(left)", message:nameField.text!+" Your contact needs to be deleted!", date: deadlinePicker.date, userInfo: userInfo)

then you can get it from notification's object and post it.

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
    if identifier == DELETECONTACT {
        print("delete action - tapped")

        NSNotificationCenter.defaultCenter().postNotificationName(DELETECONTACT, object:notification.userInfo)

And you can get name from notification's object

func contactDelete(notification : NSNotification){
let name:String? = notification.object?["name"]  as? String
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download