2015-07-05 17 views
6

Vorrei creare una funzione di supporto generica che elabori alcune elaborazioni. Uno dei suoi input sarà una funzione che restituisce una matrice di qualcosa.L'argomento per il parametro generico "T" non è stato dedotto

Non riesco a capire come farlo. Continuo a ricevere un errore di compilazione. Ho trovato il posto

argument for generic parameter could not be inferred

e provato ad aggiungere ...as [String] o ... as [Int], ecc, ma senza fortuna.

func helperThatDoesSomeGenericProcessing<T>(displayedStrings:() -> [T]) -> [String]! { 
    let listOfSomething: [T] = displayedStrings() 
    // do something with listOfSomething 
    return ["some", "resulting", "string", "from", "the", "input"] 
} 

func concreteFunction1() -> [AnyObject]! { 
    var s: [String] = helperThatDoesSomeGenericProcessing { // ERROR: Argument for generic parameter 'T' could not be inferred. 
     var str = ["One", "Two", "Three"] 
    } // tried 'as [String]' here 

    // do something with s 
    return s 
} 

func concreteFunction2() -> [AnyObject]! { 
    var s: [Int] = helperThatDoesSomeGenericProcessing { // ERROR: Argument for generic parameter 'T' could not be inferred. 
     var i = [1, 2, 3] 
    } // tried 'as [Int]' here 

    // do something with s 
    return s 
} 
+0

Che cosa stai cercando di fare esattamente? Questo è piuttosto confuso senza ulteriori dettagli su che tipo di elaborazione si sta tentando di fare ... perché sembra un po 'come un problema XY forse. – nhgrif

risposta

5

Aggiunta return in modo appropriato e in modo esplicito dichiarando il tipo concreto di () -> [T] risolvere gli errori ... ma non sono sicuro che sarà ottenere ciò che volere. Ad ogni modo ecco il codice:

func helperThatDoesSomeGenericProcessing<T>(displayedStrings:() -> [T]) -> [String]! { 
    let listOfSomething: [T] = displayedStrings() 
    // do something with listOfSomething 
    return ["some", "resulting", "string", "from", "the", "input"] 
} 

func concreteFunction1() -> [AnyObject]! { 
    var s: [String]! = helperThatDoesSomeGenericProcessing { 
     () -> [String] in // Explicit declared type 
     var str = ["One", "Two", "Three"] 
     return str 
    } // tried 'as [String]' here 

    // do something with s 
    return s 
} 

func concreteFunction2() -> [AnyObject]! { 
    var s: [String]! = helperThatDoesSomeGenericProcessing { 
     () -> [Int] in // Explicit declared type 
     var i = [1, 2, 3] 
     return i 
    } // tried 'as [Int]' here 

    // do something with s 
    return s 
} 

Avviso Ho anche corretto il tipo di var s poiché la vostra funzione generica restituisce sempre una matrice facoltativa implicitamente scartato di stringhe [String]!. Il tipo di reso non è generalizzato (ad esempio T o [T] ecc.).

Probabilmente potrebbe essere necessario modificare alcune dichiarazioni di tipo per soddisfare le esigenze di progettazione.

Spero che questo aiuti

+0

Perfetto! Grazie. – Daniel

0

hi vostro corpo di chiusura non fa ritorno

func concreteFunction1() -> [AnyObject]! { 
    let s: [String] = helperThatDoesSomeGenericProcessing { // ERROR: Argument for generic parameter 'T' could not be inferred. 
    return ["One", "Two", "Three"] 
} // tried 'as [String]' here 

    // do something with s 
    return s 
} 
+0

Che ancora non sembra risolvere il problema. Ho ancora lo stesso errore. – Daniel

Problemi correlati