lundi 28 septembre 2015

Using a BOOL to replace verbose code

I have a hideous line of code that's a gigantic PITA to type repeatedly and to read. I tried to replace it with a nice, neat BOOL property and corresponding method in my class, but it's "flakey" (and it doesn't crash). I'd like to replace this hideous monstrosity with something neater. Here's what I've done so far.

This is works...

    if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"stretchMultiplier"]integerValue] * self.currentStretch.sideMultiplier.integerValue < self.stretchSideMultiplierCount) {
        // blah blah blah
    }

...but it looks hideous, so I tried to get rid of it by creating a BOOL property in the implementation:

@property (nonatomic, assign) BOOL lastRoundOfCurrentStretch;

...and a method down below...

- (BOOL) lastRoundOfCurrentStretch {
    if (self.currentStretch.sideMultiplier.intValue * [[[NSUserDefaults standardUserDefaults]valueForKey:@"defaultStretchRepetition"]intValue] == self.stretchSideMultiplierCount) {
        NSLog(@"** LAST ROUND **");
        return YES;
    } else {
        return NO;
    }
}

When I'm done, I'd like to be able to use this BOOL as an alternative to typing out the hideous line of code at the top of my query.

Begin Edit This should be NO, not YES, as I originally typed.

    if (self.lastRoundOfCurrentStretch == NO) {
        // blah blah blah
    }

End Edit

After I made the change, the class isn't working the way it "used to", but it's not crashing. I'm certain this boils down to startlingly stupid user error on my part, but I'm coming up short finding an answer here. This is as close as I got to an answer that's applicable to what I'm trying to do.

Using a BOOL property

Aucun commentaire:

Enregistrer un commentaire