为什么我不能逐个递增变量?

时间:2014-03-24 18:03:59

标签: objective-c

每当我点击按钮flipButton一次,变量curPage就会增加4.我想将它递增一。有人能告诉我有什么问题吗? 感谢

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    curPage = 0 ;
    NSString * dic = nil;
    if ((NSInteger)tempIndex == 0) {
        type = @"Fire_";
        dic = [NSString stringWithFormat:@"%@%d",type,0];
    }
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:dic ofType:@"htm"];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile  encoding:NSUTF8StringEncoding error:nil];
    [contentWebView loadHTMLString:htmlString baseURL:nil];
    UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"Flip"
                                   style:UIBarButtonItemStyleBordered
                                   target:self
                                   action:@selector(flipView)];
    self.navigationItem.rightBarButtonItem = flipButton;
}

-(IBAction)flipView{
    curPage = curPage + 1;
    NSString *path = [NSString stringWithFormat:@"%@%d",type,(int)curPage];
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:path ofType:@"htm"];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    [contentWebView loadHTMLString:htmlString baseURL:nil];
    NSLog(path);
}

1 个答案:

答案 0 :(得分:2)

我的猜测是,这是因为您在声明intcurPage后放了一个星号:

int *curPage;

这也是编译器在将(int)传递给curPage

时将stringWithFormat:添加到int前面的原因

这声明了一个整数指针,而不是整数。指针按其指向的大小递增,因此在具有32位{{1}}的平台上,它们递增4(32位占用的字节数)。在声明基本类型的变量时,不需要星号。