SlideShare una empresa de Scribd logo
1 de 16
Descargar para leer sin conexión
iOS:	
  Selectors,	
  Delega1on,	
  Blocks	
  

              Jussi	
  Pohjolainen	
  
About	
  callbacks	
  
•  iOS	
  provides	
  several	
  ways	
  of	
  handling	
  call	
  
   backs	
  
    1.  Selectors	
  
       •    Func1on	
  pointer,	
  passing	
  method	
  as	
  on	
  argument.	
  	
  
    2.  Delegate	
  protocols 	
  	
  
       •    Several	
  call	
  back	
  methods	
  
    3.  Blocks	
  
       •    Anonymous	
  func1ons	
  that	
  have	
  access	
  to	
  local	
  
            variables	
  
Selectors	
  
•  Selectors	
  are	
  a	
  way	
  to	
  pass	
  a	
  method	
  around.	
  
•  In	
  Java:	
  reflec1on	
  
•  Common	
  use	
  in	
  controls	
  (target	
  –	
  ac1on	
  paMern)	
  
	
  
   [myButton addTarget:self
                action:@selector(myButtonWasPressed)
      forControlEvents:UIControlEventTouchUpInside];

   - (void)myButtonWasPressed {
       // Do something about it
   }
Selectors	
  
- (void) testSelectors
{
    SEL variable;
    variable = @selector(animate);

    [self methodWithSelectorAsArgument: variable];
}

- (void) methodWithSelectorAsArgument: (SEL) argument
{
    [self performSelector:argument];
}

- (void) animate
{
    ...
}
Delegate	
  
•  Using	
  protocols	
  (interfaces)	
  you	
  can	
  define	
  a	
  
   contract	
  that	
  the	
  implementor	
  fulfills	
  
•  Making	
  call	
  backs:	
  
    –  [locationManager setDelegate: self)
•  Now	
  the	
  self	
  object	
  must	
  conform	
  to	
  protocol	
  
   and	
  it’s	
  protocol	
  methods	
  are	
  called	
  (call	
  back)	
  
    –  @property id<CLLocationManagerDelegate>
       delegate
Blocks	
  
•  Good	
  mechanism	
  for	
  call	
  back	
  methods	
  
    –  Replaces	
  some	
  of	
  the	
  delegate	
  protocol	
  methods	
  
•  Block	
  is	
  a	
  chunk	
  of	
  code	
  that	
  can	
  be	
  executed	
  at	
  
   some	
  future	
  1me	
  
•  Blocks	
  are	
  func1ons	
  that	
  are	
  objects,	
  they	
  can	
  be	
  
   passed	
  as	
  variables.	
  
•  Code	
  and	
  Handling	
  task	
  are	
  related	
  to	
  each	
  
   other	
  
•  In	
  Java	
  7:	
  anonymous	
  inner	
  classes.	
  In	
  Java	
  8:	
  
   lambda	
  expressions	
  
Java	
  
Thread thread = new Thread(new Runnable() {
   public void run() {
     // The code
  }
});
Declara1on	
  Form	
  
•  The	
  declara1on	
  form	
  of	
  block	
  is	
  
    –  return_type (^block_name) (param_type,
       param_type, …)
•  Example	
  
    –  int (^add) (int, int)
•  ^ =	
  “I’m	
  a	
  block”	
  
Block	
  Defini1on	
  
•  The	
  declara1on	
  form	
  of	
  block	
  is	
  
       –  ^return_type (param_type, param_type, …)
          { ... return return_type; };
•  Example	
  
       –  ^int (int n1, int n2) { return n1+n2; }
	
  
Declara1on	
  and	
  Defini1on	
  together	
  
// Declaration of block variable add
int (^add) (int, int);

// Definition for the add block variable
add = ^int (int n1, int n2)
{
   return n1+n2;
};

// Usage
int number = add(2,2);
	
  
Block	
  as	
  an	
  Argument	
  
// The method takes a block as an argument
// The block must return int and it must have
// two integer arguments
- (void) someMethod: (int (^) (int, int)) variable
{
    NSLog(@"%i", variable(2,2));
}

