0
votes

class NewsViewController: UITableViewController, UISearchResultsUpdating {

var rssItems : [(title: String, description : String, pubDate : String, link : String)]?

var filteredRssItems = [String]()

var resultSearchController = UISearchController()

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.separatorColor = UIColor.clearColor()
    self.view.backgroundColor = UIColor(colorLiteralRed: 1.4, green: 1.4, blue: 1.4, alpha: 1)

    self.resultSearchController = UISearchController(searchResultsController: nil)
    self.resultSearchController.searchResultsUpdater = self
    self.resultSearchController.dimsBackgroundDuringPresentation = false
    self.resultSearchController.searchBar.sizeToFit()

    self.tableView.tableHeaderView = self.resultSearchController.searchBar

    self.tableView.reloadData()

}

override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated)

    let feedParser = FeedParser()
    feedParser.parseFeed("http://rss.etnews.co.kr/Section902.xml", completionHandler: { (rssItems: [(title: String, description: String, pubDate: String, link: String)]) -> Void in

        self.rssItems = rssItems
        NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in

            self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .None)

        })
    })

    tableView.reloadData()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    guard let rssItems = rssItems else {

        return 0

    }

    if (self.resultSearchController.active) {

        return self.filteredRssItems.count

    } else {

        return rssItems.count

    }

}

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

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

    if (self.resultSearchController.active) {

        cell.titleLabel.text = filteredRssItems[indexPath.row]
        cell.descriptionLabel.text = filteredRssItems[indexPath.row]
        cell.dateLabel.text = filteredRssItems[indexPath.row]

    } else {

        cell.titleLabel.text = rssItems![indexPath.row].title
        cell.descriptionLabel.text = rssItems![indexPath.row].description
        cell.dateLabel.text = rssItems![indexPath.row].pubDate

    }

    return cell

}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let item = rssItems?[indexPath.row]

    if let url = NSURL(string: item!.link) {

        let safariController = SFSafariViewController(URL: url, entersReaderIfAvailable: true)
        presentViewController(safariController, animated: true, completion: nil)

    }

    tableView.deselectRowAtIndexPath(indexPath, animated: false)

}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    return 190.0

}

@IBAction func shartBtnTapped(sender: AnyObject) {

    let actionSheet = UIAlertController(title: "", message: "Share your Note", preferredStyle: UIAlertControllerStyle.ActionSheet)

    // Configure a new action for sharing the note in Twitter.
    let tweetAction = UIAlertAction(title: "Share on Twitter", style: UIAlertActionStyle.Default) { (action) -> Void in

    }


    // Configure a new action to share on Facebook.
    let facebookPostAction = UIAlertAction(title: "Share on Facebook", style: UIAlertActionStyle.Default) { (action) -> Void in

    }

    // Configure a new action to show the UIActivityViewController
    let moreAction = UIAlertAction(title: "More", style: UIAlertActionStyle.Default) { (action) -> Void in

        let activityViewController = UIActivityViewController(activityItems: [self.title!], applicationActivities: nil)

        activityViewController.excludedActivityTypes = [UIActivityTypeMail]

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

    }


    let dismissAction = UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel) { (action) -> Void in

    }

    actionSheet.addAction(tweetAction)
    actionSheet.addAction(facebookPostAction)
    actionSheet.addAction(moreAction)
    actionSheet.addAction(dismissAction)

    presentViewController(actionSheet, animated: true, completion: nil)
}


func showAlertMessage(message: String!) {
    let alertController = UIAlertController(title: "EasyShare", message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.Default, handler: nil))
    presentViewController(alertController, animated: true, completion: nil)
}

func updateSearchResultsForSearchController(searchController: UISearchController) {

    self.filteredRssItems.removeAll(keepCapacity: false)

    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)

    let array = (self.rssItems as NSArray).filteredArrayUsingPredicate(searchPredicate)
    self.filteredRssItems = array as! [String]

    self.tableView.reloadData()

}


@IBAction func logoutBtnTapped(sender: AnyObject) {

    NSUserDefaults.standardUserDefaults().setValue(nil, forKey: "uid")
    self.dismissViewControllerAnimated(true, completion: nil)

}

please help me~ i have a problem. let array = (self.rssItems as NSArray).filteredArrayUsingPredicate(searchPredicate) => Cannot convert value of type “[(title: String, description : String, pubDate : String, link : String)]?” to type 'NSArray' in coercion

please advice me solution~

1
Please show us what you have tried and the errors you get with your attempts. Why do you think your code should be working? What have you tried differently? Please also read this page to learn how to write a good question: stackoverflow.com/help/how-to-askMatt C

1 Answers

0
votes

NSArrays can only contain types conforming to AnyObject. A tuple is not an AnyObject, and as such cannot be stored in an NSArray.

I see, however, that you cast the result of the filter to [String]. Do you perhaps mean to extract one of the entries in the tuple before applying the filter? For example, something like:

let array = (self.rssItems.map({ $0.description }) as NSArray).filteredArrayUsingPredicate(searchPredicate)

If not, you may have to consider an alternative to filteredArrayUsingPredicate. Consider instead using a Swift Array and filter with your own filtering function.