2015-07-26 22 views
6

Tôi đã tạo một ứng dụng trong XCode 6. Hôm nay tôi đã tải xuống XCode 7 và nó đã cập nhật ứng dụng của tôi thành Swift 2. Có rất nhiều lỗi, nhưng hiện tại chỉ có một lỗi tôi không thể giải quyết tôi không biết tại sao, nhưng Xcode không thích bất kỳ Bool lựa chọn cho animated và hiển thị lỗi này -.Swift 2: "Bool" không thể chuyển đổi thành 'BooleanLiteralConvertible'

'Bool' không phải là chuyển đổi thành 'BooleanLiteralConvertible'

(nếu bạn nhìn vào các chức năng riêng của mình, bạn sẽ thấy, mà nó cần chính xác Bool cho animated)

var startVC = self.viewControllerAtIndex(indexImage) as ContentViewController 
var viewControllers = NSArray(object: startVC) 

self.pageViewContorller.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil) 
    'Bool' is not convertible to 'BooleanLiteralConvertible' 

Không ai biết, làm thế nào tôi có thể giải quyết nó?

Cảm ơn.

+0

Có một typo "pageViewContorller"? – Renzo

Trả lời

12

Swift bị nhầm lẫn và cung cấp cho bạn thông báo lỗi không chính xác. Vấn đề là các tham số đầu tiên là loại [UIViewController]?, vì vậy sau đây nên làm việc:

self.pageViewContorller.setViewControllers(viewControllers as? [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil) 

Hoặc thậm chí tốt hơn, tuyên bố viewControllers là loại [UIViewController] sau đó không đúc là cần thiết trong cuộc gọi:

let viewControllers:[UIViewController] = [startVC] 
self.pageViewContorller.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil) 
+0

Cảm ơn, nó hoạt động hoàn hảo! –

1

Cố gắng tránh đúc nếu có thể. Các Swift 1 declaration cho - setViewControllers:direction:animated:completion: đã thay đổi từ:

func setViewControllers(_ viewControllers: [AnyObject]!, 
      direction direction: UIPageViewControllerNavigationDirection, 
      animated animated: Bool, 
     completion completion: ((Bool) -> Void)!) 

để

func setViewControllers(viewControllers: [UIViewController]?, 
      direction: UIPageViewControllerNavigationDirection, 
      animated: Bool, 
     completion: ((Bool) -> Void)?) 

nên dàn diễn viên của mình lẫn lộn Swift 2 vì loại [AnyObject] của viewControllers không phù hợp [UIViewController]?. Mong đợi thêm nhiều API mục tiêu-C để được kiểm tra trong tương lai.

Đầu tiên sửa chữa viewControllerAtIndex để trả về một UIViewController:

func viewControllerAtIndex(index: Int) -> UIViewController { 
    ... 
} 

sau đó chỉ cần để cho Swift suy ra các loại chính xác:

let startVC = viewControllerAtIndex(indexImage) 

let viewControllers = [startVC] 

pageViewController.setViewControllers(viewControllers, 
    direction: .Forward, animated: true, completion: nil) 

đó là phiên bản có thể đọc được của:

let startVC: UIViewController = viewControllerAtIndex(indexImage) 

let viewControllers: [UIViewController] = 
    Array<UIViewController>(arrayLiteral: startVC) 

pageViewController.setViewControllers(viewControllers, 
    direction: UIPageViewControllerNavigationDirection.Forward, 
    animated: true, completion: nil) 
Các vấn đề liên quan