iosxcodensoperation

Issues with including header files


In my application I have several classes, that cross-over with each other. I need to connect this classes and create properties of them, but some classes are not visible for another, I suppose that main issue here is in including header files.

class MyOperationQueue

#import <Foundation/Foundation.h>
#import "ContentTableView.h"
//#import "CustomTableViewCell.h"
//#import "ObjectForTableCell.h"

@interface MyOperationQueue : NSOperation

@property(assign, nonatomic) BOOL isCancelled;
@property(strong, nonatomic) ContentTableView* tableView; //unknown type name

class ObjectForTableCell

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "MyOperationQueue.h"

@interface ObjectForTableCell : NSObject

@property (strong, nonatomic) MyOperationQueue* currentQueue;//unknown type name

class ContentTableView - here I don't have warnings

#import <UIKit/UIKit.h>
#import "CustomTableViewCell.h"
#import "Protocol.h"
#import "MyOperationQueue.h"
#import "ObjectForTableCell.h"
#import "ImageViewController.h"

@interface ContentTableView : UITableViewController<CellDelegate, NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate>

Solution

  • Stick a @class MyOperationQueue; above

    @interface ObjectForTableCell : NSObject
    

    to make it:

    @class MyOperationQueue;
    
    @interface ObjectForTableCell : NSObject
    

    Here is an excerpt from official documentation here:

    Referring to Other Classes

    An interface file declares a class and, by importing its superclass, implicitly contains declarations for all inherited classes, from NSObject on down through its superclass. If the interface mentions classes not in this hierarchy, it must import them explicitly or declare them with the @class directive:

    @class Rectangle, Circle;
    

    This directive simply informs the compiler that “Rectangle” and “Circle” are class names. It doesn’t import their interface files. An interface file mentions class names when it statically types instance variables, return values, and arguments. For example, this declaration mentions the NSColor class.

    Since declarations like this simply use the class name as a type and don’t depend on any details of the class interface (its methods and instance variables), the @class directive gives the compiler sufficient forewarning of what to expect. However, where the interface to a class is actually used (instances created, messages sent), the class interface must be imported. Typically, an interface file uses @class to declare classes, and the corresponding implementation file imports their interfaces (since it will need to create instances of those classes or send them messages).

    The @class directive minimizes the amount of code seen by the compiler and linker, and is therefore the simplest way to give a forward declaration of a class name. Being simple, it avoids potential problems that may come with importing files that import still other files. For example, if one class declares a statically typed instance variable of another class, and their two interface files import each other, neither class may compile correctly.

    There is also an excellent article (by NSHipster) on various other compiler directives here. I recommend reading it, for this knowledge will likely serve you well in the future.