2015-02-09 46 views
1

這是工作昨晚,但是當我運行我的代碼,我現在接受:找不到致命錯誤:意外發現零而展開的可選值

fatal error: unexpectedly found nil while unwrapping an Optional value.

誰能幫我找到這個錯誤?

import UIKit 
class UserRegistration: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate { 

//USER REGISTRATION FORM 

//Activity Indicator 
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView() 

//Error 
func displayAlert(title:String, error:String){ 
    //create Alert 
    var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert) 
    alert.addAction(UIAlertAction(title: "Ok", style: .Cancel, handler: nil)) 
    self.presentViewController(alert, animated: true, completion: nil) 
} 

//User Profile Picture Selection 
var profileImage = UIImage() 
var isThereImage = false 
@IBOutlet var uploadProfilePictureButton: UIButton! 
@IBAction func uploadProfilePicture(sender: AnyObject) { 
    //Settings needed for image upload 
    var image = UIImagePickerController() 
    image.delegate = self 
    image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary //can use '.camera' to access camera 
    image.allowsEditing = true 
    //Select image. FYI Completion is a function that happens when viewcontroller is presented 
    self.presentViewController(image, animated: true, completion: nil) 
} 
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 
     //Store image in local variable to be resized later 
     profileImage = image 
     println("Image is selected") 
     //Manually Close View Controller 
     self.dismissViewControllerAnimated(true, completion: nil) 
     //Remove button title 
     uploadProfilePictureButton.setTitle("", forState: .Normal) 
     //Display Image 
     uploadProfilePictureButton.setBackgroundImage(image, forState: .Normal) 
     //Set isThereImage Boolean 
     isThereImage = true 
    } 

//--------------------------------------- 

//User Input Information 
@IBOutlet var userEmailAddress: UITextField! 
@IBOutlet var userPasswordOne: UITextField! 
@IBOutlet var userPasswordTwo: UITextField! 
@IBOutlet var passwordConfirmationMatch: UILabel! 
var confirmedPassword = Bool() 

//--------------------------------------- 

//Submit User Input to Database 

@IBAction func userRegistration(sender: AnyObject) { 
    var error = "" 
    //Verify if User Exist and Passwords Match 
    if userEmailAddress.text == "" || userPasswordOne.text == "" || confirmedPassword == false { 
      error = "Please enter an email address and password, or make sure your passwords match." 
      println("Registration had an error") 
    } 
    if error != "" { 
     displayAlert("Error in Registration", error: error) 
    } else { 

    //Sign Up User 
    var user = PFUser() 

    //Resize Profile Picture 
    let size = CGSizeApplyAffineTransform(profileImage.size, CGAffineTransformMakeScale(0.5, 0.5)) 
    let hasAlpha = true 
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen 
    UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale) 
    profileImage.drawInRect(CGRect(origin: CGPointZero, size: size)) 
    let scaledImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    //User Information 
    user.password = userPasswordTwo.text 
    user.email = userEmailAddress.text 
    user.username = userEmailAddress.text 
     if isThereImage == false { 
      displayAlert("Please upload a picture for your profile.", error: error) 
     }else if isThereImage == true { 
      var imageData = UIImagePNGRepresentation(scaledImage) 
      var imageFile = PFFile(name: userEmailAddress.text + ".png", data:imageData) 
      user.setObject(imageFile, forKey: "userProfileImage") 
     } 
    user.setObject("", forKey: "firstName") 
    user.setObject("", forKey: "lastName") 
    user.setObject("", forKey: "userLocation") 

    //Insert Activity Indicator here 
    activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50)) 
    activityIndicator.center = self.view.center 
    activityIndicator.hidesWhenStopped = true 
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray 
    view.addSubview(activityIndicator) 
    activityIndicator.startAnimating() 
    UIApplication.sharedApplication().beginIgnoringInteractionEvents() 
    //------------------------------- 

    user.signUpInBackgroundWithBlock { 
     (succeeded: Bool!, signupError: NSError!) -> Void in 
      //Stop activity indicator whether there is an error or not 
      self.activityIndicator.stopAnimating() 
      UIApplication.sharedApplication().endIgnoringInteractionEvents() 
      if signupError == nil { 
      // Hooray! Let them use the app now. 
      println("Registration Completed") 
      } else { 
       //Keep this here! 
       if let errorString = signupError.userInfo?["error"] as? NSString{ 
        error = errorString 
       } else { 
        error = "Please try again later." 
       } 
      self.displayAlert("Could not Sign Up", error: error) 
      println(signupError) 
      } 
     } 
    } 
    //Print Confirmation to Cortana 
} 

//--------------------------------------- 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    passwordConfirmationMatch.hidden = true 
    //UITextField Delegate 
    self.userEmailAddress.delegate = self 
    self.userPasswordOne.delegate = self 
    self.userPasswordTwo.delegate = self 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

//Password Matching Function 
func passwordCheck() { 
    if userPasswordTwo.text == userPasswordOne.text { 
     passwordConfirmationMatch.hidden = false 
     confirmedPassword = true 
     println("Password match") 
    } else { 
     passwordConfirmationMatch.hidden = true 
     confirmedPassword = false 
     println("Passwords don't match") 
    } 
} 

//Handle Keyboard 

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    self.view.endEditing(true) 
    passwordCheck() 
} 

func textFieldShouldReturn(textField: UITextField!) -> Bool { 
    userEmailAddress.resignFirstResponder() 
    userPasswordOne.resignFirstResponder() 
    userPasswordTwo.resignFirstResponder() 
    passwordCheck() 
    return true 
} 
} 
+1

錯誤發生在哪裏? – emlai 2015-02-09 13:14:18

+0

我相信它在運行時發生。我有2個獨立的視圖控制器,並希望將一個類文件連接到它們兩個。那可能嗎? – 2015-02-09 13:15:56

+0

錯誤發生在哪一行? – rakeshbs 2015-02-09 13:16:34

回答

2

您收到的錯誤表明您的代碼試圖訪問它時,其中一個變量已被聲明爲可選項。

您是否從錯誤中獲得更多信息?例如變量的名稱?如果沒有,使用一些斷點找到罪魁禍首,並確保它不是零,當它的時間來使用它。

+0

我要試着找到罪魁禍首。謝謝! – 2015-02-09 13:18:38

相關問題