Cannot set delegate in Swift

Hello.


I am trying to create a TCP connection class with protocol and delegate instances and methods:


protocol IGAConnectionTCPDelegate
{
    var ipAddressString : String {get set}
    var ipPort : UInt32 {get set}
    var messageToServer : String {get set}

    func messageReceived(message: NSString)
    func displayErrorWithTitle(title: String, errorMessage err: String)
}
class IGAConnectionTCP: NSObject, NSStreamDelegate
{
    var delegate: IGAConnectionTCPDelegate?

    var inputStream : NSInputStream?
    var outputStream : NSOutputStream?
    var outputBuffer : NSMutableData!
    var error : NSError?

    func openNetworkCommunication()
    {

        if(self.inputStream != nil && self.outputStream != nil)
        {
            closeNetworkCommunication()
        }

        let ipAddress = delegate?.ipAddressString as! CFStringRef
        let portNumber = delegate?.ipPort

        var readStream:  Unmanaged<CFReadStream>?
        var writeStream: Unmanaged<CFWriteStream>?
        CFStreamCreatePairWithSocketToHost(nil, ipAddress, portNumber!, &readStream, &writeStream)

        self.inputStream = readStream!.takeRetainedValue()
        self.outputStream = writeStream!.takeRetainedValue()

        self.outputBuffer = delegate?.messageToServer.dataUsingEncoding(NSUTF8StringEncoding) as? NSMutableData

        self.inputStream?.delegate = self
        self.outputStream?.delegate = self

        self.inputStream?.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
        self.outputStream?.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

        self.inputStream?.open()
        self.outputStream?.open()

        println("OPENWORKCOMMUNICATION")

    }


    func closeNetworkCommunication()
    {
       ...

        println("CLOSENETWORKCOMMUNICATION")
    }


    func stream(theStream : NSStream, handleEvent streamEvent : NSStreamEvent)
    {
        ...
    }

   
    func sendToServer()
    {
       ...
    }

}


And then I try to make the following method conform to the protocol:


// GET MY FIRST ERROR MESSAGE HERE
class IGAFlightplanVC: UIViewController, IGAConnectionTCPDelegate
{
    var connectionObj = IGAConnectionTCP()

    var ipAddressString = "192.168.1.6"
    var ipPort = 7767
    var messageToServer = "MESSAGE"

    override func viewDidLoad()
    {
        super.viewDidLoad()

        //GET MY SECOND ERROR HERE!!! 
        connectionObj.delegate = self
    }


    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    func messageReceived(message : NSString)
    {
        println(message)
    }


    func makeMessageToServer() -> String
    {
        let finalMessage = "MESSAGE:";
        return finalMessage;
    }


    func displayErrorWithTitle(title: String, errorMessage: String)
    {
        var disconnectMsg = UIAlertController(title: title, message: errorMessage as String, preferredStyle: UIAlertControllerStyle.Alert)
        disconnectMsg.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(disconnectMsg, animated: true, completion: nil)
    }


My first error message is:

class IGAFlightplanVC: UIViewController, IGAConnectionTCPDelegate
Type 'IGAFlightplanVC' does not conform to protocol IGAConnectionTCPDelegate


My second error message is:

self.connectionObj.delegate = self
Cannot assign a value of type 'IGAFlighplanVC' to a value of type IGAConnectionTCPDelegate?

What am I doing wrong?

Thanks a lot!

Answered by marchyman in 48108022

Your protocal says...

var ipPort : UInt32 {get set}


but the class that you are trying to conform to that protocal says...


var ipPort = 7767


That ipPort is an Int, not a UInt32. That doesn't conform to the protocol.

Accepted Answer

Your protocal says...

var ipPort : UInt32 {get set}


but the class that you are trying to conform to that protocal says...


var ipPort = 7767


That ipPort is an Int, not a UInt32. That doesn't conform to the protocol.

Changed to:


var ipPort : UInt32 = 7767


Working now. Thanks a lot!

Just as a matter of style, I think

Int
would be better here. Both
UInt
and
Int
can hold invalid port values, so you still have to handle the ‘value out of range’ case. And Swift, in general, recommends
Int
over
UInt
unless there are really good reasons to do otherwise.

Notably, if you look at the new-in-iOS 8

+[NSStream getStreamsToHostWithName:port:inputStream:outputStream:]
, you’ll see it uses
Int
.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you, eskimo. I will change it. Best.

Cannot set delegate in Swift
 
 
Q