开发者

How can you use circular #import to strongly typed objects in objective-c

开发者 https://www.devze.com 2022-12-19 05:28 出处:网络
How can you use circular #import to strongly typed objects in objective-c I have an audio controller class, a menu class and a gameview class.

How can you use circular #import to strongly typed objects in objective-c

I have an audio controller class, a menu class and a gameview class.

The application delegate sets up these classes and assigns a pointers so:

the menu class is awar开发者_Go百科e of the audio and gameview class the gameview class has a reference to the audio and menu class

I am using NSObject for the reference to the gameview class from the menu class. This is because the menu class has a reference to the gameview class and has a #import gameview.h declaration. The code won't compile with circular #import

Suggestions please :) ?

@interface MenuViewController : UIViewController {

    NSObject *gameref; // weak type here to avoid include of gameview above
    AudioController *audioref;

}

and...

#import "AudioController.h"
#import "MenuViewController.h"

    @interface GameViewController : UIViewController {

        MenuViewController *menuref;
        AudioController *audioref;

    }


Fisrt, in your .h file use

@class GameViewController, AudioController

and

@class AudioController, MenuViewController

in your .m file use

#import "GameViewController"
#import "AudioController"

and

#import "AudioController.h"
#import "MenuViewController.h"

There is no 'circular reference' problem. Second, using NSObject instead of the actual classname isn't in any way a weak reference. Also, if you mean #include say #include. If you mean #import, say #import.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号