Objective-c Agent

Source: Internet
Author: User





1. Agent First Knowledge


Suppose the boss needs to buy a computer for the worker to buy



The boss needs an agent to buy a computer for the worker.



Boss creates a protocol protocol for the method of buying a computer



Bosses have a class of attribute properties that conform to the protocol



Boss has a member method method implementation for the agent to buy a computer



Worker classes need to follow protocol implementation protocol method buy a computer



The code is implemented as follows


 
 
 1 #import <Foundation/Foundation.h>
 2 #import "Boss.h"
 3 #import "Worker.h"
 4 
 5 int main(int argc, const char * argv[]) {
 6     @autoreleasepool {
 7 
 8         Boss * boss = [[Boss alloc] init];
 9         Worker * worker = [[Worker alloc] init];
10         [boss setDelegate:worker];
11         [boss delegateBuyMacbook];
12                 
13     }
14     return 0;
15 }
main.m
 
1 #import <Foundation / Foundation.h>
  2 
  3 // Create agreement
  4 @protocol BossDelegate <NSObject>
  5
  6-(void) buyMacbook;
  7
  8 @end
  9 
10
11 @interface Boss: NSObject
12 // There is an agent who abides by the agreement
13 @property (nonatomic, weak) id <BossDelegate> delegate;
14 // Method calls agent to buy computer
15-(void) delegateBuyMacbook;
16 @end
Boss.h
 
1 #import "Boss.h"
2 
3 @implementation Boss
4-(void) delegateBuyMacbook {
5 NSLog (@ "The boss started looking for someone to buy a computer");
6 [_delegate buyMacbook];
7}
8 @end
BOSS.M
 
1 #import <Foundation/Foundation.h>
2 #import "Boss.h"
3 @interface Worker : NSObject<BossDelegate>
4 
5 @end
Worker.h
1 #import "Worker.h"
2 
3 @implementation Worker
4-(void) buyMacbook {
5 NSLog (@ "Bought 60 Macbook Pro 15 high with");
6}
7 @end
WORKER.M





Objective-c Agent


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.