我是Xcode编程的新手,在处理Dictionary UISplitViewController项目时遇到此错误。 我制作了一个“ Word”类和一个函数,但是当我尝试在AppDelegate.swift中组装它们时,出现以下错误:“类型'DetailViewController'的值没有成员'word'“。
每个插座已经在Main.Storyboard的Detail View Controller中正确链接到其每个标签。
//"Word" class
class Word {
let name: String
let meaning: String
let numberOfTimesTapped: String
init(name: String, meaning: String, numberOfTimesTapped: String) {
self.name = name
self.meaning = meaning
self.numberOfTimesTapped = numberOfTimesTapped
}
}
//"words" function in the DetailViewController.swift
import UIKit
let words = [
Word(name: "Home", meaning: "A place where one lives", numberOfTimesTapped: "5"),
Word(name: "Place", meaning: "A particular position, point, or area in space; a location.", numberOfTimesTapped: "3"),
Word(name: "Like", meaning: "Having the same characteristics or qualities as; similar to.", numberOfTimesTapped: "1")
]
class DetailViewController: UIViewController {
class Word {
let name: String
let meaning: String
let numberOfTimesTapped: String
init(name: String, meaning: String, numberOfTimesTapped: String) {
self.name = name
self.meaning = meaning
self.numberOfTimesTapped = numberOfTimesTapped
}
}
@IBOutlet weak var WordLabel: UILabel!
@IBOutlet weak var DescriptionLabel: UILabel!
@IBOutlet weak var NumberOfTimesTappedLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
var word: Word? {
didSet {
refreshUI()
}
}
func refreshUI() {
loadViewIfNeeded()
WordLabel.text = word?.name
DescriptionLabel.text = word?.meaning
NumberOfTimesTappedLabel.text = word?.numberOfTimesTapped
}
//Content inside of MasterViewController.swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
guard let splitViewController = window?.rootViewController as? UISplitViewController,
let leftNavController = splitViewController.viewControllers.first as? UINavigationController,
let masterViewController = leftNavController.topViewController as? MasterViewController,
let detailViewController = splitViewController.viewControllers.last as? DetailViewController
else { fatalError() }
let firstWord = masterViewController.words.first
detailViewController.word = firstWord //THIS IS WHERE I GET THE ERROR "Value of type 'DetailViewController' has no member 'word'"
return true
}
答案 0 :(得分:0)
您的变量不可见,因为它位于方法viewDidLoad
内,这使其仅在viewDidLoad
范围内可见。
我想您需要将它作为控制器的属性放在类范围内,因此只需将其与方法refreshUI()
一起从方法移到类范围
class DetailViewController: UIViewController {
var word: Word? {
didSet { refreshUI() }
}
func refreshUI() {
...
}
}