所以我有一個類,我想創建一個實例,只要應用程序啓動。我想從我的ViewController中的開關/切換初始化它的一些屬性。我在哪裏申報啓動應用程序的變量,訪問ViewController屬性?
這裏是類,其中我試圖創建一個實例:
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)"
}
}
所以: 把申報AppDelegate.swift
給了我一個錯誤(「unresoved標識的使用‘property1Switch’ 「):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
var thePropertyCollection: PropertyCollection = PropertyCollection(property1: property1Switch.isOn)
return true
}
添加」ViewController「。在「property1switch」前面也沒有幫助。我得到另一個錯誤
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
var thePropertyCollection: PropertyCollection = PropertyCollection(property1: ViewController.property1Switch.isOn)
return true
}
(「實例成員‘property1Switch’不能在類型‘視圖控制器’使用」),但我並不想對類型「視圖控制器「使用型‘property1Switch’ 「」。我以爲這是你如何引用其他類的東西?
請分享一下PropertyCollection類的代碼嗎? PropertyCollection是一個控制器類? –
已添加類。我不知道控制器班是什麼,但我的猜測是否定的。 – Marmelador