Error when using Generic as property type in Swift

前端 未结 3 1323
清歌不尽
清歌不尽 2021-01-05 17:46

I\'m having an issue when using a Generic as the type constraint on a property. Here is a very simple example:

import UIKit

class TSSignal

        
相关标签:
3条回答
  • 2021-01-05 18:27

    This is NOT an "answer" (it is my own question), but I thought it worth noting what I did in this case to move passed this situation, for the time being.

    import UIKit
    class TSSignal {
        var message: AnyObject?
        init() {
        }
    }
    

    Lame, but I am sure it's only temporary.

    0 讨论(0)
  • 2021-01-05 18:28

    There is a workaround without type erasure (works as of Xcode6-Beta2):

    import UIKit
    
    class TSSignal<MessageType> {
        var _message: [MessageType] = []
    
        func getMessage() -> MessageType? {
            if _message.count > 0 {
                return _message[0]
            } else {
                return nil
            }
        }
    
        func setMessage(maybeMessage: MessageType?) {
            if let message = maybeMessage {
                _message = [message]
            } else {
                _message = []
            }
        }
    
        init() {
        }
    }
    
    0 讨论(0)
  • 2021-01-05 18:38

    EDIT EDIT:

    This is definitely a bug in the compiler.

    I tried to 'outsmart' the compiler by using the following:

    class TSSignal<TMessage>
    {
        var messageType : Optional<TMessage> = nil
    
        init() { }
    }
    

    Same issue.

    0 讨论(0)
提交回复
热议问题