Skip navigation.
 
mlRe: FYI: Bug with archived NSShadow objects
FROM : Ricky Sharp
DATE : Tue Dec 07 19:45:27 2004

On Tuesday, December 07, 2004, at 11:21AM, Ricky Sharp <<email_removed>> 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)

Related mailsAuthorDate
mlFYI: Bug with archived NSShadow objects Ricky Sharp Dec 7, 18:20
mlRe: FYI: Bug with archived NSShadow objects Ricky Sharp Dec 7, 19:45