2017-04-25 106 views
0

我有一個包含各種if語句的按鈕內的代碼塊。除了我的最後一個所有的工作,Obj-c-如果語句被跳過?

如果(usercurrentPoints < hoursCalculated){

這個if語句似乎被跳過,我不知道爲什麼(我想下來,削減該代碼儘可能多)? E.g.如果points大於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"]; 
+0

您是否嘗試添加斷點並通過代碼? –

+0

@Shabirjan是的,如果語句被跳過。 – Brittany

+0

使用int而不是NSInteger,看看會發生什麼? –

回答

2

問題:

使用<或>用繩子做怪異的東西。它一定不會做你想要的。

最少的代碼變化解決方案:

[usercurrentPoints intValue]通過本身並沒有什麼意義。由於您只需要修正比較,請將其更改爲if ([usercurrentPoints intValue] < [hoursCalculated intValue])

更好的解決方案:

使用該值的正確的數據類型。 pointshoursCalculated只能用作整數,所以沒有理由將它們定義爲字符串。我對DIOSSession不熟悉,但是我不明白爲什麼你不能將intValue分配給一個int變量。