0
我想构建一个可以在线程中运行循环的对象。 当我运行使用NSThread螺纹循环,有时工作正常有时出现错误消息:NSThread中的Objective-c Loop
Uncaught exception NSInvalidArgumentException, reason: Tried to add nil value for key 'NSArgumentDomain' to dictionary.
如何解决呢?或NSthread教程可以有所帮助。
#import <Foundation/Foundation.h>
@interface ThreadTest: NSObject
-(void) run;
-(void) goThread;
@end
@implementation ThreadTest
-(void) run {
@autoreleasepool {
int i;
for (i = 1; i < 5; i++) {
NSLog (@"Bar");
}
NSLog (@"end of loop");
}
}
-(void) goThread {
[NSThread detachNewThreadSelector: @selector(run)
toTarget: self
withObject: nil];
}
int main(void) {
@autoreleasepool {
ThreadTest *t = [[ThreadTest alloc] init];
[t goThread];
}
return 0;
}
我不明白,你能解释一下吗? – Sam
我误解了你的问题,看着你的错误日志。可能会出现异常,因为这不是线程安全的,多个线程可能试图同时访问该项目。这里有很多类似的问题,你应该通过很好的答案来搜索。看看这个例子:http://stackoverflow.com/questions/12091212/understanding-nsrunloop – 2017-01-16 13:11:19
我想通过制作像乌龟和兔子赛车程序的比赛游戏来理解objective-c中的线程。它不像java中的线程那么容易使用。我想我需要更多地了解这一点。非常感谢你。 – Sam