- (void) someOtherMethod
{
    // Declaration of block variable add
    int (^add) (int, int);

    // Definition for the add block variable
    add = ^int (int n1, int n2)
    {
          return n1+n2;
    };

    // Calling the someMethod with an argument block
    [self someMethod: add];
}
Block	
  as	
  an	
  Anonymous	
  Argument	
  
- (void) someMethod: (int (^) (int, int)) variable
{
    NSLog(@"%i", variable(2,2));
}

- (void)someOtherMethod
{
    [self someMethod:
        ^int (int n1, int n2) {
            return n1+n2;
        }
    ];
}
UIView	
  Anima1on	
  without	
  Blocks	
  
- (void)removeAnimationView:(id)sender {
    [animatingView removeFromSuperview];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [UIView beginAnimations:@"Example" context:nil];
    [UIView setAnimationDuration:5.0];
    [UIView setAnimationDidStopSelector:@selector(removeAnimationView)];
    [animatingView setAlpha:0];
    [animatingView setCenter:CGPointMake(animatingView.center.x+50.0,
                                         animatingView.center.y+50.0)];
    [UIView commitAnimations];
}
Anima1on	
  with	
  Blocks	
  
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [UIView animateWithDuration:5.0

            animations:^{
                [animatingView setAlpha:0];
                [animatingView setCenter:CGPointMake(animatingView.center.x+50.0,
                                                     animatingView.center.y+50.0)];
                }

            completion:^(BOOL finished) {
                [animatingView removeFromSuperview];
            }

    ];
}
Block	
  Advantages	
  
•  Simplifies	
  code	
  
•  Keeps	
  code	
  together	
  
•  Apple	
  official	
  recommenda1on	
  (use	
  block	
  
   methods	
  if	
  possible)	
  
Recap	
  
•  Selectors	
  
    –  Invoke	
  a	
  method	
  but	
  you	
  don’t	
  the	
  name	
  of	
  the	
  
       method	
  at	
  compile	
  1me	
  
•  Delegates	
  
    –  Great	
  when	
  there	
  is	
  need	
  for	
  several	
  call	
  back	
  
       methods	
  
•  Blocks	
  
    –  Only	
  need	
  for	
  one	
  anonymous	
  method	
  with	
  
       access	
  of	
  local	
  variables,	
  blocks	
  are	
  great	
  

Más contenido relacionado

La actualidad más candente

Operator overloading
Operator overloadingOperator overloading
Operator overloadingArunaDevi63
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKamal Acharya
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Nettjunicornfx
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversionsAmogh Kalyanshetti
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpprajshreemuthiah
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overridingRajab Ali
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 

La actualidad más candente (20)

14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4
 
Understanding Subroutines and Functions in VB6
Understanding Subroutines and Functions in VB6Understanding Subroutines and Functions in VB6
Understanding Subroutines and Functions in VB6
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
 
Java8
Java8Java8
Java8
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3
 

Similar a iOS Selectors Blocks and Delegation

Objective C Block
Objective C BlockObjective C Block
Objective C BlockFantageek
 
iOS Development with Blocks
iOS Development with BlocksiOS Development with Blocks
iOS Development with BlocksJeff Kelley
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsSubhransu Behera
 
Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not JavaChris Adamson
 
Pavel kravchenko obj c runtime
Pavel kravchenko obj c runtimePavel kravchenko obj c runtime
Pavel kravchenko obj c runtimeDneprCiklumEvents
 
iOS 2 - The practical Stuff
iOS 2 - The practical StuffiOS 2 - The practical Stuff
iOS 2 - The practical StuffPetr Dvorak
 
iPhone dev intro
iPhone dev introiPhone dev intro
iPhone dev introVonbo
 
Beginning to iPhone development
Beginning to iPhone developmentBeginning to iPhone development
Beginning to iPhone developmentVonbo
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN StackTroy Miles
 
DIG1108C Lesson 6 - Fall 2014
DIG1108C Lesson 6 - Fall 2014DIG1108C Lesson 6 - Fall 2014
DIG1108C Lesson 6 - Fall 2014David Wolfpaw
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxINTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxDeepasCSE
 
