简体   繁体   中英

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. Both functions have the same body, the same conditions, but first function returns Int dat type and second returns String data type.

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.

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 .

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:

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

Also, for convention your code, you can use typealias keyword to define name for your 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

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

and return like this 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")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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