编译器崩溃,说来自FIR数据库的变量是出乎意料的零,在阅读所需值后,毫秒



这是我称之为崩溃的功能:

func retrieveOtherUserData(){
    profileRef.queryOrdered(byChild: "uid").queryEqual(toValue: "aRandomUserID6388").observe(.value, with:{
            snapshot in
            print("snapshot.value is (snapshot.value!)")
        for item in snapshot.children {
                let profile = Profile(snapshot: item as! FIRDataSnapshot)
                otherUID = profile!.userId!
                otherUserName = profile!.firstName!
                otherUserProfilePicURLString = profile!.fbDownloadURLForMediumProfPic!
            }
        })
}

当强制解开配置文件语句时,它始终说出意外地发现了零。这是引用的个人资料:

import Foundation
import UIKit
import Firebase
class Profile: NSObject {
var userId: String!
var firstName: String!
var age: Int!
var birthday: String!
var gender: String!
var education: String!
var thisIsMyFun: String!
var iAm: String!
var location: String!
var userLat: Double!
var userLong: Double!
var firStorageNameForFB1: String!
var firStorageNameForFB2: String!
var firStorageNameForFB3: String?
var firStorageNameForFB4: String?
var firStorageNameForFB5: String?
var firStorageNameForFB6: String?
var firStorageNameForFB7: String?
var firStorageNameForFB8: String?
var firStorageNameForFB9: String?
var firStorageNameForFB10: String?
var firStorageNameForPH1: String?
var firStorageNameForPH2: String?
var fbDownloadURLForSmallProfPic: String!
var fbDownloadURLForMediumProfPic: String!
var fbDownloadURLForLargeProfPic: String!
var firDownloadURLForFB1: String!
var firDownloadURLForFB2: String!
var firDownloadURLForFB3: String?
var firDownloadURLForFB4: String?
var firDownloadURLForFB5: String?
var firDownloadURLForFB6: String?
var firDownloadURLForFB7: String?
var firDownloadURLForFB8: String?
var firDownloadURLForFB9: String?
var firDownloadURLForFB10: String?
var firDownloadURLForPH1: String?
var firDownloadURLForPH2: String?
var profileKey: String!
var profileRef: FIRDatabaseReference!
init?(snapshot: FIRDataSnapshot) {
 guard let dict = snapshot.value as? [String: Any] else { return nil }
    guard let userId = dict["uid"] else { return nil }
    guard let firstName  = dict["name"] else { return nil }
    guard let age = dict["age"] else { return nil }
    guard let birthday = dict["birthday"] else { return nil }
    guard let gender = dict["gender"] else { return nil }
    guard let education = dict["education"]  else { return nil }
    guard let thisIsMyFun  = dict["bodyOfThisIsMyFun"]  else { return nil }
    guard let iAm = dict["bodyOfIAM"] else { return nil }
    guard let location = dict["locationOfUser"] else { return nil }
    guard let userLat = dict["latitudeOfUser"] else { return nil }
    guard let userLong = dict["longitudeOfUser"] else { return nil }
    guard let firStorageNameForFB1 = dict["firStorageNameForFBPhoto1"]  else { return nil }
    guard let firStorageNameForFB2  = dict["firStorageNameForFBPhoto2"]  else { return nil }
    guard let firStorageNameForFB3 = dict["firStorageNameForFBPhoto3"] else { return nil }
    guard let firStorageNameForFB4 = dict["firStorageNameForFBPhoto4"] else { return nil }
    guard let firStorageNameForFB5 = dict["firStorageNameForFBPhoto5"] else { return nil }
    guard let firStorageNameForFB6 = dict["firStorageNameForFBPhoto6"]  else { return nil }
    guard let firStorageNameForFB7  = dict["firStorageNameForFBPhoto7"]  else { return nil }
    guard let firStorageNameForFB8 = dict["firStorageNameForFBPhoto8"] else { return nil }
    guard let firStorageNameForFB9 = dict["firStorageNameForFBPhoto9"] else { return nil }
    guard let firStorageNameForFB10 = dict["firStorageNameForFBPhoto10"] else { return nil }
    guard let firStorageNameForPH1  = dict["firStorageNameForPHPhoto1"]  else { return nil }
    guard let firStorageNameForPH2 = dict["firStorageNameForPHPhoto2"] else { return nil }
    guard let fbDownloadURLForSmallProfPic = dict["URLofSmallFBProfPic"] else { return nil }
    guard let fbDownloadURLForMediumProfPic = dict["URLofMediumFBProfPic"] else { return nil }
    guard let fbDownloadURLForLargeProfPic = dict["URLofLargeFBProfPic"]  else { return nil }
    guard let firDownloadURLForFB1  = dict["firDownloadURLStringForFBPic1"]  else { return nil }
    guard let firDownloadURLForFB2 = dict["firDownloadURLStringForFBPic2"] else { return nil }
    guard let firDownloadURLForFB3 = dict["firDownloadURLStringForFBPic3"] else { return nil }
    guard let firDownloadURLForFB4 = dict["firDownloadURLStringForFBPic4"] else { return nil }
    guard let firDownloadURLForFB5 = dict["firDownloadURLStringForFBPic5"] else { return nil }
    guard let firDownloadURLForFB6  = dict["firDownloadURLStringForFBPic6"]  else { return nil }
    guard let firDownloadURLForFB7 = dict["firDownloadURLStringForFBPic7"] else { return nil }
    guard let firDownloadURLForFB8 = dict["firDownloadURLStringForFBPic8"] else { return nil }
    guard let firDownloadURLForFB9 = dict["firDownloadURLStringForFBPic9"] else { return nil }
    guard let firDownloadURLForFB10 = dict["firDownloadURLStringForFBPic10"] else { return nil }
    guard let firDownloadURLForPH1 = dict["firDownloadURLStringForPHPic1"] else { return nil }
    guard let firDownloadURLForPH2 = dict["firDownloadURLStringForPHPic2"] else { return nil }

    self.userId = userId as! String
    self.firstName = firstName as! String
    self.age = age as! Int
    self.birthday = birthday as! String
    self.gender = gender as! String
    self.education = education as! String
    self.thisIsMyFun  = thisIsMyFun as! String
    self.iAm = iAm as! String
    self.location = location as! String
    self.userLat = userLat as! Double
    self.userLong = userLong as! Double
    self.firStorageNameForFB1 = firStorageNameForFB1 as! String
    self.firStorageNameForFB2 = firStorageNameForFB2 as! String
    self.firStorageNameForFB3 = firStorageNameForFB3 as? String
    self.firStorageNameForFB4 = firStorageNameForFB4 as? String
    self.firStorageNameForFB5 = firStorageNameForFB5 as? String
    self.firStorageNameForFB6 = firStorageNameForFB6 as? String
    self.firStorageNameForFB7 = firStorageNameForFB7 as? String
    self.firStorageNameForFB8 = firStorageNameForFB8 as? String
    self.firStorageNameForFB9 = firStorageNameForFB9 as? String
    self.firStorageNameForFB10 = firStorageNameForFB10 as? String
    self.firStorageNameForPH1 = firStorageNameForPH1 as? String
    self.firStorageNameForPH2 = firStorageNameForPH2 as? String
    self.fbDownloadURLForSmallProfPic = fbDownloadURLForSmallProfPic as! String
    self.fbDownloadURLForMediumProfPic = fbDownloadURLForMediumProfPic as! String
    self.fbDownloadURLForLargeProfPic = fbDownloadURLForLargeProfPic as! String
    self.firDownloadURLForFB1 = firDownloadURLForFB1 as! String
    self.firDownloadURLForFB2 = firDownloadURLForFB2 as! String
    self.firDownloadURLForFB3 = firDownloadURLForFB3 as? String
    self.firDownloadURLForFB4 = firDownloadURLForFB4 as? String
    self.firDownloadURLForFB5 = firDownloadURLForFB5 as? String
    self.firDownloadURLForFB6 = firDownloadURLForFB6 as? String
    self.firDownloadURLForFB7 = firDownloadURLForFB7 as? String
    self.firDownloadURLForFB8 = firDownloadURLForFB8 as? String
    self.firDownloadURLForFB9 = firDownloadURLForFB9 as? String
    self.firDownloadURLForFB10 = firDownloadURLForFB10 as? String
    self.firDownloadURLForPH1 = firDownloadURLForPH1 as? String
    self.firDownloadURLForPH2 = firDownloadURLForPH2 as? String
}

}

