2016-04-03 15 views
8

Mục tiêu là cập nhật điều kiện bên dưới thành cú pháp Swift 2.2, lời khuyên sử dụng #selector or explicitly constructing a Selector.Đối số của #selector không thể tham chiếu đến thuộc tính

if activityViewController.respondsToSelector("popoverPresentationController") { 

} 

Tuy nhiên, bằng cách sử dụng dưới đây như là một sự thay thế không và tạo ra một lỗi nói Argument of #selector cannot refer to a property

if activityViewController.respondsToSelector(#selector(popoverPresentationController)) { 

} 

đúng cách để thực hiện việc kiểm tra này với #selector là gì?

+0

popOverPresentationController là một tài sản không một func. Đúng? – Darko

Trả lời

2

Bạn có thể sử dụng như sau:

if activityViewController.respondsToSelector(Selector("popoverPresentationController")) { 

} 

Hoặc nếu bạn nhắm mục tiêu iOS chỉ

if #available(iOS 8.0, *) { 
    // You can use the property like this 
    activityViewController.popoverPresentationController?.sourceView = sourceView 
} else { 

} 

Hoặc nếu mã của bạn không giới hạn iOS

#if os(iOS) 
    if #available(iOS 8.0, *) { 
     activityViewController.popoverPresentationController?.sourceView = sourceView 
    } else { 

    } 
#endif 
Các vấn đề liên quan