My Favourite 10 Things about Xcode/ObjectiveC
My Favourite 10 Things about Xcode/ObjectiveCMy Favourite 10 Things about Xcode/ObjectiveC
My Favourite 10 Things about Xcode/ObjectiveCJohnKennedy
 
45 aop-programming
45 aop-programming45 aop-programming
45 aop-programmingdaotuan85
 
React Native One Day
React Native One DayReact Native One Day
React Native One DayTroy Miles
 

Similar a iOS Selectors Blocks and Delegation (20)

Objective C Block
Objective C BlockObjective C Block
Objective C Block
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
iOS Development with Blocks
iOS Development with BlocksiOS Development with Blocks
iOS Development with Blocks
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIs
 
Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not Java
 
Pavel kravchenko obj c runtime
Pavel kravchenko obj c runtimePavel kravchenko obj c runtime
Pavel kravchenko obj c runtime
 
iOS 2 - The practical Stuff
iOS 2 - The practical StuffiOS 2 - The practical Stuff
iOS 2 - The practical Stuff
 
iPhone dev intro
iPhone dev introiPhone dev intro
iPhone dev intro
 
Beginning to iPhone development
Beginning to iPhone developmentBeginning to iPhone development
Beginning to iPhone development
 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
 
DIG1108C Lesson 6 - Fall 2014
DIG1108C Lesson 6 - Fall 2014DIG1108C Lesson 6 - Fall 2014
DIG1108C Lesson 6 - Fall 2014
 
Spock
SpockSpock
Spock
 
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptxINTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING.pptx
 
My Favourite 10 Things about Xcode/ObjectiveC
My Favourite 10 Things about Xcode/ObjectiveCMy Favourite 10 Things about Xcode/ObjectiveC
My Favourite 10 Things about Xcode/ObjectiveC
 
45 aop-programming
45 aop-programming45 aop-programming
45 aop-programming
 
React Native One Day
React Native One DayReact Native One Day
React Native One Day
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 

Más de Jussi Pohjolainen

libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferencesJussi Pohjolainen
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationJussi Pohjolainen
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDXJussi Pohjolainen
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript DevelopmentJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDXJussi Pohjolainen
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDXJussi Pohjolainen
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesJussi Pohjolainen
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platformJussi Pohjolainen
 

Más de Jussi Pohjolainen (20)

Moved to Speakerdeck
Moved to SpeakerdeckMoved to Speakerdeck
Moved to Speakerdeck
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
 
Box2D and libGDX
Box2D and libGDXBox2D and libGDX
Box2D and libGDX
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
 
libGDX: Tiled Maps
libGDX: Tiled MapslibGDX: Tiled Maps
libGDX: Tiled Maps
 
libGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame AnimationlibGDX: User Input and Frame by Frame Animation
libGDX: User Input and Frame by Frame Animation
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDX
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
libGDX: Scene2D
libGDX: Scene2DlibGDX: Scene2D
libGDX: Scene2D
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
libGDX: User Input
libGDX: User InputlibGDX: User Input
libGDX: User Input
 
Implementing a Simple Game using libGDX
Implementing a Simple Game using libGDXImplementing a Simple Game using libGDX
Implementing a Simple Game using libGDX
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
 
Creating Games for Asha - platform
Creating Games for Asha - platformCreating Games for Asha - platform
Creating Games for Asha - platform
 
Intro to Asha UI
Intro to Asha UIIntro to Asha UI
Intro to Asha UI
 

Último

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Último (20)

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 

