Return to Video

What is a Delegate - Intro to iOS App Development with Swift

  • 0:00 - 0:04
    A common answer we heard from our students, is that the word delegate,
  • 0:04 - 0:08
    makes them think of the relationship between a boss and an employee.
  • 0:08 - 0:11
    Perhaps you've been in a situation where your boss has delegated some work to
  • 0:11 - 0:12
    you, or given you some work.
  • 0:12 - 0:15
    The idea of delegating work is similar in iOS,
  • 0:15 - 0:20
    here you can think of AV audio recorder like boss.
  • 0:20 - 0:21
    It knows how to record audio.
  • 0:21 - 0:25
    It also knows when the recording is done and is ready for playback.
  • 0:25 - 0:28
    In fact, if you look deep enough in the code for AV audio recorder you'll find
  • 0:28 - 0:32
    a function called audio recorder did finish recording.
  • 0:32 - 0:35
    This is the function that gets called when the recording has finished.
  • 0:35 - 0:39
    Now honestly, this seems like a method we could use in our class.
  • 0:39 - 0:41
    Record sounds view controller.
  • 0:41 - 0:45
    Remember the situation where our user would record a five minute long
  • 0:45 - 0:47
    conversation with a friend on their app?
  • 0:47 - 0:49
    If somehow we had access to this method,
  • 0:49 - 0:52
    it could tell us exactly when the audio was recorded successfully, so
  • 0:52 - 0:56
    then we could do things with that audio, for instance, playback.
  • 0:56 - 1:01
    So then, why doesn't av audio recorder just tell our class that hey.
  • 1:01 - 1:02
    Audio has been recorded successfully.
  • 1:02 - 1:07
    Well, one reason for that is that we wrote our class today.
  • 1:07 - 1:11
    And this class, AVAudioRecorder, was written by an Apple engineer years ago.
  • 1:11 - 1:13
    At the time they wrote this class,
  • 1:13 - 1:16
    they had no idea we would be writing our class.
  • 1:16 - 1:18
    And that we would call it record sounds view controller.
  • 1:18 - 1:20
    One way Apple decided to overcome this problem,
  • 1:20 - 1:25
    was by delegating the implementation of methods like this, to classes like ours.
  • 1:25 - 1:30
    Our class record sounds view controller can take advantage of this method,
  • 1:30 - 1:33
    if we become a delegate of AV audio recorder.
  • 1:33 - 1:35
    So to make this boss, employee, or
  • 1:35 - 1:39
    delegate relationship work in our code, we have to do two things.
  • 1:39 - 1:43
    The first thing we have to do, is to change the definition of record sounds view
  • 1:43 - 1:47
    controller by adding this thing av audio recorder delegate to it.
  • 1:47 - 1:50
    The second thing we have to do is, somewhere in our class,
  • 1:50 - 1:54
    we have to write this line of code: audio recorder dot delegate equal to self.
  • 1:54 - 1:57
    Self here means record sounds view controller.
  • 1:57 - 2:01
    And what this statement is saying is that RecordSoundsViewController is now
  • 2:01 - 2:03
    the delegate of audioRecorder.
  • 2:03 - 2:07
    After we have done these two things, we can then implement the function
  • 2:07 - 2:10
    audioRecorderDidFinishRecording inside our class.
  • 2:10 - 2:13
    So in the next video, we will add these two things to our code.
Cím:
What is a Delegate - Intro to iOS App Development with Swift
Leírás:

more » « less
Video Language:
English
Team:
Udacity
Projekt:
UD585 - Intro to iOS App Development with Swift
Duration:
02:14

English subtitles

Felülvizsgálatok Compare revisions