2017-11-14 2 views
0

Bestätigung Hier ist mein Stück Code,Strukturtyp Zurück zur Protokoll

protocol Configuration { 

    static var url: String { get } 
    static var username: String { get } 
    static var password: String { get } 
} 

protocol Server { 

    associatedtype Profile: Configuration 
    associatedtype Payment: Configuration 
    associatedtype Images: Configuration 
} 

struct Development: Server { 

    struct Profile: Configuration { 

     static var url: String { return "http://google.com" } 
     static var username: String { return "abc" } 
     static var password: String { return "xyz" } 
    } 

    struct Payment: Configuration { 

     static var url: String { return "http://bing.com" } 
     static var username: String { return "abc" } 
     static var password: String { return "xyz" } 
    } 

    struct Images: Configuration { 

     static var url: String { return "http://duckduckgo.com" } 
     static var username: String { return "abc" } 
     static var password: String { return "xyz" } 
    } 
} 

struct Production: Server { 

    struct Profile: Configuration { 

     static var url: String { return "http://amazon.com" } 
     static var username: String { return "pqr" } 
     static var password: String { return "xyz" } 
    } 

    struct Payment: Configuration { 

     static var url: String { return "http://facebook.com" } 
     static var username: String { return "pqr" } 
     static var password: String { return "xyz" } 
    } 

    struct Images: Configuration { 

     static var url: String { return "http://orkut.com" } 
     static var username: String { return "pqr" } 
     static var password: String { return "xyz" } 
    } 
} 

Frage ist, ich mag meine ‚Struktur‘ holen (je nach meiner Umgebung). Ich brauche etwas wie,

Aber die Rückgabe eines bestimmten Typs zu meinem Protokoll bestätigen ist nicht möglich.

Jeder Vorschlag zu diesem oder einem anderen Ansatz ist hilfreich.

+0

Wollen Sie Strom(), um den Strukturtyp zurück? Keine Instanz der Struktur? – andlin

+0

@andlin Ja, dann könnte ich nur statische Eigenschaften verwenden, – itsji10dra

Antwort

4

"Entwicklung" und "Produktion" sind Werte, keine Typen. Sie brauchen hier keine Protokolle und sollten sie nicht verwenden. Erstellen Sie einfach die Werte, die Sie brauchen, und sie zurück:

struct Server { 
    let url: String 
    let username: String 
    let password: String 
} 

struct Environment { 
    let profile: Server 
    let payment: Server 
    let images: Server 

    static let development = Environment(
     profile: Server(url: "http://google.com", username: "abc", password: "xyz"), 
     payment: Server(url: "http://bing.com", username: "abc", password: "xyz"), 
     images: Server(url: "http://duckduckgo.com", username: "abc", password: "xyz") 
    ) 

    static let production = Environment(...) 

    static var current: Environment { 
     #if DEBUG 
      return development 
     #else 
      return production 
     #endif 
    } 
} 
+0

Dies scheint ein guter Ansatz. Vielen Dank. – itsji10dra

+0

Bearbeitet, um es ein bisschen mehr zu vereinfachen. Es gab keinen Grund für drei Ebenen der Struktur; nur zwei. –

0

Rückkehr Nur Server, nicht Any:Server:

static func current() -> Server { 

    if #DEBUG 
     return Development 
    #endif 

    return Production 
} 
+0

Server ist ein Protokoll. Fehler: Protokoll 'Server' kann nur als generische Einschränkung verwendet werden, da es Selbst - oder assoziierte Typanforderungen hat. – itsji10dra

+0

Einverstanden, dass dies nicht behoben wird, aber Andreas ist richtig, 'Any: Server' (für jedes Protokoll, nicht nur' Server') wäre immer falsch. Sie können das "generische Constraint" -Problem nicht vermeiden, indem Sie einfach "Any" hineinwerfen. –

1

Sie können einen generischen Typ zurückgeben oder Sie können einfach etwas tun:

struct Config { 

    static var BaseURL: URL { 
    switch Environment.current { 
     case .debug: 
     return URL(string: "https://...dev.com")! 
     case .staging: 
     return URL(string: "https://...staging.com")! 
     case .release: 
     return URL(string: "https://...production.com")! 
     } 
    } 

}