Objective-C Protocol and Delegates- raywenderlich.com

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 ม.ค. 2025
  • Even though we have ARC, understanding memory management is critical to writing effectives. This video covers the process.
    ----
    About www.raywenderlich.com:
    raywenderlich.com is a website focused on developing high quality programming tutorials. Our goal is to take the coolest and most challenging topics and make them easy for everyone to learn - so we can all make amazing apps.
    We are also focused on developing a strong community. Our goal is to help each other reach our dreams through friendship and cooperation. As you can see below, a bunch of us have joined forces to make this happen: authors, editors, subject matter experts, app reviewers, and most importantly our amazing readers!
    ----
    About Protocols:
    developer.appl...
    In the real world, people on official business are often required to follow strict procedures when dealing with certain situations. Law enforcement officials, for example, are required to “follow protocol” when making enquiries or collecting evidence.
    In the world of object-oriented programming, it’s important to be able to define a set of behavior that is expected of an object in a given situation. As an example, a table view expects to be able to communicate with a data source object in order to find out what it is required to display. This means that the data source must respond to a specific set of messages that the table view might send.
    The data source could be an instance of any class, such as a view controller (a subclass of NSViewController on OS X or UIViewController on iOS) or a dedicated data source class that perhaps just inherits from NSObject. In order for the table view to know whether an object is suitable as a data source, it’s important to be able to declare that the object implements the necessary methods.
    Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. This chapter describes the syntax to define a formal protocol, and explains how to mark a class interface as conforming to a protocol, which means that the class must implement the required methods.
    A class interface declares the methods and properties associated with that class. A protocol, by contrast, is used to declare methods and properties that are independent of any specific class.
    Protocols can include declarations for both instance methods and class methods, as well as properties.
    To make the view as reusable as possible, all decisions about the information should be left to another object, a data source. This means that multiple instances of the same view class could display different information just by communicating with different sources.
    Protocols Can Have Optional Methods
    By default, all methods declared in a protocol are required methods. This means that any class that conforms to the protocol must implement those methods.
    It’s also possible to specify optional methods in a protocol. These are methods that a class can implement only if it needs to.
    As an example, you might decide that the titles on the pie chart should be optional. If the data source object doesn’t implement the titleForSegmentAtIndex:, no titles should be shown in the view.
    Check that Optional Methods Are Implemented at Runtime
    If a method in a protocol is marked as optional, you must check whether an object implements that method before attempting to call it.
    Protocols Inherit from Other Protocols
    In the same way that an Objective-C class can inherit from a superclass, you can also specify that one protocol conforms to another.
    As an example, it’s best practice to define your protocols to conform to the NSObject protocol (some of the NSObject behavior is split from its class interface into a separate protocol; the NSObject class adopts the NSObject protocol).
    By indicating that your own protocol conforms to the NSObject protocol, you’re indicating that any object that adopts the custom protocol will also provide implementations for each of the NSObject protocol methods. Because you’re presumably using some subclass of NSObject, you don’t need to worry about providing your own implementations for these NSObject methods. The protocol adoption is useful, however, for situations like that described above.
    About Objective C (from Wikipedia)
    Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It was the main programming language used by Apple for the OS X and iOS operating systems, and their respective application programming interfaces (APIs) Cocoa and Cocoa Touch prior to the introduction of Swift.
    The programming language Objective-C was originally developed in the early 1980s. It was selected as the main language used by NeXT for its NeXTSTEP operating system, from which OS X and iOS are derived.[3] a

