【问题标题】:Obj-c- if statement being skipped?Obj-c- if 语句被跳过?
【发布时间】:2017-04-25 17:48:21
【问题描述】:

我在按钮内有一段代码,其中包含各种 if 语句。除了我的最后一个,所有的工作,

if (usercurrentPoints

这个 if 语句似乎被跳过了,我不知道为什么(我试图尽可能减少这段代码)? 例如如果点数大于 currentuserPoints,则应弹出警报。但是它绕过了这一点,并且无论如何都保存了我的数据(因此,将我的用户发送到负点余额)。任何帮助表示赞赏!

- (IBAction)sendMessage:(id)sender {

        NSInteger hour = [components hour];
        NSInteger minute = [components minute];

        NSString *points;

        if (hour <= 1) {

            points = @"1";


        } else if (hour > 1 && hour < 9) {

            points = @"9";


        } else if (hour <= 9) {

            points = @"9";

        } else if (hour > 9 && hour <= 24) {

            points = @"24";


        } else if (hour > 24 && hour <= 48) {

            points = @"48";

        } else if (hour > 48 && hour <= 72) {

            points = @"72";

        } else if (hour > 72 && hour <= 96) {

            points = @"96";

        } else if (hour > 96) {

            points = @"96";

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Uh Oh!"
                                                            message:@"Requests cannot exceed 5 days. Please create separate requests."
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];




           self.sendMessage.enabled = NO;



            return;


        }






        NSDictionary *swapValues = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:points, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
        NSDictionary *totalPoints = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:swapValues] forKey:@"und"];

        [nodeData setObject:totalPoints forKey:@"field_swapvalue"];



        NSDictionary *enddateValues = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:formattedendDate, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];

        NSDictionary *finalendDate = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:enddateValues] forKey:@"und"];

      [nodeData setObject:finalendDate forKey:@"field_endswaptime"];


        NSDictionary *swapInitiated = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Requested", nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
        NSDictionary *swapRequest = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:swapInitiated] forKey:@"und"];

        [nodeData setObject:swapRequest forKey:@"field_swapaccepted"];


        NSString *hoursCalculated = points;

            NSString *usercurrentPoints = [[[DIOSSession sharedSession] user] objectForKey:@"field_points_balance"][@"und"][0][@"value"];
        NSLog(@"USERS CURRENT BALANCE %@", usercurrentPoints);

[usercurrentPoints intValue];
[hoursCalculated intValue];


     if (usercurrentPoints < hoursCalculated ) {
            NSLog(@"CALCULATED %@", hoursCalculated);


            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Uh Oh!"
                                                            message:@"You don't have enough hours to complete this."
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];




        } else  {


            int result = [usercurrentPoints intValue] - [points intValue];


            NSString *resultPoints = [NSString stringWithFormat:@"%d", result];


            NSMutableDictionary *userData = [NSMutableDictionary new];


            NSDictionary *targetPoints = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects: resultPoints, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
            NSDictionary *finalPoints = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:targetPoints] forKey:@"und"];

            [userData setObject:finalPoints forKey:@"field_points_balance"];

【问题讨论】:

  • 您是否尝试过添加断点并遍历代码?
  • @Shabirjan 是的,并且跳过了 if 语句。
  • 用 int 代替 NSInteger 看看会发生什么?
  • 您正在尝试通过&lt;比较NSString*??你期望发生什么?
  • 您尝试比较字符串值是否有原因?

标签: ios objective-c


【解决方案1】:

问题:

将 与字符串一起使用会做一些奇怪的事情。它肯定不会始终如一地做你想做的事。

最小的代码更改解决方案:

[usercurrentPoints intValue] 本身没有任何意义。由于您只需要修复比较,因此将其更改为if ([usercurrentPoints intValue] &lt; [hoursCalculated intValue])

更好的解决方案:

为值使用正确的数据类型。 pointshoursCalculated 只用作整数,因此没有理由将它们定义为字符串。我不熟悉 DIOSSession,但我不明白为什么不能将 intValue 分配给 int 变量。

【讨论】:

    【解决方案2】:

    您正在比较两个指针而不是值,结果取决于指向的对象在地址空间中的相对位置。

    less-than-comparison-for-void-pointers 可能会帮助您更好地理解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      相关资源
      最近更新 更多