让我先说一下这个问题,我说我认为这是一个记忆管理错误.我似乎无法弄清楚它为什么会发生.
我有一个viewcontroller和一个名为的模型类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
Run Code Online (Sandbox Code Playgroud)
在视图控制器中,我有一个属性设置如下:
@property (nonatomic, strong) Part *part;
Run Code Online (Sandbox Code Playgroud)
在我的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;
}
Run Code Online (Sandbox Code Playgroud)
在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;
}
Run Code Online (Sandbox Code Playgroud)
当%@
用于打印NSObject时,它debugDescription
默认调用该description
对象的方法,并且您的Part
对象始终具有nil
描述.
您最好通过将description
属性名称更改为其他内容来解决此问题,因为它与description
方法冲突NSObject
.
另请参见:NSObject描述和debugDescription
归档时间: |
|
查看次数: |
3083 次 |
最近记录: |