如何快速返回多个值

Posted

技术标签:

【中文标题】如何快速返回多个值【英文标题】:How to return multiple values in swift 【发布时间】:2016-08-18 15:45:43 【问题描述】:

我是 swift 的初学者。 我创建 tableview 并从 jsonFile 获取数据以显示文本和图片。 然后我想在tableview上添加searchBar但是有问题。

import UIKit

    class EpisodesTableViewController: UITableViewController
    
        var episodes = [Episode]()

        var names = [Episode]()

        let searchController = UISearchController(searchResultsController: nil)
        var filteredNames = [Episode]()

        func filterContentForSearchText(searchText: String) 
            filteredNames = self.names.filter  name in
                return name.title!.lowercaseString.containsString(searchText.lowercaseString)
            
            tableView.reloadData()
        





        override func viewDidLoad()
        
            super.viewDidLoad()


            searchController.searchResultsUpdater = self
            searchController.dimsBackgroundDuringPresentation = false
            definesPresentationContext = true
            tableView.tableHeaderView = searchController.searchBar

            tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.size.height), animated: false)

            tableView.estimatedRowHeight = tableView.rowHeight
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.separatorStyle = .None

            self.episodes = Episode.downloadAllEpisodes()
            self.tableView.reloadData()
        

        override func preferredStatusBarStyle() -> UIStatusBarStyle 
            return .LightContent
        

        // MARK: - Table view data source

        override func numberOfSectionsInTableView(tableView: UITableView) -> Int
        
            return 1
        

        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        
            if searchController.active && searchController.searchBar.text != ""
                return filteredNames.count
            else
                return names.count
            
            return episodes.count
        

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

            let cell = tableView.dequeueReusableCellWithIdentifier("Episode Cell", forIndexPath: indexPath) as! EpisodeTableViewCell
            let episode = self.episodes[indexPath.row]

            let data: Episode

            if searchController.active && searchController.searchBar.text != "" 
                data = filteredNames[indexPath.row]
            
            else 
                data = names[indexPath.row]
            

            let titleName = data.title!

            cell.episode = episode
            cell.textLabel?.text = titleName

            return cell
        

        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
            if segue.identifier == "SendData"
                if let detailPage = segue.destinationViewController as? detailEpisodeViewController 
                    if let indexpath = tableView.indexPathForSelectedRow 
                        let episode = episodes[indexpath.row]
                        detailPage.episode = episode
                    
                
        
    




    extension EpisodesTableViewController: UISearchResultsUpdating 

        func updateSearchResultsForSearchController(searchController: UISearchController) 
            filterContentForSearchText(searchController.searchBar.text!)
        
    

这是我的代码。

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        
            if searchController.active && searchController.searchBar.text != ""
                return filteredNames.count
            else
                return names.count
            
            return episodes.count
        

当我返回过滤名称和名称界面时,只显示搜索栏。如果我返回过滤后的名称和剧集显示错误索引超出范围。 我不知道如何解决这个问题。

【问题讨论】:

return 存在方法,不能有这样的连续返回语句。当其中一个被击中时,返回值并退出方法。您可以返回元组、结构、对象或集合作为返回多条数据的一种方式,但在这种情况下,您必须返回单个 Int 以满足方法签名。 【参考方案1】:

如果你想返回两个值,只需要像这样返回一个 touple:

返回(数据类型,数据类型)

所以这可能是

func returnTouple() -> (String, AnyObject) 
    return ("Hello World", 1)

那么你可以像这样访问它:

let (myString, myObject) = returnTouple()

myString == "Hello World"

您还可以通过 .0 和 .1 访问,例如 returnTouple().0 == "Hello World"

接下来,

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    if searchController.active && searchController.searchBar.text != "" 
        return filteredNames.count
     else 
        return names.count
    
    return episodes.count

这个功能不应该工作。你有一个if else ,在这两个部分都有一个返回语句。除非你说if else if 这使得第三个返回语句不可能被击中,所以它不应该在那里。

【讨论】:

以上是关于如何快速返回多个值的主要内容,如果未能解决你的问题,请参考以下文章

快速访问多个值

如何快速从 Promise 返回值 [重复]

如何从多个单选组传递单选按钮值?如何解决返回第一个值?

如何从 UIActivityItemSource 函数返回多个值

进程退出并返回值 3221225725 - 快速排序最坏情况,malloc 崩溃,如何解决? - C

如何在不使用闭包的情况下快速执行SelectorOnMainThread 并获取返回值?