fatal error: unexpectedly found nil while unwrapping an Optional value. Swift
fatal error: unexpectedly found nil while unwrapping an Optional value. Swift
我是 Swift 的新人。我的问题是我不确定如何展开可选值。当我打印 object.objectForKey("profile_picture") 时,我可以看到 Optional(<PFFile: 0x7fb3fd8344d0>)
。
let userQuery = PFUser.query()
//first_name is unique in Parse. So, I expect there is only 1 object I can find.
userQuery?.whereKey("first_name", equalTo: currentUser)
userQuery?.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
if error != nil {
}
for object in objects! {
if object.objectForKey("profile_picture") != nil {
print(object.objectForKey("profile_picture"))
self.userProfilePicture.image = UIImage(data: object.objectForKey("profile_pricture")! as! NSData)
}
}
})
您将使用 if let
来执行 "optional binding",仅当所讨论的结果不是 nil
时才执行块(并将变量 profilePicture
绑定到过程中未包装的价值)。
它会是这样的:
userQuery?.findObjectsInBackgroundWithBlock { objects, error in
guard error == nil && objects != nil else {
print(error)
return
}
for object in objects! {
if let profilePicture = object.objectForKey("profile_picture") as? PFFile {
print(profilePicture)
do {
let data = try profilePicture.getData()
self.userProfilePicture.image = UIImage(data: data)
} catch let imageDataError {
print(imageDataError)
}
}
}
}
或者,如果你想异步获取数据,或许:
userQuery?.findObjectsInBackgroundWithBlock { objects, error in
guard error == nil && objects != nil else {
print(error)
return
}
for object in objects! {
if let profilePicture = object.objectForKey("profile_picture") as? PFFile {
profilePicture.getDataInBackgroundWithBlock { data, error in
guard data != nil && error == nil else {
print(error)
return
}
self.userProfilePicture.image = UIImage(data: data!)
}
}
}
}
这将是沿着这些思路的东西,使用 if let
来解包那个可选的。然后您必须获得与 PFFile
对象关联的 NSData
(大概来自 getData
方法或 getDataInBackgroundWithBlock
)。
请参阅 Swift 编程语言中的 Optional Binding 讨论。
我是 Swift 的新人。我的问题是我不确定如何展开可选值。当我打印 object.objectForKey("profile_picture") 时,我可以看到 Optional(<PFFile: 0x7fb3fd8344d0>)
。
let userQuery = PFUser.query()
//first_name is unique in Parse. So, I expect there is only 1 object I can find.
userQuery?.whereKey("first_name", equalTo: currentUser)
userQuery?.findObjectsInBackgroundWithBlock({ (objects: [PFObject]?, error: NSError?) -> Void in
if error != nil {
}
for object in objects! {
if object.objectForKey("profile_picture") != nil {
print(object.objectForKey("profile_picture"))
self.userProfilePicture.image = UIImage(data: object.objectForKey("profile_pricture")! as! NSData)
}
}
})
您将使用 if let
来执行 "optional binding",仅当所讨论的结果不是 nil
时才执行块(并将变量 profilePicture
绑定到过程中未包装的价值)。
它会是这样的:
userQuery?.findObjectsInBackgroundWithBlock { objects, error in
guard error == nil && objects != nil else {
print(error)
return
}
for object in objects! {
if let profilePicture = object.objectForKey("profile_picture") as? PFFile {
print(profilePicture)
do {
let data = try profilePicture.getData()
self.userProfilePicture.image = UIImage(data: data)
} catch let imageDataError {
print(imageDataError)
}
}
}
}
或者,如果你想异步获取数据,或许:
userQuery?.findObjectsInBackgroundWithBlock { objects, error in
guard error == nil && objects != nil else {
print(error)
return
}
for object in objects! {
if let profilePicture = object.objectForKey("profile_picture") as? PFFile {
profilePicture.getDataInBackgroundWithBlock { data, error in
guard data != nil && error == nil else {
print(error)
return
}
self.userProfilePicture.image = UIImage(data: data!)
}
}
}
}
这将是沿着这些思路的东西,使用 if let
来解包那个可选的。然后您必须获得与 PFFile
对象关联的 NSData
(大概来自 getData
方法或 getDataInBackgroundWithBlock
)。
请参阅 Swift 编程语言中的 Optional Binding 讨论。