Adding rows to a swift array of my Schedule from a Detail View Controller won't work

336 Views Asked by At

I have a list of events where each of them pass in data to a detail VC with an object from a core data model. For example, an Event can have a title, date, room, isFavorite, etc. Now, from the DetailVC, I have an "Add to my Schedule" button to let a user select their favorite event to attend. Once clicked, I would like to save this object to an array.

I get the this error1:

Cannot invoke append with an argument list of type String.

Detail View Controller:

class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var managedObjectContext: NSManagedObjectContext? = nil

    //    @IBOutlet weak var detailDescriptionLabel: UILabel!

    @IBOutlet weak var tableView: UITableView!

    //create a new array to hold on favorite objects (sessions add it to My Schedule)
    var favesArray = [Event]()

    var detailItem: Event!

This is the Detail View Controller cellForRowAtIndexPath:

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

    if indexPath.section == Sections.info && indexPath.row == 1 {
        let cell: TitleCell = tableView.dequeueReusableCellWithIdentifier("faveCell", forIndexPath: indexPath) as! TitleCell

        if detailItem.isFavorite.boolValue {
            cell.valueFaveButton.setTitle("Remove from My Schedule", forState: .Normal)
        } else {
            cell.valueFaveButton.setTitle("Add to My Schedule", forState: .Normal)
        }

        cell.valueFaveButton.addTarget(self, action: "myScheduleButton:", forControlEvents: .TouchUpInside)

        return cell

    } else if indexPath.section == Sections.info && indexPath.row == 0 {

        let cell: TitleCell = tableView.dequeueReusableCellWithIdentifier("TitleCell", forIndexPath: indexPath) as! TitleCell

        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "hh:mm"
        var dateString = dateFormatter.stringFromDate(detailItem!.date)

        cell.titleLabel.text = detailItem?.title
        cell.fieldLabel.text = dateString

        return cell

    } else if indexPath.section == Sections.info && indexPath.row == 2 {
        let cell: RemoveFaveCell = tableView.dequeueReusableCellWithIdentifier("removeFaveCell", forIndexPath: indexPath) as! RemoveFaveCell

        cell.removeFaveButton.addTarget(self, action: "removeFavoriteButton:", forControlEvents: .TouchUpInside)

        return cell

    } else if indexPath.section == Sections.description {

        let cell: LabelCell = tableView.dequeueReusableCellWithIdentifier("labelCell", forIndexPath: indexPath) as! LabelCell

        cell.labelDescriptionField.text = "Swift is the new language from Apple."

        return cell
    } else {

        assertionFailure("Unhandled session table view section")
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell

        return cell
    }
}

    func myScheduleButton(sender: UIButton) {


      detailItem.isFavorite = !detailItem.isFavorite.boolValue

      tableView.reloadSections(NSIndexSet(index: Sections.info), withRowAnimation: .Automatic)

        //now add this clicked/save object(event row) over the faves array


        //1 yellow warning in next line:Conditional Cast from Event to  Event always succeeds. But it compiles fine.
        if let faveSession:Event = detailItem as? Event {

            self.favesArray.append(faveSession)
        }


    }

Can't figure this out yet. Is this a valid approach to work with core data object to be saved later as new arrays? In my case, this will be shown when a segment button called My Schedule is pressed. Thank you for your answers.

Event.swift

class Event: NSManagedObject {

    @NSManaged var date: NSDate
    @NSManaged var isFavorite: NSNumber
    @NSManaged var timeStamp: NSDate
    @NSManaged var title: String
    @NSManaged var classSelected: String
    @NSManaged var desc: String
    
}

Display My Schedule (favesArray objects selected in detailVC)

@IBAction func FilterHSClassesByPeriod(sender: UISegmentedControl) {
        let classSelected: String?
        
        if sender.selectedSegmentIndex == 0 {
            classSelected = "Period 1"
        }else {
            classSelected = "My Schedule"
        }        
      
        let filterPredicate = NSPredicate(format: "classSelected = %@", classSelected!)
        var request: NSFetchRequest = self.fetchedResultsController.fetchRequest
        
        request.predicate = filterPredicate        
      
        var e: NSError? = nil
        
        self.fetchedResultsController.performFetch(&e)        
       
        self.tableView.reloadData()
        
    }

1

There are 1 best solutions below

7
On BEST ANSWER

You define favesArray as an array of Event:

var favesArray = [Event]()

But you are trying to append a String

if let faveSession:String = detailItem.title as? String {
  self.favesArray.append(faveSession) 
}

If you want to be able to append Event and String you need to change your declaration to be:

var favesArray = [AnyObject]()

Or (if possible) cast your faveSession to Event instead String, but just you can decide if it is possible

if let faveSession:Event = detailItem.title as? Event {
  self.favesArray.append(faveSession) 
}