Support for multiple iOS SDK versions when changing protocol invalid value

Protocol MCSessionDelegatechanged in iOS 11 of

- (void)                    session:(MCSession *)session
 didFinishReceivingResourceWithName:(NSString *)resourceName
                           fromPeer:(MCPeerID *)peerID
                              atURL:(NSURL *)localURL
                          withError:(nullable NSError *)error;

to

- (void)                    session:(MCSession *)session
 didFinishReceivingResourceWithName:(NSString *)resourceName
                           fromPeer:(MCPeerID *)peerID
                              atURL:(nullable NSURL *)localURL
                          withError:(nullable NSError *)error;

This leads to the fact that when implementing this delegate in Swift, use

func session(_ session: MCSession,
             didFinishReceivingResourceWithName resourceName: String,
             fromPeer peerID: MCPeerID,
             at localURL: URL?,
             withError error: Error?) {}

will not compile on Xcode 8; and

func session(_ session: MCSession,
             didFinishReceivingResourceWithName resourceName: String,
             fromPeer peerID: MCPeerID,
             at localURL: URL,
             withError error: Error?) {}

will not compile on Xcode 9.

In both cases, Xcode shows this error:

The parameter "session (_: didFinishReceivingResourceWithName: fromPeer: at: withError :)" has a different option than that required by the protocol "MCSessionDelegate"


How to compile in both versions?

+6
source share
2 answers

I do not understand why this is a problem.

Xcode 8, , AppStore. SDK iOS10 iOS10 iOS11.

Xcode 9, , ( Xcode 9 ) AppStore. iOS11 SDK iOS11.

, , , , Xcode 8 ( ), Xcode 9 ( iOS11). iOS11 git, , ?

+2

XCode, ,

#if swift(>=2.3)
let specifier = url.resourceSpecifier ?? ""
#else
let specifier = url.resourceSpecifier
#endif

heres , .. http://radex.io/xcode7-xcode8/:

func optionalize<T>(x: T?) -> T? {
    return x
}

, . , , :

let URL = optionalize(url) ?? "" // works on both versions!

, . , optionalize(), , , , , as-is. , , url (Xcode 8) (Xcode 7), "" .

( : Swift Foo Foo?, Foo . , - Foo Foo?.)

+1

All Articles