class BibliothequesViewController: UIViewController {
static let sharedInstance = BibliothequesViewController()
var presentedBy: UIViewController?
}
I tried to access, both sharedInstance
and presentedBy
inside the extension:
extension BibliothequesViewController: UITableViewDelegate, UITableViewDataSource {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//Trying to present by using presentBy property
let vc = segue.destination as! presentedBy
// This throws this error: Use of undeclared type 'presentedBy'
let vc = segue.destination as! BibliothequesViewController.sharedInstance.presentedBy
// This throws this error: Static property 'sharedInstance' is not a member type of 'BibliothequesViewController'
}
}
The first error makes sense.
The second one doesn't as I have used BibliothequesViewController.sharedInstance.presentedBy
in other areas in the app and it works fine.
The point is I want to know is there a way to access sharedInstance
or presentedBy
inside the extension?
The target of as
is a type, not a variable. The only thing you know about presentedBy
is that it's of type Optional<UIViewController>
. And segue.destination
is of type UIViewController. Since every type can be promoted to the optional of its type, your as
isn't doing anything. When you're done, you know it's a UIViewController. You're good to go. You can call any UIViewController methods you want, but you could do that anyway.
In short: your as!
isn't doing anything.Just get rid of it.
(As @Runt8 notes, yes, you can definitely access stored properties from an extension, but that has nothing to do with your actual question.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.