http://stackoverflow.com/questions/24024466/how-do-i-make-a-class-conform-to-a-delegate-in-swift
Defining a protocol
1 2 3 4 |
protocol SampleProtocol { func someMethod() } |
Conforming to protocol
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class MyClass: SampleProtocol { // Conforming to SampleProtocol (implementing it) func someMethod() { } } class AnotherClass: SomeSuperClass, SampleProtocol { // A subclass conforming to SampleProtocol func someMethod() { } } |
Delegate it
To create a delegate in obj-c, it uses id to mean that this class can be of any type,
as long as it conforms to some protocol in the “<" ">” bracket. The variable name is delegate.
1 2 3 |
@interface FirstClass : NSObject @property (nonatomic, weak) id<SampleProtocol> delegate; @end |
in swift, it is simply declaring an optional variable called delegate of type SampleProtocol.
That way, it can show that its valid or nil.
The variable is of any type by default.
1 2 3 4 |
class FirstClass { var delegate:SampleProtocol? } |
Using your delegate
if you delegate is connected, using the protocol function will not be nil. If your delegate is not connected and is nil, then delegate?.YourFunc() will return nil
1 2 3 4 5 |
if delegate?.addHotel() != nil { print("hotel added to UIViewController") } else { print("your delegate is not connected!") } |
Optional protocol functions
http://stackoverflow.com/questions/24032754/how-to-define-optional-methods-in-swift-protocol
If you want to declare optional functions in your protocol, you must declare @obj keyword in your protocol declaration. You must ALSO declare @obj keyword in front of your optional function
1 2 3 4 5 |
@objc protocol MyProtocol2 { func doThis() @objc optional func doThat() } |
1 2 3 4 5 |
class MyClass : MyProtocol2 { func doThis() { } } |
Remember to declare the required protocol functions in your class. If not, you’ll get a compiler error to remind you to do so:
Object type of the delegate
In swift, a variable can be of any type. Its type is set when you assign it to an object.
Thus, in swift, you do not need to specify id, as you did in objective c.
Swift’s var vs Obj-C’s id
Swift is a statically typed language, so once a variable (var) is defined, the type of that variable will remain the same forever. With id in Objective-C, that’s not necessarily the case.
In Swift, once a variable is defined, you can’t change its type. However, an obj-c id can switch around.
This illustrates the difference between Swift and Objective-C, or in a more general sense, the difference between statically typed and dynamically typed languages.
Let’s look at the definition of those words: “static” and “dynamic”. Starting with “static”, courtesy of the Dictionary in OS X:
“lacking in movement, action, or change”
So if calling something static means that it lacks movement, action, or change, then saying a language is statically typed means that its types cannot change. And that’s what we have in Swift.
Contrast this with “dynamic”:
“characterized by constant change, activity, or progress”
Sounds a bit like Objective-C’s id type, right? Its type is contantly changing, or at least, it can be changed constantly, as we did in the example above.
“But wait,” you may be thinking, “Swift’s var is constantly changing, too – it’s just like Objective-C’s id.” And you’re half-right – we can do the following in Swift:
1 2 |
var a = 1 var b = "hello" |
So it looks like var is constantly changing – sometimes it represents an Int, sometimes a String. But the type of the variable never changes – our a is always an Int, and our b is always a String. And remember our previous example, where we tried to change x from 1 to “hello” and the compiler screamed at us. Once a variable’s type is set in Swift, it’s set. It can’t change. And that’s what static typing is.
So that brings us to another question: how is a variable’s type set in Swift? Let’s look at our previous Swift example again:
1 2 |
var x = 1 x = "hello" |
The error from the compiler (on line 2 above) gives us the type. The error says (in Xcode 6 Beta 4):
Type ‘Int’ does not conform to protocol ‘StringLiteralConvertible’
So we can conclude that Swift has inferred the type of x – it’s an Int, and we didn’t call it one. This is known as type inference, and it’s one nice feature in Swift that’s not in Objective-C.
Let’s look at the definition of the word “infer”:
“deduce or conclude (information) from evidence and reasoning rather than from explicit statements”
Swift uses type inference to conclude that our little x is of type Int, even though we didn’t explicitly state that x should be an Int. And then it prevents us from changing x to a String since Swift is statically typed. This is in stark contrast to Objcetive-C, which requires explicit types and is dynamically typed.
Swift’s var is decidedly not Objective-C’s id – it’s just a keyword we use to state that we’re creating a variable. That variable’s type is inferred by the compiler via a feature called type inference, and once that variable has a type, it always has that type. Once an Int, always an Int.
In Swift 3, the id type in Objective-C now maps to the Any type in Swift, which describes a value of any type, whether a class, enum, struct, or any other Swift type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class FirstClass { var delegate:MyProtocol? init() { delegate = TestClass() // delegate is like id, it takes on TestClass //thus, here, its type will be TestClass print( "String(delegate.dynamicType) -> \(type(of: delegate!))") } } var myvar1 = FirstClass() print( "String(myvar1.dynamicType) -> \(type(of: myvar1))") |