[swift-users] Optional conformance warnings with Protocols

Rod Brown rodney.brown6 at icloud.com
Sun Jul 24 08:55:38 CDT 2016


The final code to test this is:

import Foundation
import MapKit

class ManifestItem: NSObject {
    
    let title: String!
    let subtitle: String?
    dynamic var coordinate: CLLocationCoordinate2D
    
    init(title: String, subtitle: String?, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate
    }
}

extension ManifestItem: MKAnnotation {
}

This displays a warning in beta 3, as well as in Swift 2.2 in the current Xcode.

I understand why: MKAnnotation declares my title must be optional (“?”) but I am declaring it as an implicitly unwrapped optional (“!”). Still, I believe the fact is that they are both optional, and are a legitimate way of declaring “a protocol I conform to requires this to be optional, but it can be treated as implicitly inwrapped because it will never be nil”.

Thoughts?

- Rod



> On 24 Jul 2016, at 11:09 PM, Zhao Xin <owenzx at gmail.com> wrote:
> 
> I pasted you code into Xcode 8 beta3 playground, everything seemed fine with below code.
> 
> import Cocoa
> import CoreLocation
> 
> class ManifestItem: NSObject {
>     let value:    String
>     let title:    String
>     let subtitle: String?
>     dynamic var coordinate: CLLocationCoordinate2D
>     
>     init(value:String, title:String, subtitle:String? = nil, coordinate:CLLocationCoordinate2D) {
>         self.value = value
>         self.title = title
>         self.subtitle = subtitle
>         self.coordinate = coordinate
>     }
> }
> 
> @objc public protocol MKAnnotation : NSObjectProtocol {
>     
>     // Center latitude and longitude of the annotation view.
>     // The implementation of this property must be KVO compliant.
>     var coordinate: CLLocationCoordinate2D { get }
>     
>     // Title and subtitle for use by selection UI.
>     @objc optional var title: String? { get }
>     @objc optional var subtitle: String? { get }
> }
> 
> I didn't encounter the error you mentioned. But I had to remove public and add @objc as Xcode asked.
> 
> Zhaoxin
> 
> On Sun, Jul 24, 2016 at 12:09 PM, Rod Brown via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
> Hi Swift Users,
> 
> I just ran across an issue where I had the following code:
> 
> class ManifestItem: NSObject {
>     let value:    String
>     let title:    String
>     let subtitle: String?
>     dynamic var coordinate: CLLocationCoordinate2D
> }
> 
> 
> As part of utilising this object, I needed it to conform to MKAnnotation, which declares:
> 
> 
> public protocol MKAnnotation : NSObjectProtocol {
>     
>     // Center latitude and longitude of the annotation view.
>     // The implementation of this property must be KVO compliant.
>     public var coordinate: CLLocationCoordinate2D { get }
>     
>     // Title and subtitle for use by selection UI.
>     optional public var title: String? { get }
>     optional public var subtitle: String? { get }
> }
> 
> I added the appropriate extension to “ManifestItem” and promptly got reminded with an error that MKAnnotation requires title be an optional.
> 
> To do this, I converted “title” in my ManifestItem class to be an implicitly unwrapped optional. This value can never be nil, and should be treated as such throughout my code. However, the compiler still emits a warning. "Type of 'title' has different optionality than expected by protocol ‘MKAnnotation' "
> 
> I’m wondering if this should be considered a bug? I am actually comforming correctly to the protocol - title is optional, though implicitly unwrapped. Should I have to make “title” optional, and every use of it optional or a force unwrap, when my code verifies that the manifest item is legitimately invalid if title property is nil?
> 
> I think this is valid use of the implicitly unwrapped optional. It allows you to say “this is optional for some reason, but should generally always be assumed to be non null.” This sounds remarkably similar to my use case "this is optional for conformance reasons, but should generally always be assumed to be non null.”
> 
> Thanks for your opinions,
> 
> Rod
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org <mailto:swift-users at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160724/ba798ef6/attachment.html>


More information about the swift-users mailing list