0

So I have a class of which I want to create an instance as soon as the app launches. I would like to initialize some of its properties from switches/toggles in my ViewController.

Here is the class, of which I am trying to create an instance:

import Foundation

class PropertyCollection {
    var property1: Bool

    init (property1: Bool) {
        self.property1 = property1

    }

    func disableAll() {
        self.property1 = false

    }

    func enableAll() {
        self.property1 = true

    }

    func info() -> String {
        return "The properties are: \(property1)"
    }

}

So: Putting the declaration in AppDelegate.swift gives me an error ("Use of unresoved identifier 'property1Switch'"):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        var thePropertyCollection: PropertyCollection = PropertyCollection(property1: property1Switch.isOn)

        return true
    }

Adding "ViewController." in front of "property1switch" doesn't help either. I get another error ("Instance Member 'property1Switch' cannot be used on type 'ViewController'")

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    var thePropertyCollection: PropertyCollection = PropertyCollection(property1: ViewController.property1Switch.isOn)

    return true
}

But I am not trying to "use type 'property1Switch' on type 'View Controller'". I thought this was how you reference stuff from other classes?

Marmelador
  • 947
  • 7
  • 27

2 Answers2

0

It should be like this:

See you are initializing this object, so you must provide it initial value.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        var thePropertyCollection: PropertyCollection = PropertyCollection(property1: true)

        return true
}
Dheeraj D
  • 4,386
  • 4
  • 20
  • 34
  • That is not what I am trying to do. I do wish to provide an initial value, but it should come from `property1Switch` which is a property of `ViewController`. That is where the problem arises. – Marmelador Apr 07 '17 at 08:28
0

You may be in dealing with a similar issue as this one: 'Use of Unresolved Identifier' in Swift One possible issue is that your new class has a different Target(s) from the other one.

For example, it might have a testing target and the other doesn't. For this specific case, you have to include all of your classes in the testing target or none of them.

Community
  • 1
  • 1
Ding Qiu
  • 64
  • 3