Can't get code to compile on BNRDrawView.m


#1

Am having trouble with the code on BNRDrawView.m at Location 6550 on Kindle. Tried looking for the errors with FileMerge, but couldn’t find it. Have pasted the code in below; the error messages are commented out.

Thanks in advance.

BNRDrawView.m

[code]#import “BNRDrawView.h”
#import “BNRLine.h”

@interface BNRDrawView()

@property (nonatomic, strong) BNRLine *currentLine;
@property (nonatomic, strong) BNRLine *finishedLines;

@end

@implementation BNRDrawView

  • (instancetype)initWithFrame:(CGRect)r
    {
    self = [super initWithFrame:r];

    if (self) {
    self.finishedLines = [[NSMutableArray alloc] init]; // Incompatible pointer types assigning to BNRLine from NSMutableArray
    self.backgroundColor = [UIColor grayColor];
    }
    return self;
    }

  • (void)strokeLine:(BNRLine *)line
    {
    UIBezierPath *bp = [UIBezierPath bezierPath];
    bp.lineWidth = 10;
    bp.lineCapStyle = kCGLineCapRound;

    [bp moveToPoint:line.begin];
    [bp addLineToPoint:line.end];
    [bp stroke];

}

  • (void)drawRect:(CGRect)rect
    {
    // Draw finished lines in black
    [[UIColor blackColor] set];
    for (BNRLine *line in self.finishedLines) { // Collection expression type BNRLine may not respond to countByEnumeratingWithState:objects.count:
    [self strokeLine:line];
    }
    if (self.currentLine) {
    // If there is a line currently being drawn, do it in red
    [[UIColor redColor] set];
    [self strokeLine:self.currentLine];
    }
    }

  • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    UITouch *t = [touches anyObject];

    // Get location of the touch in view’s coordinate system
    CGPoint location = [t locationInView:self];

    self.currentLine = [[BNRLine alloc] init];
    self.currentLine.begin = location;
    self.currentLine.end = location;

    [self setNeedsDisplay];
    }

  • (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
    UITouch *t = [touches anyObject];
    CGPoint location = [t locationInView:self];

    self.currentLine.end = location;

    [self setNeedsDisplay];
    }

  • (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    [self.finishedLines addObject:self.currentLine]; // No visible interface for BNRLine declares the selector addObject:
    self.currentLine = nil;
    [self setNeedsDisplay];
    }
    [/code]


#2

Can you post you BNRLine code too? I’m guessing the problem might lie in that.


#3

Here you go; thank you:

BNRLine.h:

[code]#import <Foundation/Foundation.h>

@interface BNRLine : NSObject

@property (nonatomic) CGPoint begin;
@property (nonatomic) CGPoint end;

@end[/code]

BNRLine.m

[code]#import “BNRLine.h”

@implementation BNRLine

@end[/code]


#4

Just found the problem.

In BNRDrawView.m, I had declared finishedLines as a BNRLine property. Once I changed it to NSMutableArray everything worked.

Thank you.