1

我有.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];

这是正确的吗??

4

1 回答 1

1

你需要这样分配NSMutableArray

myArray=[[NSMutableArray alloc]init];

快乐编码

于 2013-09-17T11:07:45.130 回答