[英]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
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.