在使用 swift 协议来简化 UIPageViewController 时遇到问题:
我有这个协议
protocol Pagable {
var pageIndex: Int? { get set }
}
我让 UIPageViewController 呈现的所有 UIViewController 都符合这一点。
然后在我的 UIPageViewController 中,我这样做:
var vc = StoryboardScene.Challenges.acceptedViewController() as! Pagable
vc.pageIndex = index
return vc as? UIViewController
哪个有效,但我真正想做的是:
var vc = StoryboardScene.Challenges.acceptedViewController()
(vc as? Pagable)?.pageIndex = index
return vc
并且出于某种原因,每当我这样做时(对我来说,这与片段 1 完全相同),我会收到一条错误(vc as? Pagable)?.pageIndex = index
消息,即“无法分配给不可变类型的表达式Int?
”。
我彻底糊涂了。希望深入了解为什么类型系统对我这样做。