我有.h
并且.m
要实现NSCoding
,但mutableArray
对象计数总是为 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
我会像这样使用这个类
Favorite *fav = [[Favorite alloc] init];
fav.myArray = self.anotherArray;
[fav release];
这是正确的吗??