Match Card Game Lesson 5: Required Protocols and Developer Document Issues

I have spent a fair amount of time reviewing Lesson 5 multiple times have run into 3 issues which I would greatly appreciate added clarification on:

  1. Protocols:

I think of Protocols as a bridge that enables a “delegate class” to communicate with the another class that it knows nothing about (i.e. the “protocol class”); in the lesson the ViewController is delegate class and the UICollectionView is the protocol class.

If the above is correct (and, if it is not, please correct my above understanding as well) then please help me understand:

a. I understand that the “delegate class” must declare / define all the REQUIRED properties and methods of the protocol class but in reading the Apple Developer document I do not see nothing to distinguish between OPTION and REQUIRED properties and methods. Please explain how this can be done.

b. While I believe I understand Protocols (per the above) I do not understand what any of the following lines of code do and would appreciate a detailed explanation:

  1. Apple Development Documentation

I have – as Chris suggested – reviewed this document many times but I find it confusing in the following ways:

a. As there is a no diagram hierarchy it is very difficult to know when there is a need to use Protocols. Is there a better or an easier way to determine when their is a need to use Protocols other than tracing the inheritance of the sub classes that are being used to determine whether they “know of” / “can speak to” each other?

b. And this is big one, I find it very difficult and at times impossible to understand what is being explained in the Apple Developer document. I think – and please confirm or correct my understanding – that the programming process will be greatly were I am able to easily read and understand the Aple Developer Document. Please, what is the best and easiest way to master this skill.

Thx in advance.

a. I understand that the “delegate class” must declare / define all the REQUIRED properties and methods of the protocol class but in reading the Apple Developer document I do not see nothing to distinguish between OPTION and REQUIRED properties and methods. Please explain how this can be done.

One the page in the documentation for a protocol, it will say Required if it’s required. Like this (from UICollectionViewDataSource):

b. While I believe I understand Protocols (per the above) I do not understand what any of the following lines of code do and would appreciate a detailed explanation:

Those methods do pretty much what they say they do. They are UICollectionViewDelegate methods that:

  • return numberOfItemsInSection. The UICollectionView has no idea how many items it should display in a section and relies on its delegate to feed it that information when needed. This method does that by returning the count of cards in cardArray since there is only one section.
  • return cellForItemAt. When the UICollectionView needs a new cell (e.g., when scrolling to a previously offscreen section of the view), it asks its delegate to supply one by calling this method. cellForItemAt then creates a new cell, either by dequeueing a previously used one or by creating one from scratch if no previous cell is available, and then returns it to the UICollectionView.
  • perform some action when the user didSelectItemAt. Pretty self-explanatory. The user selects an item in the UICollectionView and it calls this method to do something in response.

a. As there is a no diagram hierarchy it is very difficult to know when there is a need to use Protocols. Is there a better or an easier way to determine when their is a need to use Protocols other than tracing the inheritance of the sub classes that are being used to determine whether they “know of” / “can speak to” each other?

Usually the documentation for a UIKit class will indicate when you need to use an object that implements a particular protocol. Like with UICollectionView and its delegate, which implements the UICollectionViewDelegate protocol:

b. And this is big one, I find it very difficult and at times impossible to understand what is being explained in the Apple Developer document. I think – and please confirm or correct my understanding – that the programming process will be greatly were I am able to easily read and understand the Aple Developer Document. Please, what is the best and easiest way to master this skill.

Well, Apple’s documentation isn’t great. It’s way too sparse at times. You’re better off just using it as a reference to look up method signatures and parameter types and whatnot until you get more familiar with it. I would definitely not try to learn how to program for Apple’s various OSes from reading the documentation.

@roosterboy, thank you for the fast and helpful response.

While I do not fully understand all the code I will play with it a bit more in the hope that it becomes clearer; still finding all the parameters in the methods difficult.

With that, one follow up. You noted that “…I would definitely try not to learn how to program for Apple’s vaious Oses from reading the documentation…” which is fine as I am using a combination of Apple’s Swoft guide, CodeWIthChris, forums, etc. but, that said, what would be your recommended best approach / method for learning.

Thx!

I neglected to mention the easiest way to find out what methods and properties are required for protocol conformance: just let the compiler tell you.

Say if you add conformance to UICollectionViewDataSource to your ViewController and try to build without supplying any of the required methods, you’ll get a FixIt from the compiler offering to add the protocol stubs. Let it do that and then just fill in the bodies of the methods.

As for best approach/method… whatever works for you. There are a number of good teachers out there. I tend to bounce back and forth among CodeWithChris, Sean Allen and Hacking with Swift as my mood suits me. They all teach the same things, just differ in their styles.

@roosterboy, again, thank you, much appreciated., especially the suggestions on other sites to visit which I will most definitely take a look at…one follow up, how long did it take to become comfortable / proficient at Swift using the sites that you referenced.

Thx!

Heh, I’ll let you know once I feel I am proficient! :wink:

@roosterboy, fair enough…let me try it another way, how long did it take until you no longer felt frustrated??

I have progressed further in my efforts and would appreciate some additional assistance as follows:

  1. I have written some additional code to access each contacts phone number. The code for the 2 contact in the array of contacts appears as follows:

  1. While I have been successful at extracting the label and value for each CNPhoneNumber property (i.e. home / mobile / etc.) as demonstrated by the below output I am at a loss as to how to extract / fetch the label string (i.e. home or work) and the label value (i.e. the actual phone number).

Screen Shot 2020-04-04 at 4.08.41 PM

  1. Would appreciate any and all help in as to how to extract / fetch the label string (i.e. Home or Work) and the label value (i.e. the actual phone number).

Thanks!