damianesteban
damianesteban

Reputation: 1611

iOS 8 Swift - Implementing a Share Sheet with Core Data

I'm trying to implement a share sheet in my iOS application and I'm getting an error.

Here is my code:

class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet var backpackerSpotImageView:UIImageView!
    @IBOutlet var tableView:UITableView!


    var backpackerSpot:BackpackerSpot?


    override func viewDidLoad() {
        super.viewDidLoad()

        // customizing background of tableview
        self.tableView.backgroundColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.2)

        // remove extra separators
        self.tableView.tableFooterView = UIView(frame: CGRectZero)

        // change the color of the separator
        self.tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.8)

        // self-sizing cells
        tableView.estimatedRowHeight = 36.0
        tableView.rowHeight = UITableViewAutomaticDimension

        // Do any additional setup after loading the view.
        if let spotImage = backpackerSpot?.spotImage
        {
        self.backpackerSpotImageView.image = UIImage(data:spotImage)
        }
    }

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as DetailTableViewCell

        // make cell transparent so background color can be seen
        cell.backgroundColor = UIColor.clearColor()

        cell.mapButton.hidden = true

        switch indexPath.row {
        case 0:
            cell.fieldLabel.text = "Name"
            cell.valueLabel.text = backpackerSpot?.spotName
        case 1:
            cell.fieldLabel.text = "Location"
            cell.valueLabel.text = backpackerSpot?.spotLocation
            cell.mapButton.hidden = false
        case 2:
            cell.fieldLabel.text = "Notes"
            cell.valueLabel.text = backpackerSpot?.spotNote
        default:
            cell.fieldLabel.text = ""
            cell.valueLabel.text = ""

        }

        return cell
    }

    @IBAction func shareSheet(sender:AnyObject) {
        let firstActivityItem = backpackerSpot?.spotName
        let secondActivityItem = backpackerSpot?.spotLocation

        let activityViewController = UIActivityViewController(
            activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)

        activityViewController.excludedActivityTypes = [
            UIActivityTypePostToVimeo,
            UIActivityTypePostToTencentWeibo,
            UIActivityTypePostToFlickr,
            UIActivityTypePostToWeibo,
            UIActivityTypeSaveToCameraRoll,
            UIActivityTypePrint,
            UIActivityTypeAssignToContact,
            UIActivityTypeAddToReadingList
        ]

        self.presentViewController(activityViewController, animated: true, completion: nil)

    }

On the line where I declare activityViewController I get the following error:

Cannot invoke 'init' with an argument list of type '(activityItems: $T2, applicationActivities: NilLiteralConvertible)'

I'm relatively new to developing iOS applications and one of the biggest issues I'm having is deciphering the error messages. Much appreciated if someone could point me in the right direction. Thank you.

Upvotes: 2

Views: 478

Answers (2)

sheisd
sheisd

Reputation: 179

Try changing these lines:

let firstActivityItem = backpacker!.spotName

let secondActivityItem  = backpacker!.spotLocation

The two must not be nil.

Upvotes: 2

damianesteban
damianesteban

Reputation: 1611

Well I figured out what was wrong.

To begin with, I did not have activityViewController properly defined. Secondly, I had to unwrap backpackerSpot. I also changed the sender on the function to UIBarButtonItem.

Here is the code:

@IBAction func shareSheet(sender:UIBarButtonItem) {
    let firstActivityItem = backpackerSpot!.spotName
    let secondActivityItem = backpackerSpot!.spotLocation

    let activityViewController : UIActivityViewController = UIActivityViewController(
        activityItems: [firstActivityItem, secondActivityItem], applicationActivities: nil)

    activityViewController.excludedActivityTypes = [
        UIActivityTypePostToVimeo,
        UIActivityTypePostToTencentWeibo,
        UIActivityTypePostToFlickr,
        UIActivityTypePostToWeibo,
        UIActivityTypeSaveToCameraRoll,
        UIActivityTypePrint,
        UIActivityTypeAssignToContact,
        UIActivityTypeAddToReadingList
    ]

    self.presentViewController(activityViewController, animated: true, completion: nil)

}

Upvotes: 2

Related Questions