cs193p – Assignment #2 Task #8

Please note, this blog entry is from a previous course. You might want to check out the current one.

Your Set game should also report (mis)matches like Required Task #3 in Assignment 1, but you’ll have to enhance this feature (to use NSAttributedString) to make it work for displaying Set card matches.

In storyboard copy the label from the card-game view to the set-game view and connect it to the set-game view controller. Change it from “plain” to “attributed”.

In updateUI create a new attributed string which derives from the last-flip description. Update this string with the card attributes in the for loop and – at the end – use it for the label text:

- (void)updateUI
{
    NSAttributedString *lastFlip = [[NSAttributedString alloc]
      initWithString:
        self.game.descriptionOfLastFlip ? self.game.descriptionOfLastFlip : @""];    
    for (UIButton *cardButton in self.cardButtons) {
        ...
        if ([card isKindOfClass:[SetCard class]]) {
            ...
            lastFlip = [self updateAttributedString:lastFlip 
                               withAttributesOfCard:setCard];
        }
        ...
    }    
    [super updateUI];
    self.resultOfLastFlipLabel.attributedText = lastFlip;
}

Just one tine problem remains, card.contents currently uses the symbol of the card only and is ambiguous.

Change it to hold all card attributes:

- (NSString *)contents
{
    return [NSString stringWithFormat:@"%@:%@:%@:%d", self.symbol, self.color, self.shading, self.number];
}

… and adjust the updateAttributedString:withAttributesOfCard: method to look for the whole string:

- (NSAttributedString *)updateAttributedString:(NSAttributedString *)attributedString withAttributesOfCard:(SetCard *)card
{
    ...
    NSRange range = [[mutableAttributedString string] rangeOfString:card.contents];
    ...
}

The complete code is available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail