Reputation: 519
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.
Upvotes: 3
Views: 3587
Reputation: 33
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")
}
Upvotes: 2
Reputation: 2650
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.
Upvotes: 2
Reputation: 6018
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)")
Upvotes: 5
Reputation: 3657
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
Upvotes: 2
Reputation: 16466
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
Upvotes: 2