Wael
Wael

Reputation: 71

Verifying the if statement syntax

system1I want to verify this syntax ( when pressing a radio group). Is this syntax correct :

if([[sender selectedCell ] tag]==1) 
        [prefs addChild:element(@"System", [NSString stringWithUTF8String:"system1"])];
    if([[sender selectedCell] tag]==2) 
        [prefs addChild:element(@"system", [NSString stringWithUTF8String:"system2"])];
    if([[sender selectedCell] tag]==3) {        
        [advancedButton setEnabled:NO];

        [notifs addChild:element(@"UploadedFiles", [NSString stringWithUTF8String:"YES"])];
        [notifs addChild:element(@"DeletedFiles", [NSString stringWithUTF8String:"YES"])];

    }

Upvotes: 0

Views: 54

Answers (2)

Alladinian
Alladinian

Reputation: 35646

Yes that would be correct. tag property is just an NSInteger so you can compare it to 1.


Geeky note: If you try to compile that exact code it would produce an error since the comment (//) applies to whatever follows in that line and includes your right curly bracket (EDIT: OP edited the question so my Geeky note doesn't apply any more)

Upvotes: 3

Hermann Klecker
Hermann Klecker

Reputation: 14068

if([[sender selectedCell] tag]==1) {/* do something */}

:)

Despite of that it should be fine.

The devil is in the details here. sender ist just an id object and therefore not more than NSObject *. You cannot just access the method without typecasting or receiving a compiler warning. It may not even compile when you use ARC.

For a full picture:

(IBAction) performMyAction:(id) sender {

  if ([sender isKindOfClass:[MyVeryOwnClass class]]) {

    MyVeryOwnClass myVeryOwnObject = (MyVeryOwnClass *) sender;

    if([[myVeryOwnObject selectedCell] tag]==1) {
      /* here goes the business logic */

     }
  }
}

There are other ways of archieving the same. You can just typecast stender without assigning it to a temp object reference. Or you can use performsToSelector: and performSelector to access selectedCell. For some reason I prefer it this way. I feel this is well readable and debugging is easy.

Upvotes: 1

Related Questions