Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 671
Prabh Avatar asked Feb 01 '11 12:02

Prabh


People also ask

When to use forward declaration objective c?

Forward declarations are often used in header files also to avoid importing the header of another class, which would import all the declarations present in the latter (classes, protocols, categories, types and constants) which, in turn, would also spread in all the files that import the header that includes it.

Is a forward declaration Objective C?

In Objective-C, classes and protocols can be forward-declared like this: @class MyClass; @protocol MyProtocol; In Objective-C, classes and protocols can be forward-declared if you only need to use them as part of an object pointer type, e.g. MyClass * or id<MyProtocol>.

What is forward declaration in C++?

Forward Declaration refers to the beforehand declaration of the syntax or signature of an identifier, variable, function, class, etc. prior to its usage (done later in the program). Example: In C++, Forward declarations are usually used for Classes.

What is object Objective C protocol?

Objective-C Protocols. Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. Protocols are implemented in the classes conforming to the protocol.

Why does the compiler throw this error when Class B is declared?

Explanation: Here the compiler throws this error because, in class B, the object of class A is being used, which has no declaration till that line. Hence compiler couldn’t find class A.

What is a protocol in Java with example?

A simple example would be a network URL handling class, it will have a protocol with methods like processCompleted delegate method that intimates the calling class once the network URL fetching operation is over. A syntax of protocol is shown below.


1 Answers

/*
  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
like image 197
justin Avatar answered Oct 31 '22 06:10

justin