Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
September 20, 2018, 12:30:30 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Uncertain on catagories & protocols + exercise 11.3  (Read 1734 times)
seerex
Full Member
***
Posts: 177


Email
« on: September 03, 2011, 01:59:11 AM »

Hello,

I can't seem to find the informal protocol named NSComparisonMethods. I should mention i am not 100% clear on protocols and categories, so i'll do a short re-write to see if i got em right, so please correct me if i'm wrong:

@Catagory - Is an extension of a class. If the category is made in a different file, the name extension is usually the class in which conforms to the category: myClassAndCatagory and that is pretty much it. The category has access to all instance variables, but cannot declare new ones. If that is needed, subclassing is better. So in short, category = an extension of a class, which could just be achieved by defining the methods in the class itself, but this aids in readability and scatters the code out on several files, under it's own name for better reference.

Protocol - Is a collection of methods, and unlike the category, protocols are not pre-associated with any class. It is made as a stand-alone file, and if a class wishes to conform / adopt the protocol, they do so by using <protocolName> after the parent class name in the interface file of the class. A protocol also does NOT define the methods / implements them, it only declares them. Thus, you must declare them in the class that conforms to that protocol. They are usually well documented to draw the purpose of these methods, and often written more dynamically to ensure being used well by others. If you needed a protocol associated with only 1 class, a category might be better.

Informal protocols - This is the part where the exercise really throws me off. First, i thought informal protocols was being pushed out of the language, and replaced by the @optional directive in a normal protocol? why then use it all?

But my understanding of informal protocols is that they are pretty much the same as a normal protocol, if that protocol declared all the methods in the @optional directive, right?

But some points i really don't understand.
1 - Which files do you have to import for a protocol to work? none?
2 - Why can't it find the NSComparisonMethods? Do i have to create the protocol myself?
3 - am i right in assuming you make categories in a new file, write the class name, put category name in ( ) and thats it? you don't include anything in the original file in which the category belongs to? or do you make (categoryName) in that interface / implementation file as well?

i know it's a long post, but i'm kinda confused Smiley thanks on advance
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
dharr19
Full Member
***
Posts: 175


Email
« Reply #1 on: September 03, 2011, 11:16:43 AM »

Hi Seerex,

This is as far as I have gotten with protocols.  I hope it helps.  

First I created a protocol header file called myProtocol.h and put my protocol definition in it.

Code: (Objective-C)
// this is my protocol header file 
@protocol someProtocol <NSObject>

-(void) print;

@end

then I created my header and implementation files for my class called protocols. Then I imported my protocol header file "myProtocol.h"
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "myProtocol.h"

@interface protocols : NSObject <someProtocols>

@end

Then I defined the print method I declared in my protocol and put it in my protocol implementation file.
Code: (Objective-C)
#import "protocols.h"

@implementation protocols
-(void) print{
    NSLog(@"Object-C blogging is cool!");
}

@end

Here is the main body of my program

Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "protocols.h"
int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    protocols *p = [[protocols alloc] init];
    
    [p print];
    
    [p release];     
    [pool drain];   
    return 0;
}

output

2011-09-03 14:12:14.795 protocols[558:707] Object-C blogging is cool!

Hope this helps
Cheers
david
« Last Edit: February 09, 2012, 01:39:57 PM by dharr19 » Logged
seerex
Full Member
***
Posts: 177


Email
« Reply #2 on: September 04, 2011, 01:20:08 AM »

Alright i Think i Got it. Then it seems Like i Got the protocols properly understood now. Thanks for the example mate!
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.