Objective-C Expected a type错误

刚学到Objective-C的复合,于是就写了一个关于英雄和怪物的简单模拟程序,目测写法没有任何的错,但就是出现一个Expected a type的错误,很坑,但后来发现,问题出现在了前边的头文件,虽然以后在写东西是老师说都是先继承一个类,再在子类中复合,所以不会出现这种错误,但是相信新手也会和我一样出现这种问题,

代码如下

首先是Hero.h文件

//
//  Hero.h
//  OC_sj2.1
//
//  Created by Mac on 15/8/4.
//  Copyright (c) 2015年 yue. All rights reserved.
//

#import 
//之前出现错误是用的import
//#import "Monster.h"
@class Monster;

@interface Hero : NSObject


@property(nonatomic,strong)NSString *name;//英雄名字
@property(nonatomic,assign)int level;//英雄等级
@property(nonatomic,assign)int experience;//经验
@property(nonatomic,assign)int currentHP;//当前血量
@property(nonatomic,assign)int originalHP;//最初血量
@property(nonatomic)CGPoint currentPoint;//当前位置
@property(nonatomic)CGPoint originalPoint;//最初位置



-(instancetype)initWithName:(NSString *)aName;

//移动行为
-(void)movePoint:(CGPoint)point;

//攻击怪物行为
-(void)attackMonster:(Monster *)monster;//如果同时在Hero.h和Monser.h中import Monster.h和Hero.h,就会在这里报错

//受到攻击
-(void)beAttackByMonster:(Monster *)monster;

@end

然后是实现的Hero.m文件

//
//  Hero.m
//  OC_sj2.1
//
//  Created by Mac on 15/8/4.
//  Copyright (c) 2015年 yue. All rights reserved.
//

#import "Hero.h"
#import "Monster.h"

@implementation Hero

@synthesize name,level,experience,currentHP,originalHP,currentPoint,originalPoint;

-(instancetype)initWithName:(NSString *)aName
{
    
    if (self = [super init])
    {
        //初始化等级
        [self setLevel:1];
        
        //初始化血量
        [self setOriginalHP:10];
        [self setCurrentHP:[self originalHP]];
        
        //初始化位置
        [self setCurrentPoint:CGPointMake(0, 0)];
        
        NSLog(@"我是英雄,我的等级是:%d  血量是:%d ",[self level],[self currentHP]);
    }
    
    return self;
}

//移动行为
-(void)movePoint:(CGPoint)point
{
    currentPoint.x = currentPoint.x + point.x;
    currentPoint.y = currentPoint.y + point.y;
    NSLog(@"英雄移动到了位置:%g %g",currentPoint.x,currentPoint.y);
}

//攻击怪兽行为
-(void)attackMonster:(Monster *)monster
{
    [monster beAttackByHero:self];//调用怪物被攻击的方法
}

//受到攻击
-(void)beAttackByMonster:(Monster *)monster
{
    int temp = currentHP - 1;
    [self setCurrentHP:temp];
    NSLog(@"我(英雄)被攻击了!我的血量还剩:%d",[self currentHP]);
}


@end

然后是Monster.h文件

//
//  Monster.h
//  OC_sj2.1
//
//  Created by Mac on 15/8/4.
//  Copyright (c) 2015年 yue. All rights reserved.
//

#import 
//之前出现错误是用的import
//#import "Hero.h"
@class Hero;

@interface Monster : NSObject

@property(nonatomic,assign)int currentHP;//当前血量
@property(nonatomic,assign)int originalHP;//最初血量
@property(nonatomic,assign)int damage;//攻击力
@property(nonatomic,assign)int defense;//防御力
@property(nonatomic)CGPoint currentPoint;//当前位置
@property(nonatomic)CGPoint originalPoint;//最初位置

//移动行为
-(void)movePoint:(CGPoint)point;

//攻击英雄行为
-(void)attackHero:(Hero *)hero;//如果同时在Hero.h和Monser.h中import Monster.h和Hero.h,就会在这里报错

//受到攻击
-(void)beAttackByHero:(Hero *)hero;

//逃跑行为
-(void)escape;

@end

下面是实现文件Monster.m

//
//  Monster.m
//  OC_sj2.1
//
//  Created by Mac on 15/8/4.
//  Copyright (c) 2015年 yue. All rights reserved.
//

#import "Monster.h"
#import "Hero.h"

@implementation Monster

@synthesize currentHP,originalHP,damage,defense,currentPoint,originalPoint;

-(instancetype)init
{
    if (self = [super init])
    {
        //初始化最初血量
        [self setOriginalHP:5];
        [self setCurrentHP:originalHP];
        //初始化攻击力
        [self setDamage:1];
        //初始化防御力
        [self setDefense:1];
        
        NSLog(@"我是怪物,我的血量是:%d",[self currentHP]);

    }
    return self;
}

//移动行为
-(void)movePoint:(CGPoint)point
{

    currentPoint.x = currentPoint.x + point.x;
    currentPoint.y = currentPoint.y + point.y;
    
    NSLog(@"怪物移动到了位置:%g %g",currentPoint.x,currentPoint.y);
}

//攻击英雄行为
-(void)attackHero:(Hero *)hero
{
    [hero beAttackByMonster:self];
}

//受到攻击
-(void)beAttackByHero:(Hero *)hero
{
    int temp = currentHP - 1;
    [self setCurrentHP:temp];
    NSLog(@"我(怪物)被攻击了!我的血量还剩%d",[self currentHP]);
}

//逃跑行为
-(void)escape
{
    NSLog(@"打不过你,还跑不过你!");
}

@end

错误的原因就是在这两个相互调用的类里面,有种依赖关系,相互依赖,所以在编译的时候肯定是通不过的,详情我也不是很清楚,如果懂了,毕竟更新,先提供下解决方法

1、在其中一个头文件中,把import改成@class 

2、这两个头文件中,全都用@class

为什么要用@class,是为了能够防止重复编译,导致的编译不通过,@class的作用只是声明一个类,不能调用方法以及属性,但是在.h中好像也用不大到方法和属性的调用才对,本人新手一枚,讲的肯定不是那么对,我只是努力地将问题说明一下,有错误必将改正,谢谢

你可能感兴趣的:(Objective-C)