mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-11-22 21:52:31 +03:00
[NEW] Protocol Implementation
This commit is contained in:
parent
664d592bc7
commit
0932765947
@ -215,20 +215,22 @@ int main (int argc, const char * argv[])
|
|||||||
|
|
||||||
// Declare your class in a header(MyClass.h) file:
|
// Declare your class in a header(MyClass.h) file:
|
||||||
// Class Declaration Syntax:
|
// Class Declaration Syntax:
|
||||||
// @interface <class name> : <parent classname>
|
// @interface ClassName : ParentClassName <ImplementedProtocols>
|
||||||
// {
|
// {
|
||||||
// Member variable declarations;
|
// Member variable declarations;
|
||||||
// }
|
// }
|
||||||
// -/+ (type) Method declarations;
|
// -/+ (type) Method declarations;
|
||||||
// @end
|
// @end
|
||||||
@interface MyClass : NSObject
|
@interface MyClass : NSObject <MyCustomProtocol>
|
||||||
{
|
{
|
||||||
int count;
|
int count;
|
||||||
id data;
|
id data;
|
||||||
NSString *name;
|
NSString *name;
|
||||||
}
|
}
|
||||||
// Create the public getter/setter for the variable count
|
// Convenience notation to auto generate public getter and setter
|
||||||
@property(assign) int count;
|
@property int count;
|
||||||
|
@property (copy) NSString *name; // Copy the object during assignment.
|
||||||
|
@property (readonly) id data; // Declare only a getter method.
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
+/- (return type)methodSignature:(Parameter Type *)parameterName;
|
+/- (return type)methodSignature:(Parameter Type *)parameterName;
|
||||||
@ -246,8 +248,13 @@ int main (int argc, const char * argv[])
|
|||||||
|
|
||||||
@implementation UserObject
|
@implementation UserObject
|
||||||
|
|
||||||
|
// Call when the object is releasing
|
||||||
|
- (void)dealloc
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
// Constructors are a way of creating classes
|
// Constructors are a way of creating classes
|
||||||
// This is a default constructor
|
// This is a default constructor which is call when the object is creating
|
||||||
- (id)init
|
- (id)init
|
||||||
{
|
{
|
||||||
if ((self = [super init]))
|
if ((self = [super init]))
|
||||||
@ -272,8 +279,25 @@ int main (int argc, const char * argv[])
|
|||||||
return @42;
|
return @42;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Methods declared into MyProtocol
|
||||||
|
- (void)myProtocolMethod
|
||||||
|
{
|
||||||
|
// statements
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
/*
|
||||||
|
* A protocol declares methods that can be implemented by any class.
|
||||||
|
* Protocols are not classes themselves. They simply define an interface
|
||||||
|
* that other objects are responsible for implementing.
|
||||||
|
* /
|
||||||
|
@protocol MyProtocol
|
||||||
|
- (void)myProtocolMethod;
|
||||||
|
@end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
## Further Reading
|
## Further Reading
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user