On 12 Aug 2008, at 15:50, Negm-Awad Amin wrote:

Am Di,12.08.2008 um 10:42 schrieb Gerriet M. Denkmann:

On 9 Aug 2008, at 17:39:20 -0600, Jonathan deWerd <[EMAIL PROTECTED]>wrote:

On Aug 9, 2008, at 4:48 PM, Cate Tony wrote:
[...]

Also, why are you using non-keyed encoding? -encodeObject:forKey: is
the preferred way of doing things nowadays...

Well, it may be the preferred way, but it is not always the best, nor even the correct way.

Two reasons:
1. the resulting files are much bigger
2. NSKeyedArchiver can only store certain strings (tested in 10.4.11), which makes it absolutely unusable for the storage of strings if the possible values are not known in advance.
Do you have an example for that?

Yes, I have.

1. Simple Test:
In IB create new window, add NSTextField with content "$null" (without the quotes) save in 10.2 or later format. Close. Open again. Look at your string.

2. A whole new project:

------------- main.m --------------
#import <Foundation/Foundation.h>
#import "TwoStrings.h"

int main (int argc, const char * argv[])
{
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString *s1 = @"$null"; // NSKeyedArchiver cannot handle this - last tested 10.4.11
        NSString *s2 = @"$Null";
        
        TwoStrings *to = [ [ TwoStrings alloc ] initWith: s1  and: s2 ];
        NSLog(@"      original: %@", to);
        
        NSData *dataA  = [ NSArchiver archivedDataWithRootObject: to ];
        TwoStrings *ta = [ NSUnarchiver  unarchiveObjectWithData: dataA ];
        NSLog(@"      Archiver: %@ %3u bytes", ta, [ dataA length ]);

        NSData *dataK  = [ NSKeyedArchiver archivedDataWithRootObject: to ];
        TwoStrings *tk = [ NSKeyedUnarchiver  unarchiveObjectWithData: dataK ];
        NSLog(@" KeyedArchiver: %@ %3u bytes", tk, [ dataK length ]);
        
        [pool release];
        return 0;
}


------------- TwoStrings.h --------------
@interface TwoStrings : NSObject
{
        NSString *s1;
        NSString *s2;
}

- initWith:(NSString *)sender and: (NSString *)s ;

@end


------------- TwoStrings.m --------------
#import "TwoStrings.h"

@implementation TwoStrings

- initWith:(NSString *)sender and: (NSString *)s ;
{
        self = [ super init ];
        if ( self == nil ) return nil;
        s1 = [ sender retain ];
        s2 = [ s retain ];
        return self;
}

- (void)dealloc
{
        [ s1 release ];
        [ s2 release ];
        [ super dealloc ];
}

- (NSString *)description
{
NSString *s = [ NSString stringWithFormat: @"<%@ %p s1=\"[EMAIL PROTECTED]" s1=\"%@ \">",[ self class], self, s1, s2 ];
        return s;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
//[super encodeWithCoder:coder]; // not for direct subclasses of NSObject
        
        if ( [coder allowsKeyedCoding] )
        {
                [ coder encodeObject: s1        forKey: @"s1" ];
                [ coder encodeObject: s2        forKey: @"s2" ];
        }
        else                                                            
        {
                [ coder encodeObject: s1 ];
                [ coder encodeObject: s2 ];
        };
}

- (id)initWithCoder:(NSCoder *)decoder
{
//self = [super initWithCoder:decoder]; // not for direct subclasses of NSObject
        
        if ( [decoder allowsKeyedCoding] )      
        {
                s1 = [ decoder decodeObjectForKey: @"s1" ];   
                s2 = [ decoder decodeObjectForKey: @"s2" ];   
        }
        else                                                    
        {
                s1 = [ decoder decodeObject ];
                s2 = [ decoder decodeObject ];
        };
        
        [ s1 retain ];
        [ s2 retain ];
        
        return self;
}

@end

------------- End of project --------------

Maybe this will work on Leopard. On Tiger it does not. (No crash, nor error message either).


Kind regards,

Gerriet.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to