Nilesh Tupe
Nilesh Tupe

Reputation: 7803

Unable to include a class into another in IOS

I wish to create object of one class into another I have 2 classes

  1. MyviewControler
  2. Checkout

I want to import Checkout into MyviewController

#import "Checkout.h"

@interface MyViewController : UIViewController <UIImagePickerControllerDelegate>
{
    Checkout *checkout;
}

@property (nonatomic) Checkout *checkout;

@end

It is giving me error "Unknown type name checkout"

Upvotes: 2

Views: 2119

Answers (4)

Caleb
Caleb

Reputation: 124997

If the error really is as you say:

Unknown type name checkout

(note the small 'c') then the problem is that you're using checkout as a type name instead of Checkout somewhere in your code.

Upvotes: 4

mayuur
mayuur

Reputation: 4746

import the file in MyViewController.m file too.

  //in .m file
  #import "Checkout.h"

  @class Checkout;   //this was missing

Also, give the property like this.

  @property(nonatomic, retain) Checkout* checkout

and synthesize it in .m file

Upvotes: 1

Bourne
Bourne

Reputation: 10312

In MyViewController.h, before @interface add:

@class Checkout; 

In MyViewController.m, add:

#import "Checkout.h" 

Upvotes: 7

justin
justin

Reputation: 104698

you probably have a dependency cycle. use a forward declaration, which tells the compiler there is a class with that name without needing to see its declaration:

@class Checkout; // << the forward declaration

@interface MyViewController : UIViewController <UIImagePickerControllerDelegate>
{
    Checkout *checkout;
}

@property (nonatomic) Checkout *checkout;

@end

// MyViewController.m
...
#import "Checkout.h"

forward declarations are preferred in the majority of cases. the exception to this is when there is a physical dependency (e.g. the superclass' declaration should precede the subclass'). forward declarations are good because they significantly reduce the build times and the complexity of include graphs and dependency.

good luck

Upvotes: 4

Related Questions