检查 NSIndexPath 的行和部分的开关 [英] switch that checks NSIndexPath's row and section
问题描述
我想设置一个 switch 语句来检查 NSIndexPath
的值.NSIndexPath
是一个类,它由(除其他外)部分和行(indexPath.row, indexPath.section
)
I would like to set a switch statement that checks for a value if NSIndexPath
. NSIndexPath
is a class and it consists(among other things) of section and row (indexPath.row, indexPath.section
)
这就是我如何制定一个 if 语句来同时检查一行和一个部分:
this is how I would formulate an if statement to check for a row and a section at the same time:
if indexPath.section==0 && indexPath.row == 0{
//do work
}
什么是快速切换翻译?
推荐答案
一种方式(这是可行的,因为 NSIndexPaths 本身是等价的):
One way (this works because NSIndexPaths themselves are equatable):
switch indexPath {
case NSIndexPath(forRow: 0, inSection: 0) : // do something
// other possible cases
default : break
}
或者您可以使用元组模式对整数进行测试:
Or you could just test against integers, using a tuple pattern:
switch (indexPath.section, indexPath.row) {
case (0,0): // do something
// other cases
default : break
}
另一个技巧是使用 switch true
和您已经使用的相同条件:
Another trick is to use switch true
and the same condition you're already using:
switch true {
case indexPath.row == 0 && indexPath.section == 0 : // do something
// other cases
default : break
}
就我个人而言,我会使用 nested switch
语句来测试外部的 indexPath.section
和 indexPath.row
在里面.
Personally, I would use nested switch
statements where we test against indexPath.section
on the outside and indexPath.row
on the inside.
switch indexPath.section {
case 0:
switch indexPath.row {
case 0:
// do something
// other rows
default:break
}
// other sections (and _their_ rows)
default : break
}
这篇关于检查 NSIndexPath 的行和部分的开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!