iOS Selectors Blocks and Delegation

  • 1. iOS:  Selectors,  Delega1on,  Blocks   Jussi  Pohjolainen  
  • 2. About  callbacks   •  iOS  provides  several  ways  of  handling  call   backs   1.  Selectors   •  Func1on  pointer,  passing  method  as  on  argument.     2.  Delegate  protocols     •  Several  call  back  methods   3.  Blocks   •  Anonymous  func1ons  that  have  access  to  local   variables  
  • 3. Selectors   •  Selectors  are  a  way  to  pass  a  method  around.   •  In  Java:  reflec1on   •  Common  use  in  controls  (target  –  ac1on  paMern)     [myButton addTarget:self action:@selector(myButtonWasPressed) forControlEvents:UIControlEventTouchUpInside]; - (void)myButtonWasPressed { // Do something about it }
  • 4. Selectors   - (void) testSelectors { SEL variable; variable = @selector(animate); [self methodWithSelectorAsArgument: variable]; } - (void) methodWithSelectorAsArgument: (SEL) argument { [self performSelector:argument]; } - (void) animate { ... }
  • 5. Delegate   •  Using  protocols  (interfaces)  you  can  define  a   contract  that  the  implementor  fulfills   •  Making  call  backs:   –  [locationManager setDelegate: self) •  Now  the  self  object  must  conform  to  protocol   and  it’s  protocol  methods  are  called  (call  back)   –  @property id<CLLocationManagerDelegate> delegate
  • 6. Blocks   •  Good  mechanism  for  call  back  methods   –  Replaces  some  of  the  delegate  protocol  methods   •  Block  is  a  chunk  of  code  that  can  be  executed  at   some  future  1me   •  Blocks  are  func1ons  that  are  objects,  they  can  be   passed  as  variables.   •  Code  and  Handling  task  are  related  to  each   other   •  In  Java  7:  anonymous  inner  classes.  In  Java  8:   lambda  expressions  
  • 7. Java   Thread thread = new Thread(new Runnable() { public void run() { // The code } });
  • 8. Declara1on  Form   •  The  declara1on  form  of  block  is   –  return_type (^block_name) (param_type, param_type, …) •  Example   –  int (^add) (int, int) •  ^ =  “I’m  a  block”  
  • 9. Block  Defini1on   •  The  declara1on  form  of  block  is   –  ^return_type (param_type, param_type, …) { ... return return_type; }; •  Example   –  ^int (int n1, int n2) { return n1+n2; }  
  • 10. Declara1on  and  Defini1on  together   // Declaration of block variable add int (^add) (int, int); // Definition for the add block variable add = ^int (int n1, int n2) { return n1+n2; }; // Usage int number = add(2,2);  
  • 11. Block  as  an  Argument   // The method takes a block as an argument // The block must return int and it must have // two integer arguments - (void) someMethod: (int (^) (int, int)) variable { NSLog(@"%i", variable(2,2)); } - (void) someOtherMethod { // Declaration of block variable add int (^add) (int, int); // Definition for the add block variable add = ^int (int n1, int n2) { return n1+n2; }; // Calling the someMethod with an argument block [self someMethod: add]; }
  • 12. Block  as  an  Anonymous  Argument   - (void) someMethod: (int (^) (int, int)) variable { NSLog(@"%i", variable(2,2)); } - (void)someOtherMethod { [self someMethod: ^int (int n1, int n2) { return n1+n2; } ]; }
  • 13. UIView  Anima1on  without  Blocks   - (void)removeAnimationView:(id)sender { [animatingView removeFromSuperview]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [UIView beginAnimations:@"Example" context:nil]; [UIView setAnimationDuration:5.0]; [UIView setAnimationDidStopSelector:@selector(removeAnimationView)]; [animatingView setAlpha:0]; [animatingView setCenter:CGPointMake(animatingView.center.x+50.0, animatingView.center.y+50.0)]; [UIView commitAnimations]; }
  • 14. Anima1on  with  Blocks   - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [UIView animateWithDuration:5.0 animations:^{ [animatingView setAlpha:0]; [animatingView setCenter:CGPointMake(animatingView.center.x+50.0, animatingView.center.y+50.0)]; } completion:^(BOOL finished) { [animatingView removeFromSuperview]; } ]; }
  • 15. Block  Advantages   •  Simplifies  code   •  Keeps  code  together   •  Apple  official  recommenda1on  (use  block   methods  if  possible)  
  • 16. Recap   •  Selectors   –  Invoke  a  method  but  you  don’t  the  name  of  the   method  at  compile  1me   •  Delegates   –  Great  when  there  is  need  for  several  call  back   methods   •  Blocks   –  Only  need  for  one  anonymous  method  with   access  of  local  variables,  blocks  are  great