由于某种原因,访问特定的过滤器时,几乎做同一件事。这是一个非常相似的任务和指令的工作版本:

var filterSet:FilterSet!

override func viewDidLoad() {
    super.viewDidLoad()
    filterSetRef.queryOrdered(byChild: "uid").queryEqual(toValue:   "aRandomUserID6388").observe(.value, with:{snapshot 
        in
            print("snapshot.value is (snapshot.value!)")
            for item in snapshot.children {
                let filterSet = FilterSet(snapshot: item as! FIRDataSnapshot)
                ageRangeFilterDeclined = filterSet!.declinedAgeRange!
                maxAgeMatch = filterSet!.maxAge!
                minAgeMatch = filterSet!.minAge!
                genderPref = filterSet!.genderPreference!
                includeFBFriendsInMatchResults = filterSet!.includeFBFriends!
                includeSportingEvents = filterSet!.sportingEvents!
                includeHiking = filterSet!.hiking!
                includeMiscellaneous = filterSet!.misc!
                includeMuseumsOrArtGalleries = filterSet!.museumsAndArtGalleries!
                includePlayMusic = filterSet!.music!
                includePlaySports = filterSet!.playingSports!
                includeStudyPartner = filterSet!.studyPartner!
                includeDancing = filterSet!.dancing!
                includeTheatre = filterSet!.theatre!
                includeConcerts = filterSet!.concerts!
                includeRunningBuddy = filterSet!.running!
                includeGames = filterSet!.games!
                maxDistanceFilterDeclined = filterSet!.maxDistanceDeclined!
                maxDistance = filterSet!.maximumDistance!
            }
        })
}

}

