Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-04-2010, 01:19 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 8
mongoose23 is on a distinguished road
Default checking protocol types

Is there a way to check whether an object implements a certain protocol?

You can't seem to pass object references around by "protocol" in Obj-C, the way you can in Java.

That is, suppose I have a protocol:

@protocol TestProtocol

- (void)method1;
@end

I can't just go and create object points like:

TestProtocol *test;

can I? (I would like to do something like this)..

What's the best way to do multiple inheritance in ObjC?
mongoose23 is offline   Reply With Quote
Old 10-04-2010, 11:36 AM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

You can "type" the id type to create a type that the compiler will understand implements your protocol, like this:
Code:
@protocol TestProtocol
- (void)method1;
@end

...

id<TestProtocol> anInstanceThatImplementsTestProtocol = ...;
You can use this same technique to type method parameters (the compiler will enforce this with type checking):
Code:
- (void) myMethod:(id<TestProtocol>)param
{
    // variables passed as 'param' must implement TestProtocol
}
In general, you can take any id type object, and ask if it implements a protocol:
Code:
id someUnTypedObject = ...;

if ( [someUnTypedObject conformsToProtocol:@protocol(TestProtocol)] )
{
    // yes it does, can call protocol methods on it
    [someUnTypedObject someProtocolMethod];
}
Finally, since a protocol can have @optional methods, you can check if an instance implements an optional method:
Code:
id someUnTypedObject = ...;

if ( [someUnTypedObject conformsToProtocol:@protocol(TestProtocol)] )
{
    // it conforms to the protocol
    if ( [someUnTypedObject respondsToSelector:@selector(someOptionalMethod)] )
    {
        // safe to send 'someOptionalMethod'
        [someUnTypedObject someOptionalMethod];
    }
}
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Reply

Bookmarks

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 On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 476
15 members and 461 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0