0

我有一个事件列表,其中每个事件都使用来自核心数据模型的对象将数据传递给详细 VC。例如,一个Event可以有标题、日期、房间、isFavorite 等。现在,从 中DetailVC,我有一个“添加到我的日程安排”按钮,让用户选择他们最喜欢的活动参加。单击后,我想将此对象保存到数组中。

我得到这个错误1:

无法使用 String 类型的参数列表调用 append。

详细视图控制器:

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!

这是详细视图控制器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)
        }


    }

还想不通这个。这是处理稍后保存为新数组的核心数据对象的有效方法吗?就我而言,这将在按下名为 My Schedule 的分段按钮时显示。谢谢您的回答。

事件.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()
        
    }

4

1 回答 1

0

您将 favesArray 定义为 Event 数组:

var favesArray = [Event]()

但是您正在尝试附加一个字符串

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

如果您希望能够附加 Event 和 String 您需要将您的声明更改为:

var favesArray = [AnyObject]()

或者(如果可能)将您的 faveSession 转换为 Event 而不是 String,但您可以决定是否可能

if let faveSession:Event = detailItem.title as? Event {
  self.favesArray.append(faveSession) 
}
于 2015-06-01T00:19:55.487 回答