NCFUSN
NCFUSN

Reputation: 1654

if statement issue in drawRect

Yes, yes. Shame on me. I am trying to draw in UIView and my code is:

NSString *str;

if(kmObj.metal!=@"" && kmObj.metalName2!=@"" && kmObj.metalname3!=@"")
{
   str=[NSString stringWithFormat:@"%@ + %@ + %@",kmObj.metal,kmObj.metalName2,kmObj.metalname3]; 
}
if(kmObj.metal!=@"" && kmObj.metalName2!=@"" && kmObj.metalname3==@"")
{
   str=[NSString stringWithFormat:@"%@ + %@",kmObj.metal,kmObj.metalName2]; 
}
if(kmObj.metal!=@"" && kmObj.metalName2==@"" && kmObj.metalname3==@"")
{
   str=[NSString stringWithFormat:@"%@",kmObj.metal]; 
}
[str drawAtPoint:CGPointMake(10.0,234.0)
                forWidth:200
                withFont:[UIFont systemFontOfSize:20.0]
             minFontSize:20.0
          actualFontSize:NULL
           lineBreakMode:UILineBreakModeTailTruncation 
      baselineAdjustment:UIBaselineAdjustmentAlignBaselines];

So, this code suppose to check if the Object contains more than one metal name record. If so, than it has to format string to form: Au+Ag+Cu ... My problem is that in output draw I can't get rid of the + signs where I don't need them. Is there something wrong in my if statement?

Upvotes: 1

Views: 92

Answers (3)

bneely
bneely

Reputation: 9093

Instead of if (string != @""), use ![string isEqualToString:@""] or perhaps ([string length] > 0). You need to make sure you are performing a value comparison, not a pointer comparison.

Anyway, I would write code like this:

NSString *outputString = @"";

if ([firstString length] > 0) {
    outputString = [outputString stringByAppendingString:firstString];
}

if ([secondString length] > 0) {
    outputString = [outputString stringByAppendingFormat:@" + %@", secondString];
}

if ([thirdString length] > 0) {
    outputString = [outputString stringByAppendingFormat:@" + %@", thirdString];
}

With this technique, you check each string individually, and only include a plus sign when you know another valid string will follow it.

Upvotes: 2

X Slash
X Slash

Reputation: 4131

Did you mean that you don't want the "+" sign? Then don't put it in your NSString.

So instead of

str = [NSString stringWithFormat:@"%@ + %@ + %@",kmObj.metal,kmObj.metalName2,kmObj.metalname3];

do

str = [NSString stringWithFormat:@"%@ %@ %@",kmObj.metal,kmObj.metalName2,kmObj.metalname3];

also use [kmObj.metal isEqualToString:@""] for your string comparison

Upvotes: 0

Steve
Steve

Reputation: 31662

String comparisons should take the form: [stringA isEqualToString:stringB]

From the docs: When you know both objects are strings, this method is a faster way to check equality than isEqual:

Plus, == for strings is weird anyways - they are non-primitives and you're wanting a value comparison.

Also, you should take into account the possibility of having nil and/or [NSNull null] values (depending on where these values are sourced). Your current test of whether or not they are equal to empty strings doesn't take this into account.

Upvotes: 1

Related Questions