ความคิดเห็น • 22

  • @WestCoastAce27
    @WestCoastAce27 4 ปีที่แล้ว +9

    Good job. I wish you hadn’t deleted the Obj-C tutorials from your website. Not all of us have moved to Swift yet. Storage is cheap.

  • @judyliu7262
    @judyliu7262 4 ปีที่แล้ว +3

    Thank you! This is the only video that made me understand what a delegate is. It was very helpful!

  • @ssam_v9904
    @ssam_v9904 4 ปีที่แล้ว +2

    I agree with Mark. Having the objective c back on your website would be helpful. +1 to "not all of us have moved to Swift yet"

  • @MakesLoudNoise
    @MakesLoudNoise 7 ปีที่แล้ว +3

    Wow, this was extremely helpful in understanding delegates and protocols in objective-c. Working on the additional task you asked us to do now. Thanks again for these awesome video lectures! You explain things clearly, concisely and using an example helps soooooooo much!

  • @SivaPrasad-oz4qx
    @SivaPrasad-oz4qx 7 ปีที่แล้ว +1

    what is the use of class as reference data type in objective c can anyone explain

  • @attilaroy8694
    @attilaroy8694 6 ปีที่แล้ว +2

    The delegate property attribute is declared as strong. Can you please explain why it is not weak?

    • @angelchavezreyes372
      @angelchavezreyes372 5 ปีที่แล้ว

      The second best answer here says it must be "weak" for prevent a cycle. stackoverflow.com/questions/5210535/passing-data-between-view-controllers
      I'm confused about that as well... but I think the answer is right.

  • @patricklafontaine4313
    @patricklafontaine4313 4 ปีที่แล้ว

    Hi! thanks for the video! I have an issue with kind of delegate and just cant find anything! If you have a little time... :)

  • @benedictcontawe3355
    @benedictcontawe3355 3 ปีที่แล้ว

    Do you have source code on the challge I can't solve the challenge

  • @Bit-Z1
    @Bit-Z1 7 ปีที่แล้ว

    How would you implement this without using the AppDelegate class and a separate protocol (.h file) for the delegate? I've always avoided custom delegates but I think I should finally pick it up. I'm struggling to follow this properly however due to the way the video and the code is laid out.
    Could you perhaps do a more detailed video coding from scratch rather than modifying an existing program?

    • @KodecoDev
      @KodecoDev  7 ปีที่แล้ว +1

      You can define your protocol in any header file and then, you can import that header file into any class that is using it. You can learn more about protocols via. this tutorial over here (although it is somewhat out of date):
      www.raywenderlich.com/31814/objectively-speaking-2-a-crash-course-in-objective-c-for-ios-6

    • @Bit-Z1
      @Bit-Z1 7 ปีที่แล้ว

      Of course. Thanks!
      Do you guys have a series for advanced developers? I've been employed as an iOS dev for over a year but recently had to change jobs (company was a startup, sadly didn't make it). I recently had an interview and apparently I'm not quite where I should be after that year, so I'd like to develop myself further.

    • @KodecoDev
      @KodecoDev  7 ปีที่แล้ว

      In regards to your question about a course for advanced developers, you might find this new course on Advanced Swift 3 helpful: videos.raywenderlich.com/courses/70-advanced-swift-3/lessons/1
      We also have tons of other courses and screencasts for advanced developers on our site.

    • @Bit-Z1
      @Bit-Z1 7 ปีที่แล้ว

      Have you given up on Objective C? It's annoying that all the books being released and yourselves appear to have given up on it even though both languages are supported by Apple with no sign of Objective C being dropped. I used a lot of your tutorials during my undergrad and they were very helpful, but Objective C is my language of choice (and employment) for the time being, starting again with Swift isn't really something I'm looking to do just yet.
      I'd really love it if there was a way to separate your tutorials by language on the website.
      Thanks.

    • @KodecoDev
      @KodecoDev  7 ปีที่แล้ว +2

      Yes, we only cover Swift tutorials on our site these days. As a small team we don't have the bandwidth to make books and videos for both languages, and the vast majority of our customers prefer Swift tutorials these days. I do understand that some folks like yourself prefer to stay with Objective-C for now, but if/when you're ready for Swift we'll be there for you! :]

  • @kearacole6897
    @kearacole6897 6 ปีที่แล้ว

    thank you so much!

  • @sagarchandradas5831
    @sagarchandradas5831 4 ปีที่แล้ว

    Thanks.

  • @benedictcontawe3355
    @benedictcontawe3355 3 ปีที่แล้ว

    No type or protocol named 'FrecklesDelegate'
    No type or protocol named 'StephanieDelegate'

  • @arthurfelde5437
    @arthurfelde5437 6 ปีที่แล้ว +1

    You explained too fast, I couldn't realy follow

  • @AkiraRabelaisXO
    @AkiraRabelaisXO 6 ปีที่แล้ว

    "...and then you give the protocol that this protocol drives from..." useless af

  • @lqv3223
    @lqv3223 2 ปีที่แล้ว

    Thank you so much!