Re: FYI: Bug with archived NSShadow objects
Re: FYI: Bug with archived NSShadow objects
- Subject: Re: FYI: Bug with archived NSShadow objects
- From: Ricky Sharp <email@hidden>
- Date: Tue, 07 Dec 2004 12:45:27 -0600
On Tuesday, December 07, 2004, at 11:21AM, Ricky Sharp <email@hidden> wrote:
>So instead of:
>
> [coder encodeObject:[self textShadow] forKey:@"MyTextShadow"];
>
>you'd use:
>
> NSShadow* theTextShadow = [self textShadow];
>
> [coder encodeObject:[theTextShadow shadowColor] forKey:@"MyTextShadowColor"];
> [coder encodeFloat:[theTextShadow shadowBlurRadius] forKey:@"MyTextShadowBlurRadius"];
> [coder encodeFloat:[theTextShadow shadowOffset].width forKey:@"MyTextShadowXOffset"];
> [coder encodeFloat:[theTextShadow shadowOffset].height forKey:@"MyTextShadowYOffset];
Don't know why I overlooked it, but instead of encoding/decoding the individual floats for the offset, you can just encode/decode the NSSize directly.
Since many of my widgets archive NSShadow, I came up with the following category to keep the code in one place. Feel free to use it without restriction in any of your code:
[NSCoder_ShadowAdditions.h]
#import <Cocoa/Cocoa.h>
@interface NSCoder (ShadowAdditions)
- (void)encodeShadow:(NSShadow*)aShadow forKey:(NSString*)aKey;
- (NSShadow*)decodeShadowForKey:(NSString*)aKey;
@end
[NSCoder_ShadowAdditions.m]
#import "NSCoder_ShadowAdditions.h"
//#define ENCODE_SHADOW_OBJECT
@implementation NSCoder (ShadowAdditions)
- (void)encodeShadow:(NSShadow*)aShadow forKey:(NSString*)aKey
{
#if defined (ENCODE_SHADOW_OBJECT)
[self encodeObject:aShadow forKey:aKey];
#else // #if defined (ENCODE_SHADOW_OBJECT)
NSSize theShadowOffset = [aShadow shadowOffset];
[self encodeObject:[aShadow shadowColor] forKey:[aKey stringByAppendingString:@"Color"]];
[self encodeFloat:[aShadow shadowBlurRadius] forKey:[aKey stringByAppendingString:@"BlurRadius"]];
[self encodeSize:[aShadow shadowOffset] forKey:[aKey stringByAppendingString:@"Offset"]];
#endif // #if defined (ENCODE_SHADOW_OBJECT)
}
- (NSShadow*)decodeShadowForKey:(NSString*)aKey
{
#if defined (ENCODE_SHADOW_OBJECT)
return (NSShadow*) [self decodeObjectForKey:aKey];
#else // #if defined (ENCODE_SHADOW_OBJECT)
NSShadow* theShadow = [[[NSShadow alloc] init] autorelease];
[theShadow setShadowColor:[self decodeObjectForKey:[aKey stringByAppendingString:@"Color"]]];
[theShadow setShadowBlurRadius:[self decodeFloatForKey:[aKey stringByAppendingString:@"BlurRadius"]]];
[theShadow setShadowOffset:[self decodeSizeForKey:[aKey stringByAppendingString:@"Offset"]]];
return theShadow;
#endif // #if defined (ENCODE_SHADOW_OBJECT)
}
@end
Usage...
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeShadow:[self shadowAttribute] forKey:@"MyShadow"];
...
}
- (id)initWithCoder:(NSCoder *)coder
{
if (self = [super initWithCoder:coder])
{
[self setShadowAttribute:[coder decodeShadowForKey:@"MyShadow"]];
...
}
return self;
}
--
Rick Sharp
Instant Interactive(tm)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden