문제

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??

도움이 되었습니까?

해결책

You need to allocate the NSMutableArray like this ,

myArray=[[NSMutableArray alloc]init];

happy coding

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top