Can anyone give example of forward declaration in objective C for a normal class and not for category or protocol?
Asked Answered
P

1

9

Can anyone give example of forward declaration in objective C for a normal class and not for category or protocol?

Process answered 1/2, 2011 at 12:44 Comment(0)
C
9
/*
  using a forward declaration of NSDocument, there's no need
  for every source that encounters this header to include AppKit,
  allowing much faster compile times and reducing dependency
  changes for clients.

  of course, MONThang.m will need to include AppKit to use NSDocument
  - but the clients using MONThang do not need to import AppKit.
*/

@class NSDocument; // << the forward declaration

@interface MONThang : NSObject
{
    NSDocument * document;
}

@end
Circumnutate answered 1/2, 2011 at 12:48 Comment(4)
can you give it's example in case of custom class with it's usage in implementation.Process
but what difference does @class makes when we know that eventually we gonna import NSDocument in implementation?Process
because all the sources which must include MONThang.h will not be required to include AppKit when you use the forward. it's one simple key to fast compiles -- don't include huge libs where you don't need them, and don't force it on your clients because you'll end up upsetting them when they have to add AppKit (or one or several other huge libraries) to several of their source files. this is especially important when you're working with middle and large scale development, or distributing libraries to other developers.Circumnutate
that is, we only drag in all of AppKit (or any other lib) where we actually use it. using and regenerating 1GB prefix headers which include half of the system's libs is a terrible idea, and a terrible thing to force on users of your library.Circumnutate

© 2022 - 2024 — McMap. All rights reserved.