Pergunta

I have .h and .m to implement NSCoding, but the mutableArray object count always be 0...

.h

#import <Foundation/Foundation.h>

@interface Favorite : NSObject <NSCoding> {
    NSMutableArray *myArray;
}
@property (nonatomic, retain) NSMutableArray *myArray;
@end

.m

#import "Favorite.h"

@implementation Favorite
- (void)dealloc {
    [myArray release];
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:myArray];
}

- (id)initWithCoder:(NSCoder *)decoder {
    myArray = [[decoder decodeObject] retain];
    return self;
}
@end

I'll use this class like

Favorite *fav = [[Favorite alloc] init];
fav.myArray = self.anotherArray;
[fav release];

Is it correct??

Foi útil?

Solução

You need to allocate the NSMutableArray like this ,

myArray=[[NSMutableArray alloc]init];

happy coding

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top