iOS自定义对象初始化错误

Ale*_*Ale 6 iphone objective-c custom-object ios

我有一个自定义对象ProductCategory.

.h文件:

#import <Foundation/Foundation.h>

@interface ProductCategory : NSObject

@property int productCategoryId;
@property NSString *name;
@property NSArray *children;
@property int parentCategoryId;


- (id)initWithId:(int)productCategoryId name:(NSString*)name;

- (id)initWithId:(int)productCategoryId name:(NSString*)name children:(NSArray*)chidren parentCategoryId:(int)parentCategoryId;

@end
Run Code Online (Sandbox Code Playgroud)

.m文件:

#import "ProductCategory.h"

@implementation ProductCategory

- (id)init {
    if((self = [super init])) {
        self.parentCategoryId = 0;
    }

    return self;
}

- (id)initWithId:(int)productCategoryId name:(NSString*)name {
    if((self = [super init])) {
        self.productCategoryId = productCategoryId;
        self.name = name;
        self.parentCategoryId = 0;
    }

    return self;
}


- (id)initWithId:(int)productCategoryId name:(NSString*)name children:(NSArray*)chidren parentCategoryId:(int)parentCategoryId {
    if((self = [super init])) {
        self.productCategoryId = productCategoryId;
        self.name = name;
        self.children = chidren;
        self.parentCategoryId = parentCategoryId;
    }

    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

这只是一个普通的对象,我做了100000次.问题是,有时此对象的实例返回"0 objects"并有时返回正确的对象.

例如,如果我这样做,它ProductCategory *category = [[ProductCategory alloc]init];有时会返回一个ProductCategory实例,有时会返回,"0 objects"所以我不能为这个对象赋值.

在此输入图像描述

我想它应该是非常愚蠢的东西,但我没有看到它.

zev*_*ito 4

修复方法:

重新启动 XCode。

为什么会发生?:

苹果应该回答这个问题。

使用 XCode 一段时间后,似乎出现了内存垃圾问题。

如果你在那里遇到陷阱,解决方法

@HotLicks 关于使用建议NSLogpo确定该对象的状态是正确的。

expression您还可以在断点后使用调试器窗口中的命令来调用方法并读取相关对象的属性。