6

I have a custom object ProductCategory.

.h file:

#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

.m file:

#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

It's just a normal object, I have done this 100000 times. The problem is, sometimes the instance of this object returns "0 objects" and sometimes returns the correct object.

For example, if I do this ProductCategory *category = [[ProductCategory alloc]init]; sometimes it returns a ProductCategory instance, and sometimes it returns "0 objects" so I cannot assign any value to this object.

enter image description here

I guess it should be something really stupid but I don't see it.

Ale
  • 2,282
  • 5
  • 38
  • 67
  • 2
    I just created an empty project with just your code, and it seems to be working perfectly.. Maybe the error is elsewhere ? – rdurand Nov 13 '14 at 09:34
  • 1
    i didn't see errors on your code, try setup breakpoints on constructors, what you call, and how? i thing errors is on above level. – Bimawa Nov 13 '14 at 10:00
  • The weird thing is sometimes it works and sometimes not. For example, when I restart my computer, the first time I run the app it works, but after 1-2 times it doesn't work anymore :/ – Ale Nov 13 '14 at 10:05
  • I think It´s good idea to add, your overrider init to the header (.h) file. Add in the header file: -(id)init; – Onik IV Nov 13 '14 at 10:11
  • I have set a breakpoint on the empty constructor and `self` returns `0 objects`... – Ale Nov 13 '14 at 10:14
  • 1
    Don't rely on the debugger. Either use NSLog or stop at a breakpoint and type "po category" in the console window. – Hot Licks Nov 13 '14 at 13:19
  • 2
    Also, remember that if you have a local variable as an object pointer with no subsequent references, ARC will delete the object pointed to pretty much immediately. – Hot Licks Nov 13 '14 at 13:20
  • Thanks @HotLicks, you are right, i can see the variable values typing "po category" but it doesn't make any sense, this only happens to this object, if I create any other object I can see it on the debugger...it's a bit weird, isn't it? – Ale Nov 13 '14 at 14:45
  • The debugger is weird. It's so untrustworthy that I rarely use it. – Hot Licks Nov 13 '14 at 15:49
  • Also getting this - if you found a solution, would love to hear it! – Rollie Nov 20 '14 at 09:26
  • I'm sorry but I didn't find a solution yet, the debugger it's just not working only for that object :( – Ale Nov 20 '14 at 09:35
  • @Ale I have exactly the same Problem. Did you solve it yet? – davidOhara Jan 13 '15 at 17:05

2 Answers2

4

The way to fix it:

Restart XCode.

Why it happen?:

Apple should answer this question.

It seems a garbage in memory issue after a period of time using XCode.

Workarounds if you get trap there

@HotLicks is right about the advising of use NSLog and po to be sure about the state of that object.

Also you can invoke methods and read properties of the object in question by using expression command in debugger window after a breakpoint.

zevarito
  • 362
  • 4
  • 9
0

zevarito is on the right track. A bit more seems to solve the long-irritating problem:

Close the project.

Xcode -> Window -> Projects For the project in question (and all others is probably a good housecleaning idea), click Derived Data -> Delete.

Close Xcode. Close Simulator.

Restart Xcode and resume what you were doing.

ctd
  • 111
  • 1
  • 5