[英]Implicit conversion of a non-Objective-C pointer type 'int *' to 'Bird *' is disallowed with ARC
Bird, GameWorld and GameScene are three custom classes in my project. Bird,GameWorld和GameScene是我项目中的三个自定义类。
I've an object of type Bird(as a property) in the class Gameworld. 我在Gameworld类中有一个类型为Bird(作为属性)的对象。 I've objects of type Bird and Gameworld in the class GameScene.
我在GameScene类中具有Bird和Gameworld类型的对象。 Now in Gamescene class, when I do:
现在在Gamecene课上,当我这样做时:
_bird = [_gameWorld bird];
an error is alerted: 错误提示:
Implicit conversion of a non-Objective-C pointer type 'int *' to 'Bird *' is disallowed with ARC
ARC不允许将非目标C指针类型'int *'隐式转换为'Bird *'
Why this is happening? 为什么会这样呢?
Edit: 编辑:
GameScene.h GameScene.h
#import <SpriteKit/SpriteKit.h>
#import "Bird.h"
#import "ScrollHandler.h"
#import "Pipe.h"
#import "GameWorld.h"
@interface GameScene : SKScene <SKPhysicsContactDelegate>{
int _midPointY;
float _gameHeight;
NSString *_gameName;
NSString *_getReady;
Bird *_myBird;
NSTimeInterval _dt;
float bottomScrollerHeight;
GameWorld *_myWorld;
}
@property (nonatomic) SKSpriteNode* backgroundImageNode;
@property (nonatomic) SKSpriteNode* greenBird;
@property (nonatomic) NSTimeInterval lastSpawnTimeInterval;
@property (nonatomic) NSTimeInterval lastUpdateTimeInterval;
@end
GameWorld.h 游戏世界
#import <Foundation/Foundation.h>
#import <SpriteKit/SpriteKit.h>
#import "Bird.h"
#import "ScrollHandler.h"
typedef NS_ENUM(NSInteger, GameState){
MENU,
READY,
RUNNING,
GAMEOVER,
HIGHSCORE
};
@interface GameWorld : NSObject <ScrollHandlerDelegate>{
float _runTime;
GameState _currentState;
}
@property (nonatomic, strong) ScrollHandler *scroller;
@property (nonatomic, strong) Bird *bird;
@property (nonatomic, assign) int midPointY;
@property (nonatomic, assign) int score;
@property (nonatomic, strong) SKSpriteNode *bg;
@property (nonatomic, strong) NSArray *birds;
@property (nonatomic, strong) SKSpriteNode *birdNode;
@end
GameScene.m GameScene.m
#import "GameScene.h"
#define UPWARD_PILLER @"Upward_Green_Pipe"
#define Downward_PILLER @"Downward_Green_Pipe"
static const uint32_t pillerCategory = 0x1 << 0;
static const uint32_t birdCategory = 0x1 << 1;
static const uint32_t grassCategory = 0x1 << 2;
static const float BG_VELOCITY = (TIME * 60);
static inline CGPoint CGPointAdd(const CGPoint a, const CGPoint b)
{
return CGPointMake(a.x + b.x, a.y + b.y);
}
static inline CGPoint CGPointMultiplyScalar(const CGPoint a, const CGFloat b)
{
return CGPointMake(a.x * b, a.y * b);
}
@implementation GameScene
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
self.anchorPoint = CGPointMake(0, 1);
self.yScale = -1;
//To detect collision detection
self.physicsWorld.contactDelegate = self;
_myWorld = [[GameWorld alloc] initWithMidPointY:_midPointY];
[self initGameObjects];
[self initAssets];
[self setCoinAnimation];
}
return self;
}
-(void) initGameObjects {
_myBird = [_myWorld bird]; //here is the problem
}
-(void) initAssets {
//initialize other assets
}
-(void) setCoinAnimation {
//initialize other assets
}
@end
Bird.h 鸟
#import "GameWorld.h"
@import UIKit;
@interface Bird : NSObject {
CGPoint _position;
CGPoint _velocity;
CGPoint _acceleration;
float _rotation;
float _originalY;
int _width;
int _height;
int _dieCount;
bool _isAlive;
}
@end
Note:I just found out that when I remove the import "Gameworld.h" from "Bird.h", the error disappears and it is working. 注意:我刚刚发现,当我从“ Bird.h”中删除导入“ Gameworld.h”时,该错误消失并且正在运行。 The import was made there accidentally and it is not needed.
导入是在那里意外完成的,因此不需要。 But I don't why it caused the error.
但是我不为什么会导致错误。
Import statements must be linear, but you had a loop. 导入语句必须是线性的,但是您有一个循环。
GameScene.m
-> GameScene.h
-> Bird.h
-> GameWorld.h
-> Bird.h
. GameScene.m
> GameScene.h
> Bird.h
> GameWorld.h
> Bird.h
。
Because of this, when GameWorld.h
had #import "Bird.h"
, the compiler saw that Bird.h
was already imported and didn't import it. 因此,当
GameWorld.h
具有#import "Bird.h"
,编译器会看到Bird.h
已被导入而未导入。 The problem was @interface Bird
had not been defined yet, so GameWorld
didn't have the proper interface for Bird
. 问题是
@interface Bird
尚未定义,因此GameWorld
没有Bird
的适当接口。
Well as you mentioned in the end, thats was the only issue. 就像您在最后提到的那样,那是唯一的问题。
So what was happening? 那到底是怎么回事?
When you imported Gameworld
to your Bird
interface file, in that, your Gameworld
itself had a variable named bird
which was of type Bird
. 当您将
Gameworld
导入Bird
界面文件时, Gameworld
本身就有一个名为bird
的变量,其类型为Bird
。 You then went ahead and defined the Bird
class in the subsequent lines. 然后,您继续进行操作,并在随后的几行中定义了
Bird
类。 Before that the compiler din't knew what the Bird
object is and it by default assumes it as int
. 在此之前,编译器不知道
Bird
对象是什么,并且默认情况下将其假定为int
。
If you any how want to to declare Gameworld
object in your Bird
class, then instead of importing it your interface, you just need to use forward declaration, ie 如果您要如何在
Bird
类中声明Gameworld
对象,那么无需将其导入接口,只需使用正向声明即可,即
@class Gameworld;
This tells the compiler that a Class do exist with the name Gameworld
. 这告诉一类不具有名称存在编译
Gameworld
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.