如何添加到 Xcode 中的 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12226042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:23:41  来源:igfitidea点击:

How to add to an int in Xcode

objective-cxcodeintegerintadd

提问by Wesley Smith

Seems like this should be about the simplest thing you could do with an int but I cant seem to find anything showing how to do it. Tried google but I get a million hits all of which are way more in depth than what im trying to understand.

似乎这应该是你可以用 int 做的最简单的事情,但我似乎找不到任何显示如何做的东西。尝试过谷歌,但我得到了一百万次点击,所有这些都比我试图理解的更深入。

What I want to do is have an integer and as something happens (button press, image moved, whatever) I want to add 1 to this integer and have a separate method, when its called, check the integer and preform one of several actions depending on the value.

我想要做的是有一个整数,当发生某些事情时(按下按钮,移动图像,等等)我想在这个整数上加 1 并有一个单独的方法,当它被调用时,检查整数并根据几个动作执行一个在价值上。

I know how to do all of this except add 1 to the int?

除了将 1 添加到 int 之外,我知道如何执行所有这些操作?

I know this must be a crazy stupid question for you guys, anybody care to throw me a bone?

我知道这对你们来说一定是一个疯狂的愚蠢问题,有人愿意向我扔骨头吗?

回答by Dale Myers

int i = 4;
i++;

Is that what you are looking for?

这就是你要找的吗?

回答by Paresh Navadiya

Declare int index in .h file

在 .h 文件中声明 int 索引

Now on click of button:

现在点击按钮:

 -(void)btnClicked:(id)sender
 {
     index++;
 }

Any other method image moved then

然后移动的任何其他方法图像

index++;

Now perform action like this:

现在执行如下操作:

-(void)performAction:(int)index
{
   switch(index)
   {
      //make case depending on number
      case : 1
      {
          // do something here on index 1
      }
      break;
      .....
      .....
      .....
      default
      {
          // do something here if not that index 
      }
      break;

   }


}

回答by Asciiom

You should add a NSUInteger property to the UIViewController where the actons that you want to count take place. Then as a first step of each action you can add 1 to that property.

你应该向 UIViewController 添加一个 NSUInteger 属性,你想要计算的动作发生在那里。然后作为每个操作的第一步,您可以向该属性添加 1。

@interface MyViewController extends UIViewController
@property (nonatomic) NSUInteger actionCounter;
@end

@implementation MyViewController
@synthesize actionCounter;

//in your actions here, add one to actionCounter
-(void) IBAction doSomething{
    self.actionCounter++;

    //do the actual action here
}

//in your check method, read the actionCounter value and do something
-(void) checkActionCount{
    if(self.actionCounter => 1){
        NSLog(@"User did something");
    }
}

@end