如何在核心数据中保存明天日期任务以及如何在Swift中将这些任务放入表视图?

时间:2018-02-06 05:26:30

标签: ios swift core-data nspredicate

如何在核心数据中保存明天日期任务以及如何在Swift中将这些任务转换为表格视图?

当我打印(“对象在明天任务:(filterTomorrowTask.count)”)时,它显示nil数组。 我不知道如何修复这个bug。我是Swift的新手,所以不知道如何解决这个问题。

实用程序类:

class FilteredData
{
    static func filterTomorrowTask(filteredObject: String) -> [Task]
    {
        let fetchRequest: NSFetchRequest<Task> = Task.fetchRequest()

        var user: [Task]? = nil

        let predicate = NSPredicate(format: "date_from == %@", filteredObject)
        fetchRequest.predicate = predicate

        do {
            user = try context.fetch(fetchRequest)
            return user!
        }
        catch {
            return user!
        }
    }

}

ViewController.h

//CoreData Object
var filterTomorrowTask: [Task] = []

override func viewDidLoad() {
        super.viewDidLoad()

        self.tomorrowFilterTask()
        self.tblToday.reloadData()
    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        if tableView == self.tblTomorrow
        {
            print("Tomorrow Count \(filterTomorrowTask.count)")
            return filterTomorrowTask.count
        }
        else
        {
            return 0
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        if tableView == self.tblTomorrow
        {
                let cell = tableView.dequeueReusableCell(withIdentifier: "TomorrowTaskCell", for: indexPath) as! TomorrowTaskTVCell

                let filteredResult = filterTomorrowTask[indexPath.row]
                cell.lblTask.text = filteredResult.task_name
                cell.tfTime.text = filteredResult.time_from

                return cell

        }
        else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TomorrowTaskCell", for: indexPath) as! TomorrowTaskTVCell

            return cell
        }

    }

func tomorrowFilterTask()
    {
        isTomorrow = true

        //Adding 1 to Current Date
        let today = Date()
        let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: today)
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.dateFormat = "yyyy-MM-dd"
        let strTomorrow = formatter.string(from: tomorrow!)

        filterTomorrowTask = FilteredData.filterTomorrowTask(filteredObject: strTomorrow)

    }

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Func作为下面一次

class FilteredData
{
    static func filterTomorrowTask(filteredObject: String) -> [Task]
    {
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Your Entity Name")

        var user: [Task]? = nil

        let predicate = NSPredicate(format: "date_from == %@", filteredObject)
        fetchRequest.predicate = predicate

        do {
            user = try context.fetch(fetchRequest)
            return user!
        }
        catch {
            return user!
        }
    }

}

或只是简单地没有创建用户的特殊实例

class FilteredData
    {
        static func filterTomorrowTask(filteredObject: String) -> [Task]
        {
            let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Your Entity Name")

            let predicate = NSPredicate(format: "date_from == %@", filteredObject)
            fetchRequest.predicate = predicate

            ///Handler
    do
    {
        let response = try context.fetch(fetchRequest)
        return response as! [Audio]

    }
    catch let error as NSError
    {
        ///failure
        print(error)
        return [Audio]()
    }
        }

    }