0

On second time I tap button I do not have value anymore. How to Load variable on every app load. The variable I am talking about is changing since it is always textField.text! and it is user input. I use it when user taps LocalNotification action, app opens and then function triggers like this:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "someFunctionWhereIsMyProblematicVariableINeedToLoad:", name: IDENTIFIER, object: nil)

How to stick the variable every time user enters it, into local notification. Are every notification somehow different or just text changes?Are there any ID-s to make every notification special?

LocalNotificationHelper.sharedInstance().scheduleNotificationWithKey("someText", title: "see options(left)", message:textField.text!+"someText", date: deadlinePicker.date, userInfo: userInfo)

I need that textField.text to be in this variable: let predicate = CNContact.predicateForContactsMatchingName(textField.text!)

I tried to store them into NSUserDefaults and into arrays and loop through arrays to find if value exists etc. But It works only first time and second time it is nil

Edit: It keeps only the last entered value as variable. How to keep them all?

Here I show you in the pictures what I tried to explain with words:

enter image description here

Now if blue button is tapped I start function where I need to use the "firstTimeEntered" as variable but at the second notification it is"SecondTimeEntered"

Variables class scope:

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

Function "A":

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

            let userInfo = ["url" : "Hello"]

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

Function "B":

defaults.objectForKey("firstWord") as? String

        if contacts.contains(firstWordAsString){

        let predicate = CNContact.predicateForContactsMatchingName(firstWordAsString)
Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92

1 Answers1

1

This is ultimately an issue of scope.

//This is the largest scope called the global scope. anything available here is available anywhere in your application.
class myclass {
    //this is the class level scope any variables here would be available from within the class but not outside of it. I can use any variables in the class scope or the global scope.
    func myFunction() {
        //this is the function scope, any variables here would be available from within the function but not outside of this function. I can use any variables in the class scope, global scope and my own scope.
    }

    func mySecondFunction() {
        //this is also the function scope, I can have my own variables but I cannot see the variables in myFunction()
    }

So if you were to put a var savedValues = [String]() at the top of a function it would not be available from another function. but if you put the same in the class scope, it would be available in both functions. Each time a function starts, it will define the function variables and when it exits itself, the variables are removed from memory.

to solve your issue put an array of strings within your class, and then use the append method of that class level array to add new things to it. you could then search against that array by using the filter method or looping though it.

Robert Masen
  • 139
  • 8
  • Masen, How can I then search for that word I just got from notification to delete it from array? I tried it like that: if contacts.contains(firstWord) (which I can't do since it is in array). Then delete it from array. – Tarvo Mäesepp Jun 13 '16 at 14:15
  • There isn't a `.remove(object)` method for arrays instead they rely on `removeAtIndex(index)` method. You would need to find the index of an object and then call this. `if let found = array.indexOf(searchTerm) { array.removeAtIndex(found) }` would probably do the trick. – Robert Masen Jun 13 '16 at 16:27
  • I know but it only remembers last entered input. And takes it as variable value. – Tarvo Mäesepp Jun 14 '16 at 15:46
  • I am not sure what you are asking here. What only remembers the last entered input, your array? they you need to examine what scope the array is in. Are you asking something else? – Robert Masen Jun 14 '16 at 17:38
  • Every notification has different first word in sentence and it has to be variable. So if first notification has first word "first". It has to be in function. On second notification it is "second" and variable value must be second. Do you understand? The point is that It has name and it name must be deleted from contacts. Everything else is done but now I need it as variable. – Tarvo Mäesepp Jun 14 '16 at 18:15
  • I am not following. if your array is in the class scope, when your function is called, you can access all of the stored values in the array. If you needed one with a particular value, you could just use a similar method to what I posted above `if let found = array.indexOf(searchTerm) { let wordExists = array[index] }` It would be helpful to see the actual code you are working with. maybe post it on github gist? – Robert Masen Jun 14 '16 at 18:44
  • It is good idea. Check it here: https://github.com/MaeseppTarvo/TempCo/tree/master Code is in ViewController.swift – Tarvo Mäesepp Jun 14 '16 at 18:59