我如何找到&修复Xcode中的错误

时间:2014-10-05 15:33:56

标签: ios xcode swift parse-platform

所以我收到错误:

  

线程1:EXC_BREAKPOINT(代码= EXC_1386_BPT,子代码= 0x0)。

我发现错误出现在viewDidLoad()

上的super.viewDidLoad()方法中
import UIKit

class TimelineTableViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var timelineData:NSMutableArray = NSMutableArray()

override init(style: UITableViewStyle) {
    super.init(style: style)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

@IBAction func loadData(){
    timelineData.removeAllObjects()

    var findTimeLineData:PFQuery = PFQuery(className: "Jabbs")

    findTimeLineData.findObjectsInBackgroundWithBlock{
        (objects:[AnyObject]!, error:NSError!)->Void in

        if !(error != nil){
            for object:PFObject! in objects as [PFObject]{
                self.timelineData.addObject(object)
            }

            let array:NSArray = self.timelineData.reverseObjectEnumerator().allObjects
            self.timelineData = array as NSMutableArray

            self.tableView.reloadData()

        }

    }
}

override func viewDidAppear(animated: Bool) {
    self.loadData()

    var footerView:UIView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
    self.tableView.tableFooterView = footerView

    var logoutButton:UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
    logoutButton.frame = CGRectMake(20, 10, 50, 20)
    logoutButton.setTitle("Logout", forState: UIControlState.Normal)
    logoutButton.addTarget(self, action: "logout", forControlEvents: UIControlEvents.TouchUpInside)

    footerView.addSubview(logoutButton)


    if ((PFUser.currentUser()) == nil){
        self.showLoginSignUp()
    }


}


func showLoginSignUp(){
    var loginAlert:UIAlertController = UIAlertController(title: "Sign Up / Login", message: "Please sign up or login",
        preferredStyle: UIAlertControllerStyle.Alert)

    loginAlert.addTextFieldWithConfigurationHandler({
        textfield in
        textfield.placeholder = "Your Username"
    })

    loginAlert.addTextFieldWithConfigurationHandler({
        textfield in
        textfield.placeholder = "Your Password"
        textfield.secureTextEntry = true
    })

    loginAlert.addAction(UIAlertAction(title: "Login", style: UIAlertActionStyle.Default, handler: {
        alertAction in
        let textFields:NSArray = loginAlert.textFields! as NSArray
        let usernameTextfield:UITextField = textFields.objectAtIndex(0) as UITextField
        let passwordTextfield:UITextField = textFields.objectAtIndex(1) as UITextField

        PFUser.logInWithUsernameInBackground(usernameTextfield.text, password: passwordTextfield.text){
            (user:PFUser!, error:NSError!)->Void in
            if ((user) != nil){
                println("Login Successful")
            }else{
                println("Login Failed, Username/Password are Incorrect")
            }

        }



    }))


    loginAlert.addAction(UIAlertAction(title: "Sign Up", style: UIAlertActionStyle.Default, handler: {
        alertAction in
        let textFields:NSArray = loginAlert.textFields! as NSArray
        let usernameTextfield:UITextField = textFields.objectAtIndex(0) as UITextField
        let passwordTextfield:UITextField = textFields.objectAtIndex(1) as UITextField

        var jabber:PFUser = PFUser()
        jabber.username = usernameTextfield.text
        jabber.password = passwordTextfield.text

        jabber.signUpInBackgroundWithBlock{
            (success:Bool!, error:NSError!)->Void in
            if !(error != nil){
                println("Sign Up Successfull")
                var imagePicker:UIImagePickerController = UIImagePickerController()
                imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
                imagePicker.delegate = self

                self.presentViewController(imagePicker, animated: true, completion: nil)
            }else{
                let errorString = error.userInfo!["error"] as NSString
                println(errorString)
            }

        }




    }))

    self.presentViewController(loginAlert, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!) {
    let pickedImage:UIImage = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage

    // Scale down image
    let scaledImage = self.scaleImageWith(pickedImage, and: CGSizeMake(95, 95))

    let imageData = UIImagePNGRepresentation(scaledImage)

    let imageFile:PFFile = PFFile(data: imageData)

    PFUser.currentUser().setObject(imageFile, forKey: "profileImage")
    PFUser.currentUser().saveInBackground()

    picker.dismissViewControllerAnimated(true, completion:nil)

}

func scaleImageWith(image:UIImage, and newSize:CGSize)->UIImage{
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
    var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}



override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

func logout(sender:UIButton){
    PFUser.logOut()
    self.showLoginSignUp()
}




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 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return timelineData.count
}


override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {
    let cell:JabbTableViewCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as JabbTableViewCell

    let jabb:PFObject = self.timelineData.objectAtIndex(indexPath!.row) as PFObject

    cell.jabbTextView.alpha = 0
    cell.timestampLabel.alpha = 0
    cell.usernameLabel.alpha = 0

    cell.jabbTextView.text = jabb.objectForKey("content") as String


    var dataFormatter:NSDateFormatter = NSDateFormatter()
    dataFormatter.dateFormat = "yyyy-MM-dd"
    cell.timestampLabel.text = dataFormatter.stringFromDate(jabb.createdAt)

    var findJabber:PFQuery = PFUser.query()
    findJabber.whereKey("objectId", equalTo: jabb.objectForKey("jabber").objectId)

    findJabber.findObjectsInBackgroundWithBlock{
        (objects:[AnyObject]!, error:NSError!)->Void in
        if !(error != nil){
            let user:PFUser! = (objects as NSArray).lastObject as PFUser
            cell.usernameLabel.text = user.username

            // Profile Image
            cell.profileImageView.alpha = 0

            let profileImage:PFFile = user["profileImage"] as PFFile

            profileImage.getDataInBackgroundWithBlock{
                (imageData:NSData!, error:NSError!)->Void in

                if !(error != nil){
                    let image:UIImage = UIImage(data: imageData)
                    cell.profileImageView.image = image
                }
            }

            UIView.animateWithDuration(0.5, animations: {
                cell.jabbTextView.alpha = 1
                cell.timestampLabel.alpha = 1
                cell.usernameLabel.alpha = 1
                cell.profileImageView.alpha = 1
            })
        }
    }

    return cell
}

0 个答案:

没有答案