Objective-c a chain-adder calculator implementation

Source: Internet
Author: User



A chain-type addition calculator to realize the idea





1. Effect on use



Calculate * Manger=[calculate new];



int Result=manger.add (123). Add (123). Sub (123). Result;





The output is: result:123





2. Implementation methods



New calculate class;



Add an attribute @property (nonatomic,assign) int result as the result of the calculation;



This is because the method used is Result=manger.add (123). Add (123). Sub (123) .....



(In OC midpoint syntax is essentially the Get,set method that invokes the object's properties)



can use the DOT syntax to invoke the description Add (123) is the property of the Manger object, with the parameter 1231 is the block to go as a property,



Because the point syntax can be used consecutively , the return value of Add (123) should be the object itself ,



This property can be invoked using point syntax after returning the object itself;






The value type of this block's parameter should be float/double/int, etc. , used to pass the 123 value in "Add (123)".



Okay, OK. The return value type and the parameter type of the block are determined, so you can begin to design the block.



Take the input parameter value type int as an example should be



calculter* (^) (int) =^ (int inputnum) {



Self.result+=inputnum;



return self; };



Take calculter* (^) (int) as the type of block, and implement block operation in the corresponding Get method;



Here's the full code:


1 // Calculter.h
  2 // Created by longkin on 16/1/12.
  3
  4 @class Calculter;
  5 #import <Foundation/Foundation.h>
  6 //The name of the block is CalculateOption The return value type is Calculter* The parameter type is int
  7 typedef Calculter*(^CalculateOption)(int);
  8 
  9 @interface Calculter: NSObject
10 @property(nonatomic,assign) int result;
11 @property(nonatomic,copy) CalculateOption add;
12 @property(nonatomic,copy) CalculateOption sub;
13 @property(nonatomic,copy) CalculateOption muilt;
14 @property(nonatomic,copy) CalculateOption divide;
15 @end 
1 // Calculter.m
  2 // Created by longkin on 16/1/12.
  3
  4 #import "Calculter.h"
  5 @implementation Calculter
  6
  7 /**
  8 *The return value type of the get method of add is block, and the return value of block is the object itself
  9  */
10 -(CalculateOption)add
11 {
12 return ^(int inputNum){
13 self.result+=inputNum;
14 return self;
15 };
16}
17
18 -(CalculateOption)sub
19 {
20 return ^(int inputNum){
21 self.result-=inputNum;
22 return self;
twenty three     };
twenty four }
25
26 -(CalculateOption)muilt
27 {
28 return ^(int inputNum){
29 self.result*=inputNum;
30 return self;
31 };
32}
33
34 -(CalculateOption)divide
35 {
36 return ^(int inputNum){
37 self.result/=inputNum;
38 return self;
39 };
40}
41
42 @end 


Use effect:


    Calculter* calc =[Calculter new];
        int result =calc.add(1).add(2).muilt(3).divide(3).result;
        






Objective-c a chain-adder calculator implementation


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.