首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问NSUInteger属性

访问NSUInteger属性
EN

Stack Overflow用户
提问于 2013-07-06 04:48:39
回答 1查看 413关注 0票数 0

我在访问代码中的NSUInteger属性时遇到了一些问题,如下所示:

MyController.h

代码语言:javascript
复制
@interface MyController : UIViewController
  @property (retain, nonatomic) NSArray *updatesArray;

  @property (nonatomic) NSUInteger madeUpdatesCounter;
  @property (nonatomic) NSUInteger allUpdatesCounter;
@end

MyController.m

代码语言:javascript
复制
@implementation MyController

  @synthesize updatesArray;

  @synthesize madeUpdatesCounter;
  @synthesize allUpdatesCounter;

- (void)viewDidLoad
{
    ....
    madeUpdatesCounter = 0;
    allUpdatesCounter = [updatesArray count];

    ....

    // Register for progress notifications    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(makeProgressChange)
                                                 name:@"MadeOneUpdateNotification"
                                               object:nil];
}

- (void)makeProgressChange
{
     madeUpdatesCounter++;
     NSLog(@"Update progress: %d/%d", madeUpdatesCounter, allUpdatesCounter);
}
@end

我通过添加到NSOperationQueue,以NSInvocationOperation的身份处理我的更新。在一个更新操作的末尾,我发送通知:

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter] postNotificationName:@"MadeOneUpdateNotification" object:nil];

在执行上面的代码后,接收通知只执行一次,在日志中我看到了类似这样的情况:

更新进度: 1/3

当我更改线路时:

代码语言:javascript
复制
allUpdatesCounter = [updatesArray count];

代码语言:javascript
复制
allUpdatesCounter = 3;

然后一切正常,我在日志中看到:更新进度: 1/3更新进度: 2/3更新进度: 3/3

在加载视图之前初始化变量updatedArray。是这样做的:

代码语言:javascript
复制
MyController *doUpdatesVC = [self.storyboard instantiateViewControllerWithIdentifier:@"MyController"];
doUpdatesVC.updatesArray = updatesArray;
[self presentViewController:doUpdatesVC animated:YES completion:nil];

你有什么建议或提示吗?我在代码中做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-15 19:04:34

好了,我已经找到问题的原因了。通过从队列启动的操作访问同一变量而锁定的应用程序。当我更改代码的逻辑时,一切都开始正常工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17496653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档