这是成功填充然后引用的filterset类,与配置文件类别不同的是:

import UIKit
import Firebase
class FilterSet: NSObject{
    var uID: String!
    var declinedAgeRange: Bool!
    var genderPreference: String!
    var includeFBFriends: Bool!
    var sportingEvents: Bool!
    var hiking: Bool!
    var misc: Bool!
    var museumsAndArtGalleries: Bool!
    var music: Bool!
    var playingSports: Bool!
    var running: Bool!
    var studyPartner: Bool!
    var dancing: Bool!
    var theatre: Bool!
    var concerts: Bool!
    var games: Bool!
    var maxDistanceDeclined: Bool!
    var maximumDistance: Int!
    var maxAge: Int!
    var minAge: Int!
    var filterSetKey: String!
    var filterRef: FIRDatabaseReference!
init?(snapshot: FIRDataSnapshot) {
    guard let dict = snapshot.value as? [String: Any] else
      { return nil }
    guard let uID  = dict["uid"]  else { return nil }
    guard let declinedAgeRange = dict["DeclinedAgeRangeFilter"] else
      { return nil }
    guard let genderPreference = dict["GenderPreference"] else 
      { return nil }
    guard let includeFBFriends = dict["IncludeFBFriendsInMatches"] else
      { return nil }
    guard let sportingEvents  = dict["IncludeAttendSportingEvent"]  else 
       { return nil }
    guard let hiking = dict["IncludeHikingPartner"] else 
       { return nil }
    guard let misc = dict["IncludeMiscellaneous"] else
       { return nil }
    guard let museumsAndArtGalleries =   
        dict["IncludeMuseumsAndArtGalleries"] else { return nil }
    guard let music = dict["IncludePlayingMusic"] else { return nil }
    guard let playingSports = dict["IncludePlayingSports"] else 
        { return nil }
    guard let running = dict["IncludeRunningBuddy"] else
        { return nil }
    guard let studyPartner = dict["IncludeStudyPartner"] else 
        { return nil }
    guard let dancing = dict["IncludeDancing"] else { return nil }
    guard let concerts = dict["IncludeConcerts"] else { return nil }
    guard let theatre = dict["IncludeTheatre"] else { return nil }
    guard let games = dict["IncludeGames"] else { return nil }
    guard let maxDistanceDeclined  = 
         dict["MaxDistanceFilterDeclined"]  else { return nil }
    guard let maximumDistance = dict["MaxDistanceOfPostings"] else 
        { return nil }
    guard let maxAge = dict["MaximumMatchAge"] else
        { return nil }
    guard let minAge = dict["MinimumMatchAge"] else { return nil }
    self.uID = uID as! String
    self.declinedAgeRange = declinedAgeRange as! Bool
    self.genderPreference = genderPreference as! String
    self.includeFBFriends = includeFBFriends as! Bool
    self.sportingEvents = sportingEvents as! Bool
    self.hiking = hiking as! Bool
    self.misc = misc as! Bool
    self.museumsAndArtGalleries = museumsAndArtGalleries as! Bool
    self.music = music as! Bool
    self.playingSports = playingSports as! Bool
    self.running = running as! Bool
    self.studyPartner = studyPartner as! Bool
    self.dancing = dancing as! Bool
    self.concerts = concerts as! Bool
    self.theatre = theatre as! Bool
    self.games = games as! Bool
    self.maxDistanceDeclined = maxDistanceDeclined as! Bool
    self.maximumDistance = maximumDistance as! Int
    self.maxAge = maxAge as! Int
    self.minAge = minAge as! Int
}

}

