调用时可能不存在 Firebase 元素,iOS、Swift
Firebase Element May not exist on a call, iOS, Swift
我正在使用 firebase 数据库和存储来保存图像及其描述。目前,我将其编码为具有 8 个图像和 8 个描述。我正在尝试弄清楚如何获得它,以便如果用户仅 selects4 图片并输入 4 描述,它允许将其回调。它适用于 8,但当我仅添加 4 和 select 时, table 被称为它崩溃并出现错误,即第 5 个照片变量不包含任何内容。
下面是我用来为 table 调用数据的代码以及崩溃的地方。
func configureCell(post: Post) {
self.post = post
self.carImages = []
if post.imageUrl1 != "" {
self.carImages.append(post.imageUrl1)
}
if post.imageUrl2 != ""{
self.carImages.append(post.imageUrl2)
}
if post.imageUrl3 != "" {
self.carImages.append(post.imageUrl3)
}
if post.imageUrl4 != "" {
self.carImages.append(post.imageUrl4)
}
if post.imageUrl5 != "" {
print ("Tony: \(post.imageUrl5)")
self.carImages.append(post.imageUrl5)
}
if post.imageUrl6 != "" {
self.carImages.append(post.imageUrl6)
}
if post.imageUrl7 != "" {
self.carImages.append(post.imageUrl7)
}
if post.imageUrl8 != "" {
self.carImages.append(post.imageUrl8)
}
并且崩溃发生在 imageUrl5
import Foundation
import Firebase
class Post {
private var _imageUrl1: String!
private var _imageUrl2: String!
private var _imageUrl3: String!
private var _imageUrl4: String!
private var _imageUrl5: String!
private var _imageUrl6: String!
private var _imageUrl7: String!
private var _imageUrl8: String!
private var _postKey: String!
private var _postRef: DatabaseReference!
private var _photoInfo1: String!
private var _photoInfo2: String!
private var _photoInfo3: String!
private var _photoInfo4: String!
private var _photoInfo5: String!
private var _photoInfo6: String!
private var _photoInfo7: String!
private var _photoInfo8: String!
var profileImageUrl: String {
return _profileImageUrl
}
var imageUrl1: String {
return _imageUrl1
}
var imageUrl2: String {
return _imageUrl2
}
var imageUrl3: String {
return _imageUrl3
}
var imageUrl4: String {
return _imageUrl4
}
var imageUrl5: String {
return _imageUrl5
}
var imageUrl6: String {
return _imageUrl6
}
var imageUrl7: String {
return _imageUrl7
}
var imageUrl8: String {
return _imageUrl8
}
var postKey: String {
return _postKey
}
var photoInfo1: String {
return _photoInfo1
}
var photoInfo2: String {
return _photoInfo2
}
var photoInfo3: String {
return _photoInfo3
}
var photoInfo4: String {
return _photoInfo4
}
var photoInfo5: String {
return _photoInfo5
}
var photoInfo6: String {
return _photoInfo6
}
var photoInfo7: String {
return _photoInfo7
}
var photoInfo8: String {
return _photoInfo8
}
init(imageUrl1: String, imageUrl2: String, imageUrl3: String, imageUrl4: String, imageUrl5: String, imageUrl6: String, imageUrl7: String, imageUrl8: String, photoInfo1: String, photoInfo2: String, photoInfo3: String, photoInfo4: String, photoInfo5: String, photoInfo6: String, photoInfo7: String, photoInfo8: String) {
self._imageUrl1 = imageUrl1
self._imageUrl2 = imageUrl2
self._imageUrl3 = imageUrl3
self._imageUrl4 = imageUrl4
self._imageUrl5 = imageUrl5
self._imageUrl6 = imageUrl6
self._imageUrl7 = imageUrl7
self._imageUrl8 = imageUrl8
self._photoInfo1 = photoInfo1
self._photoInfo2 = photoInfo2
self._photoInfo3 = photoInfo3
self._photoInfo4 = photoInfo4
self._photoInfo5 = photoInfo5
self._photoInfo6 = photoInfo6
self._photoInfo7 = photoInfo7
self._photoInfo8 = photoInfo8
}
init(postKey: String, postData: Dictionary<String, AnyObject>) {
self._postKey = postKey
if let imageUrl1 = postData["imageUrl1"] as? String {
self._imageUrl1 = imageUrl1
}
if let imageUrl2 = postData["imageUrl2"] as? String {
self._imageUrl2 = imageUrl2
}
if let imageUrl3 = postData["imageUrl3"] as? String {
self._imageUrl3 = imageUrl3
}
if let imageUrl4 = postData["imageUrl4"] as? String {
self._imageUrl4 = imageUrl4
}
if let imageUrl5 = postData["imageUrl5"] as? String {
self._imageUrl5 = imageUrl5
}
if let imageUrl6 = postData["imageUrl6"] as? String {
self._imageUrl6 = imageUrl6
}
if let imageUrl7 = postData["imageUrl7"] as? String {
self._imageUrl7 = imageUrl7
}
if let imageUrl8 = postData["imageUrl8"] as? String {
self._imageUrl8 = imageUrl8
}
if let photoInfo1 = postData["photoInfo1"] as? String {
self._photoInfo1 = photoInfo1
}
if let photoInfo2 = postData["photoInfo2"] as? String {
self._photoInfo2 = photoInfo2
}
if let photoInfo3 = postData["photoInfo3"] as? String {
self._photoInfo3 = photoInfo3
}
if let photoInfo4 = postData["photoInfo4"] as? String {
self._photoInfo4 = photoInfo4
}
if let photoInfo5 = postData["photoInfo5"] as? String {
self._photoInfo5 = photoInfo5
}
if let photoInfo6 = postData["photoInfo6"] as? String {
self._photoInfo6 = photoInfo6
}
if let photoInfo7 = postData["photoInfo7"] as? String {
self._photoInfo7 = photoInfo7
}
if let photoInfo8 = postData["photoInfo8"] as? String {
self._photoInfo8 = photoInfo8
}
_postRef = DataService.ds.REF_POSTS.child(_postKey)
}
}
也许可以通过示例回答
我们有一个 post class 和一个可选的私有变量——这意味着它可以包含一个值或者可以是 nil。这用 String?
表示
当外部 imageUrl1 被访问时,它可能 return 一个值或者可能是 nil,又是?表示。
class Post{
private var _imageUrl1: String?
var imageUrl1: String? {
return _imageUrl1
}
init(url: String?) { //this could be nil!
self._imageUrl1 = url
}
}
let aPost = Post(url: "testUrl")
if aPost.imageUrl1 != nil {
print(aPost.imageUrl1!)
}
//or the preferred way which unwraps the optional automatically
if let aPostUrl = aPost.imageUrl1 {
print(aPostUrl)
} else {
print("it was nil")
}
//here's a nil example
let bPost = Post(url: nil)
if let bPostUrl = bPost.imageUrl1 {
print(bPostUrl)
} else {
print("it was nil") //prints it was nil
}
我正在使用 firebase 数据库和存储来保存图像及其描述。目前,我将其编码为具有 8 个图像和 8 个描述。我正在尝试弄清楚如何获得它,以便如果用户仅 selects4 图片并输入 4 描述,它允许将其回调。它适用于 8,但当我仅添加 4 和 select 时, table 被称为它崩溃并出现错误,即第 5 个照片变量不包含任何内容。
下面是我用来为 table 调用数据的代码以及崩溃的地方。
func configureCell(post: Post) {
self.post = post
self.carImages = []
if post.imageUrl1 != "" {
self.carImages.append(post.imageUrl1)
}
if post.imageUrl2 != ""{
self.carImages.append(post.imageUrl2)
}
if post.imageUrl3 != "" {
self.carImages.append(post.imageUrl3)
}
if post.imageUrl4 != "" {
self.carImages.append(post.imageUrl4)
}
if post.imageUrl5 != "" {
print ("Tony: \(post.imageUrl5)")
self.carImages.append(post.imageUrl5)
}
if post.imageUrl6 != "" {
self.carImages.append(post.imageUrl6)
}
if post.imageUrl7 != "" {
self.carImages.append(post.imageUrl7)
}
if post.imageUrl8 != "" {
self.carImages.append(post.imageUrl8)
}
并且崩溃发生在 imageUrl5
import Foundation
import Firebase
class Post {
private var _imageUrl1: String!
private var _imageUrl2: String!
private var _imageUrl3: String!
private var _imageUrl4: String!
private var _imageUrl5: String!
private var _imageUrl6: String!
private var _imageUrl7: String!
private var _imageUrl8: String!
private var _postKey: String!
private var _postRef: DatabaseReference!
private var _photoInfo1: String!
private var _photoInfo2: String!
private var _photoInfo3: String!
private var _photoInfo4: String!
private var _photoInfo5: String!
private var _photoInfo6: String!
private var _photoInfo7: String!
private var _photoInfo8: String!
var profileImageUrl: String {
return _profileImageUrl
}
var imageUrl1: String {
return _imageUrl1
}
var imageUrl2: String {
return _imageUrl2
}
var imageUrl3: String {
return _imageUrl3
}
var imageUrl4: String {
return _imageUrl4
}
var imageUrl5: String {
return _imageUrl5
}
var imageUrl6: String {
return _imageUrl6
}
var imageUrl7: String {
return _imageUrl7
}
var imageUrl8: String {
return _imageUrl8
}
var postKey: String {
return _postKey
}
var photoInfo1: String {
return _photoInfo1
}
var photoInfo2: String {
return _photoInfo2
}
var photoInfo3: String {
return _photoInfo3
}
var photoInfo4: String {
return _photoInfo4
}
var photoInfo5: String {
return _photoInfo5
}
var photoInfo6: String {
return _photoInfo6
}
var photoInfo7: String {
return _photoInfo7
}
var photoInfo8: String {
return _photoInfo8
}
init(imageUrl1: String, imageUrl2: String, imageUrl3: String, imageUrl4: String, imageUrl5: String, imageUrl6: String, imageUrl7: String, imageUrl8: String, photoInfo1: String, photoInfo2: String, photoInfo3: String, photoInfo4: String, photoInfo5: String, photoInfo6: String, photoInfo7: String, photoInfo8: String) {
self._imageUrl1 = imageUrl1
self._imageUrl2 = imageUrl2
self._imageUrl3 = imageUrl3
self._imageUrl4 = imageUrl4
self._imageUrl5 = imageUrl5
self._imageUrl6 = imageUrl6
self._imageUrl7 = imageUrl7
self._imageUrl8 = imageUrl8
self._photoInfo1 = photoInfo1
self._photoInfo2 = photoInfo2
self._photoInfo3 = photoInfo3
self._photoInfo4 = photoInfo4
self._photoInfo5 = photoInfo5
self._photoInfo6 = photoInfo6
self._photoInfo7 = photoInfo7
self._photoInfo8 = photoInfo8
}
init(postKey: String, postData: Dictionary<String, AnyObject>) {
self._postKey = postKey
if let imageUrl1 = postData["imageUrl1"] as? String {
self._imageUrl1 = imageUrl1
}
if let imageUrl2 = postData["imageUrl2"] as? String {
self._imageUrl2 = imageUrl2
}
if let imageUrl3 = postData["imageUrl3"] as? String {
self._imageUrl3 = imageUrl3
}
if let imageUrl4 = postData["imageUrl4"] as? String {
self._imageUrl4 = imageUrl4
}
if let imageUrl5 = postData["imageUrl5"] as? String {
self._imageUrl5 = imageUrl5
}
if let imageUrl6 = postData["imageUrl6"] as? String {
self._imageUrl6 = imageUrl6
}
if let imageUrl7 = postData["imageUrl7"] as? String {
self._imageUrl7 = imageUrl7
}
if let imageUrl8 = postData["imageUrl8"] as? String {
self._imageUrl8 = imageUrl8
}
if let photoInfo1 = postData["photoInfo1"] as? String {
self._photoInfo1 = photoInfo1
}
if let photoInfo2 = postData["photoInfo2"] as? String {
self._photoInfo2 = photoInfo2
}
if let photoInfo3 = postData["photoInfo3"] as? String {
self._photoInfo3 = photoInfo3
}
if let photoInfo4 = postData["photoInfo4"] as? String {
self._photoInfo4 = photoInfo4
}
if let photoInfo5 = postData["photoInfo5"] as? String {
self._photoInfo5 = photoInfo5
}
if let photoInfo6 = postData["photoInfo6"] as? String {
self._photoInfo6 = photoInfo6
}
if let photoInfo7 = postData["photoInfo7"] as? String {
self._photoInfo7 = photoInfo7
}
if let photoInfo8 = postData["photoInfo8"] as? String {
self._photoInfo8 = photoInfo8
}
_postRef = DataService.ds.REF_POSTS.child(_postKey)
}
}
也许可以通过示例回答
我们有一个 post class 和一个可选的私有变量——这意味着它可以包含一个值或者可以是 nil。这用 String?
表示当外部 imageUrl1 被访问时,它可能 return 一个值或者可能是 nil,又是?表示。
class Post{
private var _imageUrl1: String?
var imageUrl1: String? {
return _imageUrl1
}
init(url: String?) { //this could be nil!
self._imageUrl1 = url
}
}
let aPost = Post(url: "testUrl")
if aPost.imageUrl1 != nil {
print(aPost.imageUrl1!)
}
//or the preferred way which unwraps the optional automatically
if let aPostUrl = aPost.imageUrl1 {
print(aPostUrl)
} else {
print("it was nil")
}
//here's a nil example
let bPost = Post(url: nil)
if let bPostUrl = bPost.imageUrl1 {
print(bPostUrl)
} else {
print("it was nil") //prints it was nil
}