Swift取消ActivityViewController会导致致命错误

时间:2015-06-21 16:48:54

标签: ios swift optional

好的,所以我的ActivityViewController在正常共享时工作正常,但是当用户在调用ActivityViewController后点击取消时,我会收到恼人的致命错误。

fatal error: unexpectedly found nil while unwrapping an Optional value

所以看起来我没有好好打开我的选项。这是分享方法:

@IBAction func share(sender: UIBarButtonItem) {
        var   memeedimage = generateMemedImage()


        let activityViewController = UIActivityViewController(activityItems:[memeedimage] , applicationActivities: nil)
        presentViewController(activityViewController, animated: true, completion: nil)


        activityViewController.completionWithItemsHandler = {
            (activity, success, returneditems, error) in
            println("Activity: \(activity) Success: \(success) Items: \(returneditems) Error: \(error)")
            self.save()

            activityViewController.dismissViewControllerAnimated(true, completion:{
                let memevc:MemeTableViewController  = MemeTableViewController()
                self.presentViewController(memevc, animated: true, completion: nil)
                })
        }

并且share方法调用save函数,该函数使用隐式展开的可选项生成名为meme的对象,这会导致错误:

func save(){
        var meme : MemeObject!

          meme = MemeObject(textFieldtop : texfieldtop.text! ,textFieldbottom : textfieldbottom.text!  ,image : imagePickerView.image! , memedImage : generateMemedImage())
            (UIApplication.sharedApplication().delegate as! AppDelegate).memes.append(meme!)

    }

所以我决定安全地打开meme可选值但是这引发了另一个问题

 func save(){
        var meme : MemeObject?
        if let memez = meme{
          meme = MemeObject(textFieldtop : texfieldtop.text! ,textFieldbottom : textfieldbottom.text!  ,image : imagePickerView.image! , memedImage : generateMemedImage())
            (UIApplication.sharedApplication().delegate as! AppDelegate).memes.append(meme!)
        }

        else{
            println("Optionals man")
        }

    }

现在当对象不是nil时,会打印“Optionals man”,这不应该发生,并且share方法中的completionwithitemshandler属性不会显示应该在用户共享对象后直接发生的表视图控制器。

MemeTableViewController的代码:

import UIKit

class MemeTableViewController : UIViewController,UITableViewDelegate,UITableViewDataSource
{
    var memesz: [MemeObject]!


    @IBOutlet var tableView: UITableView!



    override func viewWillAppear(animated: Bool) {

            super.viewWillAppear(animated)
            let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            memesz = appDelegate.memes
            //tableView.reloadData()


    }

    override func viewDidLoad() {


        super.viewDidLoad()
        navigationController?.hidesBarsOnSwipe = true
        navigationController?.hidesBarsOnTap = true

    }

    //reserves the number of rows needed to display the image
       func tableView(tableView : UITableView, numberOfRowsInSection section : Int)->Int
       {

            return memesz.count

        }
    //Reserves the row to be dequeued for display
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell:TableViewzCell = tableView.dequeueReusableCellWithIdentifier("MemesCell", forIndexPath: indexPath) as! TableViewzCell
        let memezrow =  memesz[indexPath.row]
        cell.label1.text = memezrow.textFieldtop
        cell.label2.text = memezrow.textFieldbottom
        cell.imageview.image = memezrow.memedImage

            return cell
    }

    //Method to do something when the row is selected
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let detailController = self.storyboard!.instantiateViewControllerWithIdentifier("FullScreenMeme") as! FullScreenMeme
        detailController.meme = memesz[indexPath.row]
        self.navigationController!.pushViewController(detailController, animated: true)

    }

}

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

在查看代码之后,您似乎还没有初始化变量' meme'。

func save(){
        var meme : MemeObject? =  MemeObject(textFieldtop : texfieldtop.text! ,textFieldbottom : textfieldbottom.text!  ,image : imagePickerView.image! , memedImage : generateMemedImage())

        if let memez = meme{
            (UIApplication.sharedApplication().delegate as! AppDelegate).memes.append(meme!)
        }

        else{
            println("Optionals man")
        }

    }
}

@IBAction func share(sender: UIBarButtonItem) {
var   memeedimage = generateMemedImage()


let activityViewController = UIActivityViewController(activityItems:[memeedimage] , applicationActivities: nil)
presentViewController(activityViewController, animated: true, completion: nil)


activityViewController.completionWithItemsHandler = {
    (activity, success, returneditems, error) in
    println("Activity: \(activity) Success: \(success) Items: \(returneditems) Error: \(error)")
    self.save()

    let memevc  = self.storyboard!.instantiateViewControllerWithIdentifier("MemeTableViewConroller") as! MemeTableViewController

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

rembember to set storyboardID