Swift Events

In swift, events should be implemented from the Delegate protocol found in the .swift file. The examples in this article will be from the perspective of the FTP component, but note that the same steps could be followed for other components as well.

An easy way to implement the protocol is to copy the definition from IPWorksFTP.swift. Simply copy the method definitions for IPWorksFTPDelegateSwift into your class, and add your implementation.

For instance, you could create a class called MyFTPSwiftEvents to implement events from the IPWorksFTPDelegateSwift protocol like so: class MyFTPSwiftEvents: IPWorksFTPDelegateSwift{ func onConnectionStatus(connectionEvent: String, _ statusCode: Int32, _ description: String){ } func onDirList(dirEntry: String, _ fileName: String, _ isDir: Bool, _ fileSize: Int64, _ fileTime: String){ } func onEndTransfer(direction: Int32){ } func onError(errorCode: Int32, _ description: String){ } func onPITrail(direction: Int32, _ message: String){ } func onStartTransfer(direction: Int32){ } func onTransfer(direction: Int32, _ bytesTransferred: Int64, _ percentDone: Int32, _ text: NSData){ } }

You would then assign an instance of your newly implemented MyFTPSwiftEvents class as a delegate of your IPWorksFTPSwift class: var ftp = IPWorksFTPSwift() ftp.delegate = MyFTPSwiftEvents() ftp.remoteHost = server ftp.remotePort = port ftp.user = user ftp.password = password ftp.logon()

Note: Swift doesn't allow other classes to have access to variables within the first class, so in order to access variables outside of the MyFTPSwiftEvents class from within the events, you'll need to pass the instance of the this class into the constructor, like so: class MyFTPSwiftEvents: IPWorksFTPDelegateSwift{ var main: DetailViewController = DetailViewController() init(outer: DetailViewController){ main = outer } //other implemented events } var ftp = IPWorksFTPSwift() ftp.delegate = MyFTPSwiftEvents(main: self)

Once you can access the first class within the second class, getter and setter methods can be used to modify variables.

We appreciate your feedback.  If you have any questions, comments, or suggestions about this article please contact our support team at kb@nsoftware.com.