2017-04-05 50 views
0

我遇到了使用泛型的問題。Swift通用返回結構體

我有一個協議:SectionReusableView

public protocol SectionReusableView { 

    associatedtype AdapterType: SectionReusableViewAdapter 

    var adapter: AdapterType? { get set } 

} 

我有這樣的結構TableViewSectionReusableSupplementraryViewFactory

public struct TableViewSectionReusableSupplementraryViewFactory<T: SectionReusableView> { 

    public var adapter: T.AdapterType 

    public init(adapter: T.AdapterType) { 

     self.adapter = adapter 

    } 

} 

我要的是創造這將返回任何TableViewSectionReusableSupplementraryViewFactory指定類型的功能T

func supplementaryViewFactory<T>(at index: Int, 
           within tableView: UITableView) -> TableViewSectionReusableSupplementraryViewFactory<T>? { 

    let adapter = self.adapter(at: index, within: tableView) 
    let reuseIdentifier = self.reuseIdentifier(at: index) 

    switch reuseIdentifier { 

    case AwesomeReusableView.AwesomeReusableViewReuseIdentifier: 

     guard let adapter = adapter as? AwesomeReusableViewAdapter else { return nil } 

     return TableViewSectionReusableSupplementraryViewFactory<AwesomeReusableView>(adapter: adapter) 

    default: 

     return nil 

    } 

} 

,但我得到這個錯誤,我不知道如何繞過

error: cannot convert return expression of type 
'TableViewSectionReusableSupplementraryViewFactory<AwesomeReusableView>' to return 
type 'TableViewSectionReusableSupplementraryViewFactory<T>?' 

回答

0

這是通用的功能沒怎麼用過。

本質上,通用函數專門用於T。創建一個全新的函數(在編譯時)用於使用各種類型調用的特殊情況。

如果你在你的例子中注意到你並沒有使用T來實現這個功能。您正在給它一個明確的返回類型,它可能會或可能不會匹配未知的T,編譯器不知道,因此也不能允許它。

讓我們想象一下,如果這樣做,說你打電話這樣的let factory = supplementaryViewFactory<FOO>(...),但在方法裏面,它實際上返回TableViewSectionReusableSupplementraryViewFactory<BAR>! OOPS你已經有了一個大問題,現在

這裏將是一個轉換的例子:

func supplementaryViewFactory<T>(...) -> TableViewSectionReusableSupplementraryViewFactory<T>? where T: SectionReusableView { 

    let adapter = MyCoolAdapterClass() 

    guard let converted = adapter as? T.AdapterType else { return nil } 

    return TableViewSectionReusableSupplementraryViewFactory<T>(adapter: converted) 

} 

幾件事情要注意,首先TableViewSectionReusableSupplementraryViewFactory類型得到由所需要的類型的專業。這意味着所謂的這種類型將最終決定什麼是T

所以你必須有這樣的:

let factory : TableViewSectionReusableSupplementraryViewFactory<AwesomeReusableView>? = supplementaryViewFactory(...) 

最後一點,斯威夫特是一個很有型安全的語言,如果你希望讓你的工廠方法返回一個未知的特殊類型它會可能最好進一步抽象爲僅返回原始協議。在這種情況下,您不需要關心支持類型是什麼。

+0

謝謝:)但我不能這樣做,調用補充的ViewFactory的類不知道哪個類型要專門化。 這就是爲什麼我想返回一個「任何」TableViewSectionReusableSupplementaryViewFactory – Pwyll28

+0

@ Pwyll28,我認爲,在這種情況下,我會建議從泛型轉向,只使用普通的jane協議。或者,如果您想保留泛型,請讓您的主結構實現一個協議,然後將其作爲工廠方法的返回類型,並從中刪除「」 – utahwithak

+0

但此方法的要點是返回的工廠或並使用工廠在另一個類中實現我的UITableViewDelegate/Datasource方法@utahwithak – Pwyll28