我还尝试使用以下方式从FIR数据库中提取数据

   if let dict = snapshot.value as? Dictionary<String, AnyObject> {
   otherUserName = dict["userName"], etc.

编译器告诉我所需的值是存在的,并且可读性的毫秒可读,然后崩溃了,因为它意外地发现了" profile!userId!"的零值。或我先阅读的三个配置文件语句中的任何一个。(我编辑了此快照中所说的无关值(:

snapshot.value是{

"-Khh__kC9V9mnT1SAbGh" =     {
    URLofMediumFBProfPic = "https://scontent.xx.fbcdn.net/v/t1.0-1/p100x100/10665301_10204284275524944_7824974439027842885_n.jpg?oh=237544b1350b5759e963c2f5b8234f69&oe=594E1F1";
    name = Michael;
    uid = aRandomUserID6388;
};

}

我还试图在实现3秒的延迟后读取这些值,但这无效。

我没有看到工作代码和我的非工作代码之间的任何区别。我不知道如何解决这个问题。有人知道我的工作和非工作示例有何不同?对不起,上面有太多代码...

将配置文件对象更改为:

init?(snapshot: FIRDataSnapshot) {
    let dict = snapshot.value as! [String: Any]// else { return nil }
    let userId = dict["uid"] as! String// else { return nil }
    let firstName  = dict["name"] as! String //else { return nil }
    let age = dict["age"] as! Int //else { return nil }
    let birthday = dict["birthday"] as! String //else { return nil }
    let gender = dict["gender"] as! String //else { return nil }
    let education = dict["education"] as! String //else { return nil }
    let thisIsMyFun  = dict["bodyOfThisIsMyFun"] as! String // else { return nil }
    let iAm = dict["bodyOfIAM"] as! String //else { return nil }
    let location = dict["locationOfUser"] as! String //else { return nil }
    let userLat = dict["latitudeOfUser"] as! Double //else { return nil }
    let userLong = dict["longitudeOfUser"] as! Double //else { return nil }
    let firStorageNameForFB1 = dict["firStorageNameForFBPhoto1"]  as! String //else { return nil }
    let firStorageNameForFB2  = dict["firStorageNameForFBPhoto2"]  as! String //else { return nil }
    self.firStorageNameForFB3 = dict["firStorageNameForFBPhoto3"] as? String
    self.firStorageNameForFB4 = dict["firStorageNameForFBPhoto4"] as? String
    self.firStorageNameForFB5 = dict["firStorageNameForFBPhoto5"] as? String
    self.firStorageNameForFB6 = dict["firStorageNameForFBPhoto6"] as? String
    self.firStorageNameForFB7  = dict["firStorageNameForFBPhoto7"] as? String
    self.firStorageNameForFB8 = dict["firStorageNameForFBPhoto8"] as? String
    self.firStorageNameForFB9 = dict["firStorageNameForFBPhoto9"] as? String
    self.firStorageNameForFB10 = dict["firStorageNameForFBPhoto10"] as? String
    self.firStorageNameForPH1  = dict["firStorageNameForPHPhoto1"] as? String
    self.firStorageNameForPH2 = dict["firStorageNameForPHPhoto2"] as? String
    let fbDownloadURLForSmallProfPic = dict["URLofSmallFBProfPic"] as! String//else { return nil }
    let fbDownloadURLForMediumProfPic = dict["URLofMediumFBProfPic"] as! String//else { return nil }
    let fbDownloadURLForLargeProfPic = dict["URLofLargeFBProfPic"] as! String// else { return nil }
    let firDownloadURLForFB1  = dict["firDownloadURLStringForFBPic1"] as! String// else { return nil }
    let firDownloadURLForFB2 = dict["firDownloadURLStringForFBPic2"] as! String// else { return nil }
    self.firDownloadURLForFB3 = dict["firDownloadURLStringForFBPic3"] as? String
    self.firDownloadURLForFB4 = dict["firDownloadURLStringForFBPic4"] as? String
    self.firDownloadURLForFB5 = dict["firDownloadURLStringForFBPic5"] as? String
    self.firDownloadURLForFB6  = dict["firDownloadURLStringForFBPic6"] as? String
    self.firDownloadURLForFB7 = dict["firDownloadURLStringForFBPic7"] as? String
    self.firDownloadURLForFB8 = dict["firDownloadURLStringForFBPic8"] as? String
    self.firDownloadURLForFB9 = dict["firDownloadURLStringForFBPic9"] as? String
    self.firDownloadURLForFB10 = dict["firDownloadURLStringForFBPic10"] as? String
    self.firDownloadURLForPH1 = dict["firDownloadURLStringForPHPic1"] as? String
    self.firDownloadURLForPH2 = dict["firDownloadURLStringForPHPic2"] as? String
    self.activePostID1 = dict["FirstActivePostID"] as? String
    self.activePostID2 = dict["SecondActivePostID"] as? String
    self.activePostID3 = dict["ThirdActivePostID"] as? String
    self.activePostID4 = dict["FourthActivePostID"] as? String
    self.activePostID5  = dict["FifthActivePostID"] as? String
    self.activePostID6 = dict["SixthActivePostID"] as? String
    self.userId = userId as! String
    self.firstName = firstName as! String
    self.age = age as! Int
    self.birthday = birthday as! String
    self.gender = gender as! String
    self.education = education as! String
    self.thisIsMyFun  = thisIsMyFun as! String
    self.iAm = iAm as! String
    self.location = location as! String
    self.userLat = userLat as! Double
    self.userLong = userLong as! Double
    self.firStorageNameForFB1 = firStorageNameForFB1 as! String
    self.firStorageNameForFB2 = firStorageNameForFB2 as! String
    self.firStorageNameForFB3 = firStorageNameForFB3 as? String
    self.firStorageNameForFB4 = firStorageNameForFB4 as? String
    self.firStorageNameForFB5 = firStorageNameForFB5 as? String
    self.firStorageNameForFB6 = firStorageNameForFB6 as? String
    self.firStorageNameForFB7 = firStorageNameForFB7 as? String
    self.firStorageNameForFB8 = firStorageNameForFB8 as? String
    self.firStorageNameForFB9 = firStorageNameForFB9 as? String
    self.firStorageNameForFB10 = firStorageNameForFB10 as? String
    self.firStorageNameForPH1 = firStorageNameForPH1 as? String
    self.firStorageNameForPH2 = firStorageNameForPH2 as? String
    self.fbDownloadURLForSmallProfPic = fbDownloadURLForSmallProfPic as! String
    self.fbDownloadURLForMediumProfPic = fbDownloadURLForMediumProfPic as! String
    self.fbDownloadURLForLargeProfPic = fbDownloadURLForLargeProfPic as! String
    self.firDownloadURLForFB1 = firDownloadURLForFB1 as! String
    self.firDownloadURLForFB2 = firDownloadURLForFB2 as! String
    self.firDownloadURLForFB3 = firDownloadURLForFB3 as? String
    self.firDownloadURLForFB4 = firDownloadURLForFB4 as? String
    self.firDownloadURLForFB5 = firDownloadURLForFB5 as? String
    self.firDownloadURLForFB6 = firDownloadURLForFB6 as? String
    self.firDownloadURLForFB7 = firDownloadURLForFB7 as? String
    self.firDownloadURLForFB8 = firDownloadURLForFB8 as? String
    self.firDownloadURLForFB9 = firDownloadURLForFB9 as? String
    self.firDownloadURLForFB10 = firDownloadURLForFB10 as? String
    self.firDownloadURLForPH1 = firDownloadURLForPH1 as? String
    self.firDownloadURLForPH2 = firDownloadURLForPH2 as? String
    self.activePostID1 = activePostID1 as? String
    self.activePostID2 = activePostID2 as? String
    self.activePostID3 = activePostID3 as? String
    self.activePostID4 = activePostID4 as? String
    self.activePostID5 = activePostID5 as? String
    self.activePostID6 = activePostID6 as? String
}

这将导致原始的崩溃函数创建可选值,而当强制解开时会给我所需的值。不确定为什么将变量定义为可选值,因为它们被定义为非选项类型。无论如何,这以某种方式解决了我无法提取所需值的问题。

查看个人资料的初始化。您期望可选值(例如firStorageNameForFB3(,但您正在使用

    guard let firStorageNameForFB3 = dict["firStorageNameForFBPhoto3"] else { return nil }

因此,每次firStorageNameForFB3为nil时;个人资料也将是零。

尝试将模型中可选值的初始化更改为:

    init?(snapshot: FIRDataSnapshot) {
            // ...
            self.firStorageNameForFB3 = dict["firStorageNameForFBPhoto3"] as? String
            // ...
    }

并删除此值的guard let

最新更新