简体   繁体   English

可以在Swift函数中返回不同的数据类型吗?

[英]Can function in Swift return different data types?

I'm working with UITableView in Swift and I have a function that returns number of rows based on some conditions and another function that returns title for every section. 我正在使用Swift UITableView ,我有一个函数,它根据某些条件返回行数,另一个函数返回每个部分的标题。 Both functions have the same body, the same conditions, but first function returns Int dat type and second returns String data type. 两个函数具有相同的主体,相同的条件,但第一个函数返回Int dat类型,第二个函数返回String数据类型。

Can I somehow make this one generic function, to be one that returns some generic value but that value must be 'Int' for first function and String for second function. 我可以以某种方式使这个泛型函数成为一个返回一些通用值的函数,但该值必须是第一个函数的“Int”和第二个函数的String

The code below is the function that returns number of rows. 下面的代码是返回行数的函数。 Same body goes for function that returns title for section. 相同的主体去返回部分标题的功能。 And its return type is String . 它的返回类型是String

func getNumberOfRows(for section: Int) -> Int {

    let parameters = SuggestionsTableSectionType.Parameters(recents: recents, suggestions: suggestions, section: section)

    if SuggestionsTableSectionType.recentsAndSectionIsZero(parameters).isActive {
        return recents.count
    } else if SuggestionsTableSectionType.suggestionsAndSectionIsZero(parameters).isActive {
        return suggestions.count
    } else if SuggestionsTableSectionType.recentsAndSuggestionsAndSectionIsZero(parameters).isActive {
        return recents.count
    } else if SuggestionsTableSectionType.recentsAndSuggestionsAndSectionIsOne(parameters).isActive {
        return suggestions.count
    }
    return 0
}

Thanks for your answers. 谢谢你的回答。

You should return the tuple type like this: 你应该返回这样的tuple类型:

func getNumberOfRows(for section: Int) -> (Int, String) {}

Also, for convention your code, you can use typealias keyword to define name for your tuple : 此外,对于您的代码的惯例,您可以使用typealias关键字来定义tuple名称:

typealias NumberOfRowsInfo = (row: Int, someString: String)

func getNumberOfRows(for section: Int) -> NumberOfRowsInfo {}

And get data like this: 并获得这样的数据:

let info = getNumberOfRows(for: section)
print("Row: \(info.row), string: \(info.someString)")

You can use same function with two different return types like this 您可以使用具有两种不同返回类型的相同函数,如下所示

func getNumberOfRows(for section: Int) -> (Int,String)

Then it's invoked as 然后它被调用为

let (yourIntegerValue,yourStringValue) = getNumberOfRows(for section:yourSectionNumber)

Now use these two values to perform your task. 现在使用这两个值来执行任务。

You can return tuple 你可以返回tuple

func getNumberOfRows(for section: Int) -> (row:Int,title:String)

and return like this return(row:suggestions.count,title:"Suggestions") return如此return(row:suggestions.count,title:"Suggestions")

and you can use 你可以使用

let tupple =  getNumberOfRows(for :0) 
tupple.row or  tupple.title

I will suggest you for tuple . 我会建议你做元组

You can do with tuple something like this: 你可以用这样的元组来做:

func getNumberOfRows(for section: Int) -> (Int,String) {
    return (5,"5")
}

getNumberOfRows(for: 3).0
getNumberOfRows(for: 3).1

You can try following way: 您可以尝试以下方式:

func getNumberOfRows(for section: Int) -> (Int, String) {

    let parameters = SuggestionsTableSectionType.Parameters(recents: recents, suggestions: suggestions, section: section)

    if SuggestionsTableSectionType.recentsAndSectionIsZero(parameters).isActive {
        return (recents.count, "Some String")
    } else if SuggestionsTableSectionType.suggestionsAndSectionIsZero(parameters).isActive {
        return (suggestions.count, "Some String")
    } else if SuggestionsTableSectionType.recentsAndSuggestionsAndSectionIsZero(parameters).isActive {
        return (recents.count, "Some String")
    } else if SuggestionsTableSectionType.recentsAndSuggestionsAndSectionIsOne(parameters).isActive {
        return (suggestions.count, "Some String")
    }
    return (0, "Some String")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM