-3

How can I "conditionally" join text from 2 or more textFields into a single textView ?

I cant seem to find a way to do this, is it even possible

(conditionally meaning - join only the text|Fields that have been selected by way of switches)

I am using userDefaults and iOS switches as shown in my below code.

ViewController2 (source)

let defaults = UserDefaults.standard
    var switchON : Bool = false
    @IBAction func checkState(_ sender: AnyObject) {

        if Switch1.isOn{
            switchON = true
            defaults.set(switchON, forKey: "switch1ON")
        }
        if Switch1.isOn == false{
            switchON = false
            defaults.set(switchON, forKey: "switch1ON")
        }
        if Switch2.isOn{
            switchON = true
            defaults.set(switchON, forKey: "switch2ON")
        }
        if Switch2.isOn == false{
            switchON = false
            defaults.set(switchON, forKey: "switch2ON")

ViewController1 (destination)

override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view.

  // Select Comments -----------------------------------
        if defaults.value(forKey: "switch1ON") != nil{
            let switch1ON: Bool = defaults.value(forKey: "switch1ON")  as! Bool
            if switch1ON == true{
                let userDefaults = UserDefaults.standard
                Reference.text = userDefaults.value(forKey: "PredefinedText1") as? String
            }

        if defaults.value(forKey: "switch2ON") != nil{
            let switch2ON: Bool = defaults.value(forKey: "switch2ON")  as! Bool
            if switch2ON == true{
                let userDefaults = UserDefaults.standard
                Reference.text = userDefaults.value(forKey: "PredefinedText2") as? String
            }
           // else if switch1ON == false{
            //    let userDefaults = UserDefaults.standard
            //    Reference.text = userDefaults.value(forKey: "") as? String
            //}
        }
        }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Smithurst
  • 71
  • 1
  • 7
  • 4
    FYI - you should not be using UserDefaults just to pass data between view controllers. – rmaddy Aug 19 '19 at 22:14
  • Hi rmaddy, what is the reason for that ? I dont know any other way at this stage – Smithurst Aug 19 '19 at 22:19
  • Please see https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?r=SearchResults&s=1|392.6186 for better options of passing data. – rmaddy Aug 19 '19 at 22:24

1 Answers1

0

Ignoring the fact that you should not use UserDefaults to pass data between controllers, here are some things you need to fix in your code.

Your four if statements in ViewController2 can be replaced with these two lines:

defaults.set(switch1.isOn, forKey: "switch1ON")
defaults.set(switch2.isOn, forKey: "switch2ON")

In ViewController1 there is a much simpler way to get the two settings:

let switch1ON = defaults.bool(forKey: "switch1ON")
let switch2ON = defaults.bool(forKey: "switch2ON")

Don't use value(forKey:) unless you have a clearly understood need to use key-value coding.

Then get the two strings:

let text1 = userDefaults.string(forKey: "PredefinedText1")
let text2 = userDefaults.string(forKey: "PredefinedText2")

There are several ways to code the logic for conditionally combing the strings. Here's on way:

var textVals = [String]()
if let text1 = text1, switch1ON {
    textVals.append(text1)
}
if let text2 = text2, switch2ON {
    textVals.append(text2)
}

Results.text = textVals.joined(separator: " ")
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks Maddy, I’ll fix my code per your recommendations and if solved will tick your answer. I really appreciate your assistance too, thanks – Smithurst Aug 20 '19 at 01:37
  • Thanks Maddy, i just fixed my code per your recommendations, works perfectly, i just have to make 1 adjustment to your code, was a typo on the switch name - "switch21ON" should be "switch1ON" - but all good now - Thanks so much – Smithurst Aug 20 '19 at 08:29