致命错误:意外地发现零,而在解包
在登录应用程序中解开一个可选值(lldb)我试图使登录窗体作为主要的初始屏幕,但我不能,一直打开用户列表表。我没有关于Parse的任何数据,但我有所有需要的类。它看起来像是以匿名用户或其他方式登录的,现在它出现很多错误,因为没有适合未知用户的代码?有人可以帮我吗?致命错误:意外地发现零,而在解包
//
// FeedTableViewController.swift
// ParseStarterProject
//
// Created by Rob Percival on 19/05/2015.
// Copyright (c) 2015 Parse. All rights reserved.
//
import UIKit
import Parse
class FeedTableViewController: UITableViewController {
var messages = [String]()
var usernames = [String]()
var imageFiles = [PFFile]()
var users = [String: String]()
override func viewDidLoad() {
super.viewDidLoad()
var query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let users = objects {
self.messages.removeAll(keepCapacity: true)
self.users.removeAll(keepCapacity: true)
self.imageFiles.removeAll(keepCapacity: true)
self.usernames.removeAll(keepCapacity: true)
for object in users {
if let user = object as? PFUser {
self.users[user.objectId!] = user.username!
}
}
}
var getFollowedUsersQuery = PFQuery(className: "followers")
getFollowedUsersQuery.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
getFollowedUsersQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
var followedUser = object["following"] as! String
var query = PFQuery(className: "Post")
query.whereKey("userId", equalTo: followedUser)
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
self.messages.append(object["message"] as! String)
self.imageFiles.append(object["imageFile"] as! PFFile)
self.usernames.append(self.users[object["userId"] as! String]!)
self.tableView.reloadData()
}
}
})
}
}
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return usernames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell
imageFiles[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in
if let downloadedImage = UIImage(data: data!) {
myCell.postedImage.image = downloadedImage
}
}
myCell.username.text = usernames[indexPath.row]
myCell.message.text = messages[indexPath.row]
return myCell
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
*/
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
}
A PFUser
的对象ID未存储在设备上。你需要取它。以下是Objective-C,但它应该很容易转换为swift。
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:[[PFUser currentUser] objectForKey:@"username"]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count) {
for (PFObject *object in objects) { // just to be safe, but there should only be one.
NSLog(@"Object ID: %@", object.objectId);
}
}
}
}
在斯威夫特(因为你礼貌地问)
var query = PFUser.query()
query.whereKey("username", equalTo:PFUser.currentUser().objectForKey("username"))
query.findObjectsInBackgroundWithBlock({(objects:NSArray, error:NSError) in
if error == nil {
if objects.count {
for var object in objects {
// here you can get object.objectID
}
}
}
})
请问您可以将它转换为swift吗?确切的代码。我试图通过这个事情已经好几天了..谢谢 –
你应该在学习Swift的时候知道Objective-C。我会尽我所能,但相信自动完成,因为我在没有编译器的情况下进行输入。 – Schemetrical
我需要复制它而不是代码错误的地方? –
中的错误发生什么线? – Icaro
getFollowedUsersQuery.whereKey(“follower”,equalTo:PFUser.currentUser()!. objectId!) –