views:

61

answers:

2

Hi all,

i'm new to iphone programming and i encountered/noticed some problems while i was coding. which was after i typed a statement like

if (label.text > label2.text) { do something...}

however, after typing my application can be compiled and run however when i try to validate it by comparing the values, my specified actions can run and i can change my image view image, however the conditions is not true but the specified actions can be run. Do enlighten me thanks! i will post my codes at the bottom, do comment if you spot any better practices? thanks once again.

Oh and how can i specify to check in my label that the default value is not "Label" or empty because i would like the values to be populated with number before commencing.

-(IBAction) beginMatch {

if (resultP1.text, resultP2.text = @"Label") {
    errorMsg.text = @"Please Press Roll (:";
}

else 

if (resultP1.text > resultP2.text) {
    MG = [MainGameController alloc];

    MG.player1 = playerName.text;

}
else {

    MG.player1 = playerNameP2.text;


}   

[self.view addSubview:MG.view];

}

this is one example that it does not work i have another one which is below.

-(IBAction) btn:(id) sender {

ptc = [playerTurnController alloc];
if (ptc.player1name = MGplayerName.text) {
    if (lblDiceResultP1.text > lblDiceResultP2.text) {
        img.image = [UIImage imageNamed:@"yellow.png"];
    }
    else if (ptc.player2name = MGplayerName.text) {
        img.image = [UIImage imageNamed:@"Blue.png"];
    }
}

}

Thank you.

+1  A: 

In here you're comparing string (alphabetically) addresses:

lblDiceResultP1.text > lblDiceResultP2.text

You probably want to extract NSNumbers of out the strings and compare the numeric values.

This here is an assignment and not a comparison:

ptc.player2name = MGplayerName.text

You probably meant to use == which is also wrong.

NSStrings are compared with the isEqualToString e.g.

NSString * s1 = @"String One";
NSString * s2 = @"String Two";
if([s1 isEqualToString:s2])
     // do something when strings are equal
diciu
That isn't comparing the strings alphabetically — it's comparing the strings' addresses numerically.
Chuck
@Chuck - thanks, I've edited my answer.
diciu
@diciu - thanks for your comments but i'm new, could you point me towards your meaning of "You probably want to extract NSNumbers of out the strings and compare the numeric values." i do not quite get it
malvin
From NSString to NSNumber: http://stackoverflow.com/questions/1448804/how-to-convert-an-nsstring-into-an-nsnumber Then use the method "compare:" on one of the nsnumbers to compare it against the other: http://developer.apple.com/mac/library/documentation/cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html#//apple_ref/occ/instm/NSNumber/compare:
diciu
+2  A: 

Your code contains quite a few errors. You're trying to compare NSString values with ">", you're using the comma operator and = operator incorrectly, and you're allocating new objects in (what look to be) the wrong places.

You really should work your way through the introductory documentation on Apple's developer website first: Learning Objective-C: A Primer and Your First iPhone Application

Mark Bessey
alright, thank alot for your reccommendations.
malvin