ios - 类型“示例”没有成员“共享实例”
问题描述
我创建了类并编写了名为“示例”的单例函数
import UIKit
class example: NSObject {
class example {
static let sharedInstance = example()
var userInfo = (ID: "bobthedev", Password: 01036343984)
// Networking: communicating server
func network() {
// get everything
}
private init() { }
}
}
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
example.sharedInstance.userInfo
// (ID "bobthedev", Password 01036343984)
// ViewController One
example.sharedInstance.userInfo.ID // "bobthedev"
}
但我收到错误* 类型 'example' 没有成员 'sharedInstance' *
https://learnswiftwithbob.com/course/object-oriented-swift/singleton-pattern.html
解决方案
您创建了一个嵌套类。只需跳过内部声明:
import UIKit
class Example: NSObject {
static let sharedInstance = Example()
var userInfo = (ID: "bobthedev", Password: 01036343984)
// Networking: communicating server
func network() {
// get everything
}
private override init() { }
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
Example.sharedInstance.userInfo
// (ID "bobthedev", Password 01036343984)
// ViewController One
Example.sharedInstance.userInfo.ID // "bobthedev"
}
}
推荐阅读
- qt - 按下鼠标时不会更新 QML 图像源
- python - 如何在python中定义许多文本使用“in”条件
- node.js - How can I stop axios from retrying the request after 2 mins
- python-3.x - TypeError: 'Column' object is not callable when use 'case-when'
- python - 当我创建一个新的 Docker 容器时,Pip 会安装过时的库
- docker - Traefik Web UI Server Not Found
- go - 说明:函数在 go 中返回相同的函数
- css - 捆绑不渲染样式
- mysql - 在 nodejs 查询之外显示 mysql 结果以供稍后使用
- node.js - 具有多个批量读取和写入的 Cloud Function http 请求