-2

Can someone explain me what does this code means? Is it a good coding practice to such code. MyProtocol and OtherProtcol are some protocol method. What does NSObject mean here?

@interface MYViewController () {

    NSObject<MyProtocol> * abc;
    NSObject<OtherProtcol> * def;

}
Madu
  • 4,849
  • 9
  • 44
  • 78

1 Answers1

0

This class has 2 properties that accepts any object (subclass of NSObject) and must conform to MyProtocol and OtherProtocol

example:

@interface SomeClass {
}

myViewController.abc = [SomeClass new]; // error this class does not conform to MyProtocol 

@interface AnotherClass<MyProtocol> {
}

myViewController.abc = [AnotherClass new]; // works
myViewController.def = [AnotherClass new]; // error AnotherClass does not conform to OtherProtocol

This is the new protocol oriented programming which you should use a lot instead of subclassing. Check wwdc protocol oriented programming with swift

Basheer_CAD
  • 4,908
  • 24
  • 36
  • why do we need to have NSObjecct usually we used to have id and will it not be a good idea to make them properties of this class? – Madu Aug 10 '15 at 14:56
  • id can be any type, or any object, which will be translated as AnyObject in swift (structs, Int). NSObject implements copy so you can add it to dictionary. And also in swift some value types can conform to protocols which can not be exposed to objc, so in this case you protect your self – Basheer_CAD Aug 10 '15 at 15:02
  • The two properties probably should be declared with `id` and not `NSObject` in addition to the specific protocol. The protocols should extend the `NSObject` protocol (not to be confused with the `NSObjet` class). – rmaddy Aug 10 '15 at 16:03