简体   繁体   English

ARC不允许将非目标C指针类型'int *'隐式转换为'Bird *'

[英]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.

相关问题 ARC不允许将非Object-C指针类型void *隐式转换为NSString * __ strong * - Implicit conversion of a non-Objective-C pointer type void* to NSString*__strong* is disallowed with ARC 从Objective-C指针到int *的隐式转换不允许使用ARC - Implicit Conversion from Objective-C Pointer to int * is Disallowed With ARC ARC禁止将非目标C指针类型&#39;const UInt8 *&#39;(aka&#39;const unsigned char *&#39;)强制转换为&#39;NSData *&#39; - Cast of a non-Objective-C pointer type 'const UInt8 *' (aka 'const unsigned char *') to 'NSData *' is disallowed with ARC 在目标C中进行比较 - ARC不允许将&#39;int&#39;隐式转换为&#39;id&#39; - Comparing in objective C - Implicit conversion of 'int' to 'id' is disallowed with ARC TPMultiLayoutViewController的ARC转换; ARC不允许将Objective-C指针隐式转换为“ const void *” - ARC conversion of TPMultiLayoutViewController; Implicit conversion of an Objective-C pointer to 'const void *' is disallowed with ARC 禁止使用arc将ios的间接指针隐式转换为目标c的id指针 - ios implicit conversion of an indirect pointer to an objective c pointer to id is disallowed with arc 错误:在NSTimer选择器中,ARC无法将Objective-C指针隐式转换为&#39;SEL _Nonnull&#39; - Error : Implicit conversion of an Objective-C pointer to 'SEL _Nonnull' is disallowed with ARC in Selector Of NSTimer 使用ARC-sqlite3不允许将Objective-C指针隐式转换为'void *' - implicit conversion of an Objective-C pointer to 'void *' is disallowed with ARC - sqlite3 禁止将iOS从&#39;int&#39;隐式转换为&#39;UIColor *&#39; - iOS Implicit conversion of 'int' to 'UIColor*' is disallowed with ARC ARC不允许将&#39;int&#39;隐式转换为&#39;NSArray *&#39; - Implicit conversion of 'int' to 'NSArray*' is disallowed with ARC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM