• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newbie Objective-C question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie Objective-C question


  • Subject: Re: Newbie Objective-C question
  • From: Mike Ferris <email@hidden>
  • Date: Wed, 8 Jan 2003 13:12:07 -0800

That's a "category".

Categories are used to add methods to an existing class. In practice they are used for three things (two safe things and one not so safe).

- You can use them for your own classes simply to enable you to split a class definition across multiple implementation files.
- You can use them to add new methods to an existing class. For example, you could put a -stringByEncryptingWithRot13 method on NSString witgh a category and then all NSStrings would be able to respond to that method.
- You can "replace" a method from the main class.

That last one is the unsafe one. If the main class and a category implement the same method, the category method will "win" and will mask the implementation in the main class. If multiple categories implement the same method, the behavior is undefined (one of the categories will win, but which one is undefined). If you mask a method in a class using a category, there's no (easy) way to call the implementation you're masking (so this is not like overriding a method in a subclass).

Actually, in the next release of MOKit, I have implemented a way of replacing a method in a class while retaining the ability to message the original implementation later. This enables a more override-like behavior for replacing methods with a category. However, note that this is still a fairly sneaky thing to do and you should be sure what you're doing and that there's not a better, less sneaky, way to do it.

If you want to take a peek, you can grab the latest code for MOKit (instructions at http://mokit.sourceforge.net). The 2.7 release is not done yet, but the CVS repository at SourceForge has the current development source for 2.7.

Mike


Begin forwarded message:

From: Elias Freider <email@hidden>
Date: Wed Jan 8, 2003 4:28:54 AM US/Pacific
To: email@hidden
Subject: Newbie Objective-C question

@interface alreadyExistingClass(What_is_this)
@end

@implementation alreadyExistingClass(What_is_this)
@end

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

If I have understood things correctly (judging only from code I've
seen), What_is_this, is just some sort of keyword to say what "part" of
a class I am extending, and hasn't much to do with the finished app. I
would be happy if somebody told me why to use these and what they do.
Documentation links are appreciated.

Some links to documentation on "Delegates" and "Protocols" would also
be very useful as I use these without knowing _exactly_ what they are...
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Newbie Objective-C question (From: Elias Freider <email@hidden>)

  • Prev by Date: WebCore
  • Next by Date: Re: Notes about WebCore
  • Previous by thread: Newbie Objective-C question
  • Next by thread: Re: Newbie Objective-C question
  • Index(es):
    • Date
    • Thread