2016-05-16 14 views
5

मैं निम्नलिखित प्रोटोकॉल और इसके विस्तारस्विफ्ट प्रोटोकॉल - प्रकार एक आवश्यकता

public protocol RESEndpointReachable: CustomDebugStringConvertible 
{ 
    associatedtype EndpointType: RESEndpointReachable 


    // MARK: - Properties 

    /// The name of the endpoint as defined in the REST URI. 
    var name: String { get } 

    /// An array of possible next endpoints that this endpoint can reach. E.g account's next endpoints would be authenticate and unauthenticate. 
    var nextPossibleEndpoints: [EndpointType] { get } 


    // MARK: - Ability 

    /// Used to process the endpoint. 
    func processRequest(request: RERequest) 

    /// Processes the next endpoint that matches the name `name`. Expects an endpoint with the name `name` to exist in `nextPossibleEndpoints`. 
    func processNextEndpointWithName(name: String, request: RERequest) 
} 

public extension RESEndpointReachable 
{ 
    // MARK: - CustomDebugStringConvertible 

    public var debugDescription: String { 
     return name 
    } 


    // MARK: - RESEndpointReachable 

    var nextPossibleEndpoints: [EndpointType] { 
     return [] 
    } 

    public func processRequest(request: RERequest) 
    { 
     // Check all possible endpoints are being processed 
     if let nextEndpoint = nextPossibleEndpoints.first 
     { 
      fatalError("Unhandled endpoint \(nextEndpoint).") 
     } 
    } 

    public func processNextEndpointWithName(name: String, request: RERequest) 
    { 
     // Get the next endpoint that matches the specified name 
     let nextEndpoints = nextPossibleEndpoints.filter { $0.name == name } 

     if nextEndpoints.count > 1 
     { 
      fatalError("Multiple next endpoints found with the name '\(name)'.") 
     } 

     guard let nextEndpoint = nextEndpoints.first else 
     { 
      fatalError("No next endpoint with the name '\(name)'.") 
     } 


     // Process the next endpoint 
     nextEndpoint.processRequest(request) 
    } 
} 

इमारत पर है के रूप में खुद को संदर्भित नहीं कर सकते हैं, लाइन associatedtype EndpointType: RESEndpointReachable निम्न त्रुटि है: Type may not reference itself as a requirement। लेकिन जैसा कि मैं इसे समझता हूं, इस प्रकार आप स्विफ्ट में संबंधित प्रकारों का उपयोग करते हैं।

जैसा कि आपने अनुमान लगाया होगा, मैं हमेशा चाहता हूं कि EndpointTypeRESEndpointReachable से प्राप्त होने वाले प्रकार के रूप में सेट किया जा रहा है।

+0

मुझे यह सुविधा नहीं है अभी खून बह रहा हूँ! रेफरी: https://gist.github.com/curtclifton/1923a47774a94e904bf0 https://forums.developer.apple.com/thread/15256। यह रिकर्सिव लूप – sargeras

+0

धन्यवाद में संकलक चलाएगा :) मैंने सोचा कि शायद यह मामला हो सकता है ... शायद अगले महीने ... –

उत्तर

3

इस सुविधा को स्विफ्ट टीम द्वारा 'रिकर्सिव प्रोटोकॉल बाधा' के रूप में संदर्भित किया जाता है, और स्विफ्ट के भविष्य के संस्करण में जोड़ने के लिए रोडमैप पर है। इस और अन्य योजनाबद्ध सुविधाओं के बारे में अधिक जानकारी के लिए, स्विफ्ट टीम के 'Completing Generics' manifesto देखें।

+0

मुझे हास्यास्पद लगता है कि स्विफ्ट 4 अभी भी इसका समर्थन नहीं करता है- – d4Rk

+0

डौग ग्रेगोर की टिप्पणी के अनुसार इस समस्या को ट्रैक करने में (https://bugs.swift.org/browse/SR-1445), इसे स्विफ्ट 4.1 के साथ रिलीज़ किया गया था, इसलिए नवीनतम एक्सकोड _should_ जहाज को एक कंपाइलर के साथ समर्थन करता है जो इसका समर्थन करता है। (मैंने इसे स्वयं नहीं किया है, इसलिए मुझे नहीं पता कि सुविधा अभ्यास में कितनी अच्छी तरह काम करती है ...) – AustinZ

1

हाँ, यह तेज़ के साथ ठीक नहीं है। एक प्रोटोकॉल स्वयं को एक प्रकार की बाधा के रूप में उपयोग नहीं कर सकता है। एक समाधान एक अतिरिक्त प्रोटोकॉल घोषित करना है कि RESEndpointReachable स्वयं उस सुपर प्रोटोकॉल पर RESEndpointReachable को अपनाने और बाधित करेगा।

मैं उदाहरण किताब से आईओएस 10 प्रोग्रामिंग बुनियादी बातों स्विफ्ट साथ Neuburg एम द्वारा लिया (कृपया देखें पेज 194)

अवैध उदाहरण:

1 protocol Flier { 
    2   associatedtype Other : Flier 
    3   func flockTogetherWith(_ f: Other) 
    4 } 
    5 
    6 struct Bird : Flier { 
    7   func flockTogetherWith(_ f: Bird) {} 
    8 } 

समाधान:

1 protocol Superflier {} 
    2 protocol Flier: Superflier { 
    3   associatedtype Other : Superflier 
    4   func flockTogetherWith(_ f: Other) 
    5 }  
    6 
    7 struct Bird : Flier { 
    8   func flockTogetherWith(_ f: Bird) {} 
    9 } 

चीयर्स

संबंधित मुद्दे