Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 2 > Newsgroup comp.lang.objective-c

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-24-2009, 09:26 AM
Angelo Chen
Guest
 
Posts: n/a
Default Protocol newbie

Hi,

I have a protocol:

@protocol Copying
- (void) update : (double) value;
@end

and following class implements the protocol

@interface CopyingStub : NSObject <Copying>{
}
- (void) updateProgress : (double) value;
@end

Now, I'd like another class's method to take a protocol Copying as
method argument, following is what I did, but not acceptable by
compiler, any idea what I did wrong there? Thanks.


@interface Replicator : NSObject {
}
- (void) copyFile : (NSString*)sf to: (NSString*)df using :
id<Copying> *ui;

@end
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 11-24-2009, 11:19 AM
bandejapaisa
Guest
 
Posts: n/a
Default Re: Protocol newbie

I'm new to Ojbective-C too, so i hope this isn't blind leading the
blind - as i don't have a compiler in front of me.

Try this:

- (void) copyFileNSString*)sf toNSString*)df usingid<Copying>)
ui;

'id' is already a pointer to a data structure, so i don't think you
need the derefencing operator with it i.e you don't need to put the *.
(I think that is the correct explanation - or something like that)


Reply With Quote
  #3 (permalink)  
Old 11-25-2009, 03:40 PM
Tom Harrington
Guest
 
Posts: n/a
Default Re: Protocol newbie

In article
<cebc7480-fb70-4cbc-ba5a-9f5e3ff84526@z10g2000prh.googlegroups.com>,
Angelo Chen <angelochen960@yahoo.com.hk> wrote:

> Hi,
>
> I have a protocol:
>
> @protocol Copying
> - (void) update : (double) value;
> @end
>
> and following class implements the protocol
>
> @interface CopyingStub : NSObject <Copying>{
> }
> - (void) updateProgress : (double) value;
> @end
>
> Now, I'd like another class's method to take a protocol Copying as
> method argument, following is what I did, but not acceptable by
> compiler, any idea what I did wrong there? Thanks.


When the compiler complains, it helps if you say what it complained
about. Those error messages almost always have useful clues.

> @interface Replicator : NSObject {
> }
> - (void) copyFile : (NSString*)sf to: (NSString*)df using :
> id<Copying> *ui;


Since "id" is already a pointer to an object, you don't need the "*" on
the last argument:

- (void)copyFileNSString*)sf toNSString*)df usingid<Copying>)ui;

--
Tom "Tom" Harrington
Independent Mac OS X developer since 2002
http://www.atomicbird.com/
Reply With Quote
  #4 (permalink)  
Old 11-26-2009, 09:52 AM
spikeysnack
Guest
 
Posts: n/a
Default Re: Protocol newbie

On Nov 24, 2:26*am, Angelo Chen <angelochen...@yahoo.com.hk> wrote:
> Hi,
>
> I have a protocol:
>
> @protocol Copying
> - (void) update : (double) value;
> @end
>
> and following class implements the protocol
>
> @interface CopyingStub : NSObject <Copying>{}
>
> - (void) updateProgress : (double) value;
> @end
>
> Now, I'd like another class's method to take a protocol Copying as
> method argument, following is what I did, but not acceptable by
> compiler, any idea what I did wrong there? Thanks.
>
> @interface Replicator : NSObject {}
>
> - (void) copyFile : (NSString*)sf to: (NSString*)df using :
> id<Copying> *ui;
>
> @end


Let me adjust your thinking about protocols if I may:
Protocols are not implemented, only methods are.
Protocols are declared as a set of methods that must be conformed to
by
instances of classes implementing the methods defined by the formal
protocol.

Protocols were designed for grouping similar classes that are used the
same way but differ enough to not be subclasses of a common superclass
(other than NSObject ).
-----------------------------------------------------------------------------
//Copying.h
@protocol Copying
- (void) update : (double) value; //defines mandatory methods
- CopyingMethods;
@end

//CopyingStub.h
#import "Copying.h"
@interface CopyingStub : NSObject <Copying>{}
// [don't need to re-declare methods of the Copying protocol]
-instanceMethod; // regular method declare
@end

//CopyingStub.m
#import "CopyingStub.h"
@implementation CopyingStub
- (void) update: (double) value { //code//} // implementations
- CopyingMethods{ //code// }; // Must implement ALL methods in formal
protocol

- instanceMethod{ //code// }; //regular method code
@end

-----------------------------------------------------------------------------


A formal protocol defines an interface that must be conformed to. That
is,
the class must implement exactly the methods stated in the protocol.
Your example
differs in the method name from (void)updatedouble) to (void)
updateProgressdouble) so a test for conformity to the Copying
protocol would fail.
The compiler checks at compile time if your class implements the
methods of the protocol and issues a warning if not but compiles
anyway. During runtime, if there is doubt as to the protocol
adherence of a class passed into a method as a prameter you can check
it with the following idiom:

double version = 2.0
if ( [receiver conformsTo:@protocol(Copying)] )
[receiver update:version];

further, you may define a conforming type

id <Copying> ui;
now only instances of classes conforming to the Copying protocol may
be assigned to ui.

- (void) copyFile : (NSString*)sf to: (NSString*)df using id
<Copying>) ui;
aught to work.

You may be over thinking this here: are you planning on creating a
great number of different kinds of classes that all use the (void)
updatedouble) method? If not you could probably dispense with the
protocol stuff. Frameworks use formal protocols because of the great
number of objects' deep interaction with each other
needs to be corralled into a usable consistent set of usage methods so
that classes do not need to know as much about each classes
implementations to interact. In the Days of K&R C, giant codebooks of
rules for writing interfaces were published by every compiler maker
and every dev department and still every piece of code made by a was
different. Defining interface protocols in the language was an attempt
to impose order on that chaos. It was a good idea and is there in some
form in most modern languages today. But its not always a good idea.It
is my opinion that the unfortunate complexity of the Foundation object
archiving system was a casualty of slavishly conforming to the Coding
protocols.
Reply With Quote
 
Reply

Popular Tags in the Forum
newbie, protocol

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: external file help nospam@HOWLES.COM (Howard Schreier Newsgroup comp.soft-sys.sas 0 08-29-2007 12:11 AM
Re: external file help data _null_; Newsgroup comp.soft-sys.sas 0 08-28-2007 03:35 PM
Re: external file help Gerhard Hellriegel Newsgroup comp.soft-sys.sas 0 08-28-2007 12:49 PM
external file help boulmo Newsgroup comp.soft-sys.sas 0 08-28-2007 08:12 AM
Re: SMTP Authentication Protocol in SAS F. J. Kelley Newsgroup comp.soft-sys.sas 0 03-22-2006 02:28 PM



All times are GMT. The time now is 04:27 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.