Having a Swift class act as both a UIViewController subclass and a UITableViewDelegate
ios, protocols, swift, uiviewcontroller
Solution
From "Protocols" in the Swift Book:
Optional Protocol Requirements
You can define optional requirements for protocols, These requirements do not have to be implemented by types that conform to the protocol. Optional requirements are prefixed by the `@optional` keyword as part of the protocol’s definition.
So the `@optional` keyword is used only in the protocol definition to mark optional requirements. It is not used with implementations of the protocol methods. Removing `@optional` in your code is therefore the right solution.
That Xcode does not autocomplete protocol methods seems to a problem of the current beta release.
Problem
I have been writing an app in Swift, and as a result, need to write a class that both subclasses `UIViewController` and conforms to multiple protocols (including `UIAlertViewDelegate`, `UITableViewDelegate`, and `UITableViewDataSource`). I am currently using the Xcode 6 Beta, and have come across a lot of difficulty. The problem I'm experiencing stems from the class declaration: ``` class TableAddition : UIViewController, UIAlertViewDelegate { ``` It appears that the compiler isn't recognizing the protocol, and when I try to implement the following method: ``` @optional func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) { } ``` I get an error saying "'optional' attribute can only be applied to protocol members". Although removing the `@optional` silences the error, I don't believe that the method is recognized as a member of the `UIAlertViewDelegate` protocol (Xcode never autocompleted the method implementation for me). Is this a problem originating on the Swift side of things, or is this an error on my part?