简体   繁体   English

OBJ-C-如果语句被跳过?

[英]Obj-c- if statement being skipped?

I have a block of code inside of a button that contains various if statements. 我在包含各种if语句的按钮内有一段代码。 All work except my last one, 除了我的最后一个,所有的工作,

if (usercurrentPoints < hoursCalculated ) { 如果(usercurrentPoints <hoursCalculated){

This if statement seems to be skipped, and I'm not sure why (I tried to cut this code down as much as possible)? 这个if语句似乎被跳过了,我不确定为什么(我试图尽可能地减少此代码)? Eg if points is greater than currentuserPoints, an alert should pop up. 例如,如果点数大于currentuserPoints,则应弹出警报。 However it bypasses this, and just saves my data anyway (thus, sending my user into a negative point balance). 但是,它绕过了这一点,只是仍然保存了我的数据(因此,使我的用户陷入了负数结余)。 Any help is appreciated! 任何帮助表示赞赏!

- (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"];

PROBLEM: 问题:

Using < or > with strings does weird stuff. 在字符串中使用<或>确实很奇怪。 It certainly won't do what you want consistently. 它肯定不会一贯地做您想要的。

MINIMAL CODE CHANGE SOLUTION: 最小代码更改解决方案:

[usercurrentPoints intValue] by itself does nothing meaningful. [usercurrentPoints intValue]本身没有任何意义。 Since you only need to fix the comparison, change it to if ([usercurrentPoints intValue] < [hoursCalculated intValue]) . 由于只需要修复比较,因此将其更改为if ([usercurrentPoints intValue] < [hoursCalculated intValue])

BETTER SOLUTION: 更好的解决方案:

Use the correct data type for the value. 使用正确的数据类型作为值。 points and hoursCalculated are only ever used as integers, so there's no reason to define them as strings. pointshoursCalculated只能用作整数,因此没有理由将它们定义为字符串。 I'm not familiar with DIOSSession, but I don't see why you couldn't assign the intValue to an int variable instead. 我对DIOSSession不熟悉,但是我不明白为什么不能将intValue分配给int变量。

You are comparing two pointers not the value and the result depends on the relative locations in the address space of the objects pointed to. 您在比较两个指针而不是值,结果取决于所指向对象的地址空间中的相对位置。

less-than-comparison-for-void-pointers might help you for better understanding. 小于指针的比较可能会帮助您更好地理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM