2017-04-25 4 views
0

私は様々なif文を含むボタンの中にコードブロックを持っています。私の最後のものを除いてすべての作業、Obj-c- if文をスキップしていますか?

場合(usercurrentPoints < hoursCalculated){

このステートメントはスキップされているようだ、と(私はダウンのようにこのコードをカットしようとした私はなぜわからない場合可能な限り)? など。ポイントが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はい、そのif文はスキップされます。 – Brittany

+0

NSIntegerの代わりにintを使用し、何が起こるかを確認します。 –

答えて

2

問題:

文字列で> <を使用するかは奇妙なものを行います。確かに、あなたが一貫して欲しいものはしません。

MINIMALコード変更SOLUTION:

[usercurrentPoints intValue]それ自体では意味のある何もしません。比較を修正する必要があるだけなので、if ([usercurrentPoints intValue] < [hoursCalculated intValue])に変更してください。

よりよい解決策:

値の正しいデータ型を使用します。 pointshoursCalculatedは整数としてのみ使用されるため、文字列として定義する必要はありません。私はDIOSSessionに精通していませんが、代わりにintValueを代入できなかった理由はわかりません。

0

値ではなく2つのポインタを比較しています。結果は、指し示されたオブジェクトのアドレス空間内の相対位置によって異なります。

less-than-comparison-for-void-pointers あなたの理解を深めてください。

関連する問題