Swift Firebase数据和意外的零

Swift Firebase数据和意外的零,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,我的最终目标是从MapKit在地图上显示注释。但是,从firebase加载数据不起作用。 代码如下: var hotels = [Hotel]() override func viewDidLoad() { super.viewDidLoad() map.delegate = self checkLocationServices() observe() } 观察功能如下所示: func observe() { Data().getAllHote

我的最终目标是从MapKit在地图上显示注释。但是,从firebase加载数据不起作用。 代码如下:

var hotels = [Hotel]()

override func viewDidLoad() {
    super.viewDidLoad()
    map.delegate = self
    checkLocationServices()
    observe()
}
观察功能如下所示:

func observe() {
        Data().getAllHotel { (hotelA) -> (Void) in
            if let hotel = hotelA {
                print("hotel: ", hotel)
                self.hotels.append(hotel)
                let hotelAnnotation = MKPointAnnotation()
                hotelAnnotation.title = "\(hotel.hotelName)"
                hotelAnnotation.coordinate = CLLocationCoordinate2D(latitude: hotel.latitude, longitude: hotel.longitude)
                self.map.addAnnotation(hotelAnnotation)
            }
        }
}
请注意,if语句中的打印不是空的。 然而,行
hotelanotation.title
导致了一个意外的错误nil,我不明白为什么

此外,这里还有数据类中的
getAllHotel
函数:

func getAllHotel(closure: HotelClosure?) {
        let ref = Ref().databaseHotels
        ref.observe(.childAdded) { (snapshot) in
            if let dict = snapshot.value as? Dictionary<String, AnyObject> {
                let hotel = self.fromDictToHotel(key: snapshot.key, dict: dict)
                closure?(hotel)
            } else {
                closure?(nil)
            }
        }
    }

    func fromDictToHotel(key: String, dict: Dictionary<String, AnyObject>) -> Hotel? {
        guard let hotelName = dict["name"] as? String else { return nil }
        guard let latitude = dict["latitude"] as? Double else { return nil }
        guard let longitude = dict["longitude"] as? Double else { return nil }
        let hotelDescription = dict["description"] as? String
        let hotelGrade = dict["grade"] as? Double
        let hotelImageUrl = dict["hotelImageUrl"] as? String

        let hotel = Hotel(hid: key, hotelName: hotelName, hotelDescription: hotelDescription, latitude: latitude, longitude: longitude, grade: hotelGrade, url: hotelImageUrl)
        return hotel
    }
func getAllHotel(关闭:HotelClosure?){
设ref=ref().数据库
中的ref.observe(.childAdded){(快照)
如果让dict=snapshot.value作为字典{
让hotel=self.fromDictToHotel(key:snapshot.key,dict:dict)
关闭?(酒店)
}否则{
结案?(无)
}
}
}
func fromDictToHotel(键:String,dict:Dictionary)->Hotel?{
guard let hotelName=dict[“name”]as?String else{return nil}
guard let latitude=dict[“latitude”]as?Double-else{return nil}
guard let longitude=dict[“longitude”]as?Double-else{return nil}
将hotelDescription=dict[“description”]设为?字符串
让hotelGrade=dict[“grade”]作为双精度
让hotelImageUrl=dict[“hotelImageUrl”]作为?字符串
let hotel=hotel(hid:key,hotelName:hotelName,hotelDescription:hotelDescription,纬度:纬度,经度:经度,等级:hotelGrade,url:hotelImageUrl)
返回酒店
}

提前感谢您的帮助

您是否验证了
hotel.hotelName
是否有效且不为空?能否举例说明您在
getAllHotel
中打印语句的结果?此外,酒店定义中是否强制展开了
hotelName
(例如:
var hotelName:String!
)?如果您在
observe()
的第4行设置断点,并在中断发生时键入
po hotel.hotelName
,则输出结果是什么?@Jay hotel.hotelName不应为零如果按照hotel的定义打印某些酒店,则名称不应为零optional@Zoe,控制台的输出是hotel:ProjectName。hotel致命错误:在隐式展开可选值时意外发现nil:…我添加了!在var hotelName上,酒店定义中的纬度和经度,现在错误出现在hotelanotation.coordination线上,但问题相同嘿,各位,我找到了问题所在。Hotel是Item的一个子类。因此,打印hotelName为零,但打印itemName不是零。很抱歉,这是一个概念错误!