多线程程序中的属性值

时间:2012-04-12 16:25:44

标签: objective-c ios

我在头文件中初始化了两个属性:

@property (readwrite, assign) int Figure1;
@property (readwrite, assign) int State;

和.m

@synthesize Figure1;
@synthesize State;

然后我得到了

UISwipeGestureRecognizer *swipeLeft = 
    [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftMade:)] autorelease];
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self addGestureRecognizer:swipeLeft];

UITapGestureRecognizer *oneFingerTwoTaps = 
    [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ooneFingerTwoTaps)] autorelease];
    [oneFingerTwoTaps setNumberOfTapsRequired:2];
    [oneFingerTwoTaps setNumberOfTouchesRequired:1];
    [self addGestureRecognizer:oneFingerTwoTaps];

在课堂上初始化。

方法叫做:

- (void)ooneFingerTwoTaps
{
    [NSThread detachNewThreadSelector:@selector(oneFingerTwoTaps) toTarget:self withObject:nil];
}

- (void)swipeLeftMade:(UISwipeGestureRecognizer *)recognizer
{
    [NSThread detachNewThreadSelector:@selector(moveLeftSwipe) toTarget:self withObject:nil];
}

在第一个主题中有主程序:

- (void)oneFingerTwoTaps
{
    PlayScene *tView = [[PlayScene alloc]initWithFrame:CGRectMake(0, 0, 320, 420)];
    [self addSubview:tView];
    while (GameState==GamePlaying) {
        Figure1 = 1; State = 1;
        [self moveFig];
    }
}

在第二个线程中,我需要使用在第一个线程上更改的属性值

-(void)moveLeftSwipe  {
int fset = State, figure=Figure1;
//some other stuff
}

但问题是线程之间没有共享属性的值,我被告知“非原子”可能会导致这样的问题,但我没有使用它。可能是我在宣布错误?

1 个答案:

答案 0 :(得分:0)

只使用静态变量而不是属性。静态变量在线程之间共享。