Я пытаюсь сохранить UIBezierPath и некоторые другие значения в NSDictionary.UIBezierPath в NSDictionary - это возможно?
Я пишу в словаре так:
NSMutableArray *paths = [[NSMutableArray alloc]init];
touchesBegan:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage];
path = [[UIBezierPath bezierPath] retain];
path.lineCapStyle = kCGLineCapRound;
path.lineWidth = brushSize;
[path moveToPoint:touchPoint];
[self updateDrawingBoard];
}
touchesEnded:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage];
[path addLineToPoint:touchPoint];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: path, @"path", r, @"red", g, @"green", b, @"blue", alpha, @"alpha", brushSize, @"size", nil];
[paths addObject:dict];
[dict release];
[path release];
path = nil;
[self updateDrawingBoard];
}
И читать его, как это одна:
- (void) updateDrawingBoard {
UIGraphicsBeginImageContext(self.drawImage.bounds.size);
[[UIColor colorWithRed:r green:g blue:b alpha:alpha] setStroke];
NSLog(@"count: %d", [paths count]);
for (NSDictionary *dict in paths) {
NSLog(@"dict: %@", dict);
//Here I get the error
UIBezierPath *p = [dict objectForKey:@"path"];
p.lineWidth = [[dict objectForKey:@"size"]floatValue];
[[UIColor colorWithRed:[[dict objectForKey:@"red"]floatValue]
green:[[dict objectForKey:@"green"]floatValue]
blue:[[dict objectForKey:@"blue"]floatValue]
alpha:[[dict objectForKey:@"alpha"]floatValue]] setStroke];
[p stroke];
}
[path stroke];
self.drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Но я получаю эту ошибку:
[__NSArrayI objectForKey:]: unrecognized selector sent to instance 0x1b0210
Не думаю, что я делаю что-то неправильно.
словарь журнала:
dict: (
"<UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x1b30a0; state = Possible; enabled = NO; delaysTouchesBegan = YES; view = <UIScrollView 0x1e0380>; target= <(action=delayed:, target=<UIScrollView 0x1e0380>)>>",
"<UIScrollViewPanGestureRecognizer: 0x1b0b60; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <UIScrollView 0x1e0380>; target= <(action=handlePan:, target=<UIScrollView 0x1e0380>)>; must-fail = {\n <UIScrollViewPagingSwipeGestureRecognizer: 0x1ec7c0; state = Possible; enabled = NO; view = <UIScrollView 0x1e0380>; target= <(action=_handleSwipe:, target=<UIScrollView 0x1e0380>)>>\n }>",
"<UIScrollViewPagingSwipeGestureRecognizer: 0x1ec7c0; state = Possible; enabled = NO; view = <UIScrollView 0x1e0380>; target= <(action=_handleSwipe:, target=<UIScrollView 0x1e0380>)>; must-fail-for = {\n <UIScrollViewPanGestureRecognizer: 0x1b0b60; state = Possible; enabled = NO; delaysTouchesEnded = NO; view = <UIScrollView 0x1e0380>; target= <(action=handlePan:, target=<UIScrollView 0x1e0380>)>>\n }>"
)
словарь без путей: выход
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:r], @"red",
[NSNumber numberWithFloat:g], @"green",
[NSNumber numberWithFloat:b], @"blue",
[NSNumber numberWithFloat:alpha], @"alpha",
[NSNumber numberWithFloat:brushSize], @"size", nil];
[paths addObject:dict];
[dict release];
Вход:
count: 0
2011-06-09 10:46:28.813 L3T[913:207] count: 1
2011-06-09 10:46:28.815 L3T[913:207] dict: {
alpha = 1;
blue = 0;
green = 1;
red = 0;
size = 5;
}
2011-06-09 10:46:32.552 L3T[913:207] count: 1
sharedlibrary apply-load-rules all
Current language: auto; currently objective-c
(gdb)
Заканчивается также с треском.
@madmax Что означает 'NSLog (@" dict:% @ ", [dict class]);' yield? –
Вы должны добавить больше кода, поскольку ошибка, безусловно, находится в других частях ваших источников. Прямо сейчас вы управляете тем, что на самом деле является NSArray, как если бы это был NSDictionary. – Till
Отредактировал мой вопрос – madmax