让我先说我认为这是我的内存管理错误。我似乎无法弄清楚为什么会这样。
我有一个视图控制器和一个名为Part
.
#import <Foundation/Foundation.h>
@interface Part : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *partType;
@property (nonatomic, strong) NSString *description;
@property (nonatomic, strong) NSNumber *price;
- (id)initWithName:(NSString *)name AndType:(NSString *)type;
@end
在视图控制器中,我有一个属性设置如下:
@property (nonatomic, strong) Part *part;
在我创建一些静态数组并从中创建对象的功能init
中:ViewController
- (id)init {
self = [super init];
self.partList = [[NSMutableArray alloc] init];
NSArray *inputArray = @[@"Part1",
@"Part2",
@"Part3",
@"Part4",
@"Part5",
@"Part6",
@"Part7",
@"Part8"];
NSString *tempType = @"PartCategory";
// Add dummy static data
for (int i = 0; i < [inputArray count]; i++) {
Part *partInput = [[Part alloc] initWithName:[inputArray objectAtIndex:i] AndType:tempType];
//partInput.name = [inputArray objectAtIndex:i];
//partInput.partType = tempType;
NSLog(@"Inserting Part %@", partInput);
[self.partList addObject:partInput];
}
return self;
}
我在该NSLog
循环中调用Inserting Part *nil description*
的每个部分都会返回。我只是无法追踪这里发生的事情。
编辑:这是控制器使用的initWithName
方法Part
:
- (id)initWithName:(NSString *)name AndType:(NSString *)type {
if(self = [super init]) {
self.name = name;
self.partType = type;
}
return self;
}