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

How to get variable value right after app launch

Hey I have problem placing variables and get those as value after app launch.
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". Right now I use them on class scope.

I have those variables on class scope:

var contacts:[NSString] = []
var sentence:String = ""
var firstWord = [String]()
var firstWordAsString:String = ""

Now on function "A" I append
like this:

defaults.setObject(contacts, forKey: "key")

and in here I trigger notification also like this:

sentence = nameField.text! + " needs to be deleted."
var firstWordAsString = sentence.characters.split(" ").first.map(String.init)
defaults.setObject(firstWord, forKey: "firstWord")

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

Now in function "B" which activates on notification button click I would like to get this
as variable from notification since I need to delete that word from array:

if contacts.contains(firstWordAsString){

let predicate = CNContact.predicateForContactsMatchingName(firstWordAsString)

let toFetch = [CNContactEmailAddressesKey]

defaults.setObject(contacts, forKey: "key")


Here I show you what I need to do in picture:
enter image description here


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