我制定了一份财产清单,在无政府制时,首先出现了一系列称为可选择目标的群体。 可选择 物体类别并不包含一种内分法,它只是一些变数的集装箱,当然符合NSCoding。
可选择 选择了目标,根据选定目标,我就选择了一种可选择用途的子类。 引领这一动物。
我撰写了《动物目标》的习惯初衷,认为这是一种可选择的物体,是为了获得可选择的生物量值,然后又增加了自己的价值。 因此,这种方法认为:
- (AnimalObject *)initWithObject:(SelectableObject *)selectedObject
我从甄选屏幕上把它称作:
AnimalObject *animal = [[AnimalObject alloc] initWithObject:self.selectedObject];
如果是自发的。 选定 目标是从甄选屏幕中挑选的。
现在,在动物园地的初创者中,我确定了最初的数值,然后回到原地,叫 call。
[self saveToFile];
to save the new object to file. So having initialised it in the selection screen, I should be able to release it straight away right?
然而,如果我试图予以释放,即
AnimalObject *animal = [[AnimalObject alloc] initWithObject:self.selectedObject];
[animal release];
我坠毁。
如果我不放电,我就得到警告,动物目标初始种植者中所有首选的单体动物都在泄露记忆。
我在选择Screen时试图创造动物遗产,在释放新物体之前将新物体转让给它,但这仍然坠毁,即。
self.newAnimal = animal;
[animal release];
我很想知道,从我上述描述来看,我是否做过错? 如同在初学者中拯救一班。 或者通过母子类作为反对子类初生者...... 我不清楚我为什么可以释放我所制造的物体。
感谢任何帮助!
EDIT Ok, here s my code:
选择目标类别:
#define kObjectNumberKey @"ObjectNumber"
#define kObjectTypeKey @"Type"
#define kObjectNameKey @"Name"
#define kObjectThumbKey @"Thumb"
#define kObjectMainKey @"Main"
#import <Foundation/Foundation.h>
@interface SelectableObject : NSObject <NSCoding> {
int number;
NSString *type;
NSString *name;
NSString *thumbString;
NSString *mainString;
}
@property (nonatomic, assign) int number;
@property (nonatomic, retain) NSString *type;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *thumbString;
@property (nonatomic, retain) NSString *mainString;
@end
#import "SelectableObject.h"
@implementation SelectableObject
@synthesize number;
@synthesize type;
@synthesize name;
@synthesize thumbString;
@synthesize mainString;
- (void)dealloc {
NSLog(@"Selectable Object DEALLOC");
[name release];
[type release];
[thumbString release];
[mainString release];
[super dealloc];
}
#pragma mark -
#pragma mark NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeInt:self.number forKey:kObjectNumberKey];
[aCoder encodeObject:self.type forKey:kObjectTypeKey];
[aCoder encodeObject:self.name forKey:kObjectNameKey];
[aCoder encodeObject:self.thumbString forKey:kObjectThumbKey];
[aCoder encodeObject:self.mainString forKey:kObjectMainKey];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
self.number = [aDecoder decodeIntForKey:kObjectNumberKey];
self.type = [aDecoder decodeObjectForKey:kObjectTypeKey];
self.name = [aDecoder decodeObjectForKey:kObjectNameKey];
self.thumbString = [aDecoder decodeObjectForKey:kObjectThumbKey];
self.mainString = [aDecoder decodeObjectForKey:kObjectMainKey];
}
return self;
}
@end
另一类动物区系为新动物区,其细节如下:
- (void)createNewAnimal {
self.selectedObject.name = self.nameField.text;
Animal *animal = [[Animal alloc] initWithObject:self.selectedObject];
// [animal release]; (causes it to crash)
// Initialise the new Root Controller and load with selected object
}
动物类:
- (Animal *)initWithObject:(SelectableObject *)selectedObject {
if (self = [super init]) {
//Initialise all the values
dateCreated = [[NSDate alloc] init];
dateLastUsed = [[NSDate alloc] init];
name = selectedObject.name;
thumbString = selectedObject.thumbString;
mainString = selectedObject.mainString;
}
[self saveToFile];
return self;
}