我是objective-c的新手,想知道导入我在课堂上使用的一些外部标题的最佳实践.
我应该将#import"classB.h"存储在我自己的类.h文件中还是存储在.m文件中?
有什么不同?
谢谢!
在我需要使用Objective C类来扩展Swift类的情况下.我做了如下的事情:
在"SomeClass.swift"中:
class SomeClass: NSObject {
}
Run Code Online (Sandbox Code Playgroud)
在"SomeClass + Extension.h"中:
#import "Project-Swift.h"
@interface SomeClass (Extension)
-(void)someMethod();
@end
Run Code Online (Sandbox Code Playgroud)
这运作良好.如果我尝试在Objective C代码中使用SomeClass扩展,那很好.
问题是,如果我想someMethod()在另一个Swift类中使用,我需要将SomeClass+Extension.h文件放入我的ObjC-BridgingHeader.h文件中.
但这样做会导致循环依赖,因为SomeClass+Extension.h也是进口Project-Swift.h.
有没有人有一个很好的方法来解决这个问题?
请注意,简单地向前声明类别标题中的类将不起作用,因为类别不能使用前向声明为它自己的实现,因为:
@class SomeClass没有导入Project-Swift.h会给出编译错误.
这样做有什么不同吗?
@class MyViewController;
Run Code Online (Sandbox Code Playgroud)
而不是将.h正常导入到appdelegate.h中
#import "MyViewController.h"
Run Code Online (Sandbox Code Playgroud)
我最近看到一些使用@class方式的例子,并想知道是否有任何差异.
谢谢.
我最近看过一个教程,说话者说如果你使用它没有任何区别:
#import "Class.h"
Run Code Online (Sandbox Code Playgroud)
要么:
@class Class;
Run Code Online (Sandbox Code Playgroud)
我不得不说,我的应用程序以这种或那种方式工作.但是必须有区别吗?以为我问你们.
提前致谢!
我对一些我认为简单的事情有一个非常奇怪的错误.
#import <Foundation/Foundation.h>
#import "ViewController.h"
#import "GameObject.h"
@interface GameController : NSObject
@property (strong) GLKBaseEffect * effect;
@property (strong) NSMutableArray * gameObjects;
@property (strong) NSMutableArray * objectsToRemove;
@property (strong) NSMutableArray * objectsToAdd;
+ (GameController *) sharedGameController;
- (void) tick:(float)dt;
- (void) initializeGame: (ViewController*) viewcontroller;//ERROR: EXPECTED A TYPE
- (void) createObject:(Class) objecttype atPoint:(CGPoint)position;
- (void) deleteObject:(GameObject*) object atPoint:(CGPoint)position;
- (void) manageObjects;
@end
Run Code Online (Sandbox Code Playgroud)
为什么会问'ViewController'是否是一个类型?这是我正确实施的课程.它也被导入了.
编辑*
如果它有帮助,这是ViewController.m类.
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[GameController sharedGameController] initializeGame:self];
}
@end
Run Code Online (Sandbox Code Playgroud)
编辑2 ** …
import objective-c circular-dependency header-files forward-declaration
我正在为iphone制作Cocos2d游戏,我有我的主要游戏模式Game,它继承自CCLayer.
我正在尝试制作另一种游戏模式,MathGame它继承自Game,但是当我尝试编译时,我得到了以下错误MathGame.h:
尝试使用前瞻性"游戏"作为"MathGame"的超类
即使实现和接口为MathGame空,我也会收到错误.只有当我尝试包含MathGame.h在另一个文件中时才会发生这种情况.
这是Game类的代码:
// Game.h
#import "cocos2d.h"
#import <GameKit/GameKit.h>
#import "SplashScreenLayer.h"
@interface Game : CCLayer
// A bunch of stuff
@end
Run Code Online (Sandbox Code Playgroud)
新游戏类型:
// MathGame.h
#import "Game.h"
@interface MathGame : Game
@end
Run Code Online (Sandbox Code Playgroud)
主菜单包括:
// SplashScreen.h
#import "cocos2d.h"
#import "Game.h"
#import "MathGame.h"
#import "HowToPlayLayer.h"
#import "AboutLayer.h"
@interface SplashScreenLayer : CCLayer
// A bunch of stuff
@end
Run Code Online (Sandbox Code Playgroud)
我在网上找不到任何有用的东西.有任何想法吗?
我有以下代码:
#import <Foundation/Foundation.h>
#import "ServerRequest.h" // works even though this line is included
#import "ServerResponseRecord.h"
@protocol ServerRequestDelegate<NSObject>
-(void)request:(id)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(id)request gotError:(NSError*)error;
@end
Run Code Online (Sandbox Code Playgroud)
它编译并运行良好.但是,如果我用以下方法替换方法声明:
-(void)request:(ServerRequest*)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(ServerRequest*)request gotError:(NSError*)error;
Run Code Online (Sandbox Code Playgroud)
我在'ServerRequest'之前得到意外的语法错误"error:expected')'.我认为这可能是一个问题的唯一原因是ServerRequestDelegate.h和ServerRequest.h #import彼此.但是,我不明白为什么代码适用于带有(id)请求的#import行.我也不明白为什么这是一个语法错误.
有人能提供一个很好的解释吗?
我已经创建了一个名为"Category"的类
在我看到的一些在线示例中 - 他们实现了#import"Category.h"
在某些示例中 - 在@interface新类之前预先声明为@category的类
所以我很困惑=>何时使用#import&=>何时使用@class?
当我使用以下代码编译时,没有错误:
@class RootViewController;
//#import "RootViewController.h"
Run Code Online (Sandbox Code Playgroud)
当我使用以下代码编译时,我收到一个错误:
//@class RootViewController;
#import "RootViewController.h"
Run Code Online (Sandbox Code Playgroud)
"错误:'RootViewController'之前的预期说明符 - 限定符列表"
我不明白两者之间的区别是什么,因为我在类似的类中使用了#import并且编译时没有错误!
在Objective-c中,当我们按照约定将一个类的对象用于另一个类时,我们应该在.h文件中转发声明类,即@class classname;.并且应该在.m文件中导入头文件,即#import "header.h".但是如果我们在.h中导入头文件,那么我们不必再在.m文件中导入它.那么这个大会背后的原因是什么?哪种方式有效?
objective-c ×9
import ×4
ios ×2
xcode ×2
build-time ×1
class ×1
gcc ×1
header-files ×1
inheritance ×1
ios4 ×1
iphone ×1
swift ×1