我有一个UIPageViewController
,它具有一个可选的UIViewController
数组。
pageViewController.viewControllers
我注意到,每当我需要对其执行一些操作时,都需要编写if
检查的许多级别(4)。 // TODO: How can we improve this code?
//
// Logic: We will call setViewControllers, if viewControllers is null, or empty, or 1st element of
// viewControllers.pageIndex is not equal to self.selectedTabIndex
if let viewControllers = self.pageViewController.viewControllers {
if viewControllers.count > 0 {
if let pageIndexable = viewControllers[0] as? PageIndexable {
if pageIndexable.pageIndex != self.selectedTabIndex {
self.pageViewController.setViewControllers([viewController(At: self.selectedTabIndex)!], direction: direction, animated: true, completion: nil)
self.tabCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
}
} else {
self.pageViewController.setViewControllers([viewController(At: self.selectedTabIndex)!], direction: direction, animated: true, completion: nil)
self.tabCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
} else {
self.pageViewController.setViewControllers([viewController(At: self.selectedTabIndex)!], direction: direction, animated: true, completion: nil)
self.tabCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
我觉得由于if
语句级别过多,我可能以错误的方式使用了可选。您能为我提出一种改进代码的方法吗?
最佳答案
考虑一下您何时不想调用setViewControllers
。那是当以下是假的:viewControllers
为nil,或者为空,或者viewControllers[0].pageIndex != self.selectedTabIndex
使用德摩根定律,我们可以看到它的取反是:viewControllers
不为nil,也不为空,并且viewControllers[0].pageIndex == self.selectedTabIndex
在if语句中进行连接,我们得到:
if let vcs = self.pageViewController.viewControllers,
let firstVC = vcs.first as? PageIndexable, // this checks for non-empty
firstVC.pageIndex == self.selectedTabIndex {
// do stuff
} else {
self.pageViewController.setViewControllers([viewController(At: self.selectedTabIndex)!], direction: direction, animated: true, completion: nil)
self.tabCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
// do other stuff
如果该方法将在执行setViewControllers
和scrollToItem
之后立即返回(即,您在// do other stuff
位置没有任何代码),则可以改用guard
语句:guard let vcs = self.pageViewController.viewControllers,
let firstVC = vcs.first as? PageIndexable,
firstVC.pageIndex == self.selectedTabIndex else {
self.pageViewController.setViewControllers([viewController(At: self.selectedTabIndex)!], direction: direction, animated: true, completion: nil)
self.tabCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
return
}
// do stuff
正如亚历山大在comments中所说的,您也可以在一个表达式中执行此操作:if !((self.pageViewController.viewControllers?.first as? PageIndexable)?.pageIndex == self.selectedTabIndex) {
self.pageViewController.setViewControllers([viewController(At: self.selectedTabIndex)!], direction: direction, animated: true, completion: nil)
self.tabCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
但是我认为这种方式的意图不太明确。关于ios - 如果对可选的UIViewController数组进行操作,如何避免嵌套过多?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63785806/