Ios 在另一个控制器中获取常数的值总是使用Swift返回nil
在另一个VC中尝试获取historyRef的值时,它返回nil。我尝试了不同的解决方案(包括我正在使用的这个),但似乎无法获得viewDidLoad()中声明的historyRef变量的实际值 Firebase数据库有一个节点“history”,在MainVC中有一个键(childByAutoId())。我正试图在SecondVC中访问该密钥 在MainVC中,是一个常数:Ios 在另一个控制器中获取常数的值总是使用Swift返回nil,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,在另一个VC中尝试获取historyRef的值时,它返回nil。我尝试了不同的解决方案(包括我正在使用的这个),但似乎无法获得viewDidLoad()中声明的historyRef变量的实际值 Firebase数据库有一个节点“history”,在MainVC中有一个键(childByAutoId())。我正试图在SecondVC中访问该密钥 在MainVC中,是一个常数: var historyRef : FIRDatabaseReference! var ref : FIRDatabaseR
var historyRef : FIRDatabaseReference!
var ref : FIRDatabaseReference!
还声明了以下实例:
private static let _instance = MainVC()
static var instance: MainVC {
return _instance
}
viewDidLoad():
二次VC
class SecondVC: UIViewController {
var mainVC : MainVC? = nil // hold reference
override func viewDidLoad() {
super.viewDidLoad()
mainVC = MainVC() // create MainVC instance
getUserHistoryIds()
}
func getUserHistoryIds() {
let historyKey = mainVC?.historyRef
print("HistoryKey: \(String(describing: historyKey))")
}
}
打印输出:
HistoryKey: nil
我的数据库:
my-app
- history
+ LSciQTJwR0VqwaAfKVz
let historyRef = ref.child("history")
historyRef.observe(.value) { (snapshot) in
if snapshot.exists() {
for history in snapshot.children {
let snap = history as! FIRDataSnapshot
let _ = snap.value as! [String: Any] // dict
let historyKey = snap.key
print("History Key: \(historyKey)")
}
} else {
print("There are none")
}
}
编辑
我不是从另一个控制器那里得到的,而是从Firebase得到的
我能够获得childAutoById的值,但它列出了所有这些值,而不仅仅是当前值:
let historyRef = ref.child("history")
historyRef.observe(.value) { (snapshot) in
if snapshot.exists() {
for history in snapshot.children {
let snap = history as! FIRDataSnapshot
let _ = snap.value as! [String: Any] // dict
let historyKey = snap.key
print("History Key: \(historyKey)")
}
} else {
print("There are none")
}
}
您正在viewDidLoad方法中初始化MainVC::historyRef,但仅从SecondVC实例化MainVC不会导致加载或显示MainVC 您可以使用mainVC=mainVC.instance,但您依赖于先前加载的mainVC实例,而不是以其他方式丢弃的实例
我希望提取与VC无关的任何模型用法,并在需要时作为序列的一部分传递给VC。初始化MainVC时,将历史参考从viewDidLoad移动到init()。这样,只要实例化视图控制器,该方法就会运行。还要确保这不是一个异步调用,或者添加一个完成回调来知道何时完成,正如您提到的您正在使用firebase
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
historyRef = ref.child("history").childByAutoId()
}
当您实例化MainVC但从不加载它时,historyRef将永远不会运行
也不建议将视图控制器作为静态控制器。我宁愿通过初始值设定项或视图模型向下传递变量。从我的编辑中可以看出,我无法从控制器之间的传递中获得所需的内容,所以我只是从firebase数据库中获得:
my-app
- history
+ LSciQTJwR0VqwaAfKVz
let historyRef = ref.child("history")
historyRef.observe(.value) { (snapshot) in
if snapshot.exists() {
for history in snapshot.children {
let snap = history as! FIRDataSnapshot
let _ = snap.value as! [String: Any] // dict
let historyKey = snap.key
print("History Key: \(historyKey)")
}
} else {
print("There are none")
}
}
这会得到所有的随机键,而不是当前的,这是我可以使用的。请原谅我的愚蠢问题,但续集不是用来转到另一个控制器的吗?我只想把数据从主数据拉到第二数据。没有办法获得变量的首选值或最后一个值吗?即使没有显示,您也会依赖以前的VC在内存中徘徊。那太糟糕了。试着找到一个更好的方法。这可能是通过在故事板的早期某处创建一个模型,然后在加载/呈现时将加载的模型传递给VC(例如SecondVC)(可能通过segue)。如果您的流始终显示MainVC,然后作为MainVC上的交互的结果显示SecondVC,那么您可以在segue中将模型传递给SecondVC,而不是让SecondVC了解其前身。在不了解流程的情况下,我无法告诉您加载/存储模型的最佳位置。我尝试了此操作,但在historyRef行上出现了此错误:线程1:致命错误:在展开可选值时意外发现了nil-可能是因为它尚未创建?您实例化了ref变量吗?在使用它之前,您需要先实例化它。因为ref和historyRef都是引用,所以我将historyRef声明为var historyRef:FIRDatabaseReference!=FIRDatabase.database().reference()然后重写init(nibName-nibNameOrNil:String?,bundle-nibBundleOrNil:bundle?{historyRef=historyRef.child(“历史”).childByAutoId()self.init()}所需的init?(编码器aDecoder:NSCoder){fatalError(“init(编码器:)尚未实现”)}然后我在var locationManager:CLLocationManager!=同一控制器中的CLLocationManager():EXC_BAD_access您可以发布更多代码或发送到存储库的链接吗?我已经能够获取childByAutoId的值,但它列出了所有的值,而不仅仅是当前的值。。。请参见编辑#1