gpt4 book ai didi

objective-c - 为什么我的目标没有打印到控制台?

转载 作者:行者123 更新时间:2023-12-03 16:32:48 24 4
gpt4 key购买 nike

我刚刚开始学习目标,并按照所有说明创建了我的第一个控制台应用程序,该应用程序应该打印“哎呀!”每两秒一次,但尽管它在循环,但它没有打印到控制台,我不明白为什么。有人介意告诉我我做错了什么吗?

我的 .h 文件具有以下内容:

#import <Foundation/Foundation.h>

@interface Logger : NSObject

- (void)sayOuch:(NSTimer *)t;

@end

我的 .m 文件具有以下内容:

#import "Logger.h"

@implementation Logger


- (void)sayOuch:(NSTimer *)t
{
NSLog(@"Ouch!");
}


@end

最后,我的 main.m 文件包含以下内容:

#import <Foundation/Foundation.h>
#import "Logger.h"

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

@autoreleasepool {


Logger *logger = [[Logger alloc] init];
__unused NSTimer *timer = [NSTimer timerWithTimeInterval:2.0
target:logger
selector:@selector(sayOuch:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] run];

}
return 0;
}

问题是,每当我运行消息“哎呀!”时没有打印到我的控制台。感谢您提前提供的帮助。

最佳答案

改变

timerWithTimeInterval

scheduledTimerWithTimeInterval

所以你的代码更改为工作:

#import <Foundation/Foundation.h>
#import "Logger.h"

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

@autoreleasepool {


Logger *logger = [[Logger alloc] init];
__unused NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:logger
selector:@selector(sayOuch:)
userInfo:nil
repeats:YES];

[[NSRunLoop currentRunLoop] run];

}
return 0;
}

关于objective-c - 为什么我的目标没有打印到控制台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9035258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com