4-4报错

4-4报错

//Protocol.h

#import <Foundation/Foundation.h>

@protocol MyDelegate;

@interface Protocol : NSObject

@property(nonatomic,weak) id<MyDelegate> delegate;

-(void)myPrint;

@end

@protocol MyDelegate <NSObject>

@optional

-(void)print;

@end


//Protocol.m

#import "Protocol.h"

@implementation Protocol

-(void)myPrint{

    if([self.delegate respondsToSelector:@selector(print)]){

        [self.delegate print];

    }

}

@end


//Delegate.h

#import <Foundation/Foundation.h>

#import "Protocol.h"

@interface Delegate : NSObject <MyDelegate>

-(void)testDelegate;

@end


//Delegate.m

#import "Delegate.h"

@implementation Delegate

-(void)testDelegate{

    Protocol *p = [[Protocol alloc]init];

    p.delegate = self;

    [p myPrint];

}

-(void)print{

    NSLog(@"你有一条消息,请查收");

}

@end


//main.m

#import <Foundation/Foundation.h>

#import "Delegate.h"

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        Delegate *d = [[Delegate alloc]init];

        [d testDelegate];

    }

    return 0;

}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
Tender10 2018-08-02 15:12:28

你那边报的什么错误,我复制了你的代码运行结果正确。

  • 提问者 lsyyyy #1
    就是上一个问题里的那个错误,我在xcode上运行也OK,可能是编译器的问题吧,不用看了o(*////▽////*)q
    2018-08-02 15:15:17
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师