开发者

Object properties throw unknown error in some methods

开发者 https://www.devze.com 2023-03-13 05:55 出处:网络
Setup: I have a custom class I\'ve created that handles data retrieved from a web source using ASIHttpRequest. This data is json encoded. In the completion block of the async request , the custom obje

Setup: I have a custom class I've created that handles data retrieved from a web source using ASIHttpRequest. This data is json encoded. In the completion block of the async request , the custom object is allocated to memory and the json values are set to their corresponding properties. This object is property of the view. Within the completion block, a method to setup the view is called and the object is passed into a headerView which is subclassed from UIView and is then set to tableView.tableHeaderView. At this point everything works perfectly. UILabels in the headerView are set using the custom objects properties without a hitch.

Problem: Within the view, I have a UIBarButton that calls a method. When this method is called and I attempt access any property from the custom object, the program crashes (iPhone simulator freezes) and xcode points to the line in which the property is attempting to be accessed and says "Thread 1". There aren't any errors in the debugger. When I NSLog the custom object, there is a memory address returned which matches the address when I don't have any problems accessing it.

Any ideas? Are 开发者_运维技巧their any debugging methods to help work this out? I don't understand why one method of the view doesn't have any problems accessing these properties and another would. Thanks.

Edit: I disabled zombies and I now receive a EXC_BAD_ACCESS error instead of no error at all.

Edit: Code

@class Obj, ObjHeader;
@interface ObjTableViewController : UITableViewController <UIActionSheetDelegate> {

@public
    NSString* objID;
@private
    Obj* obj;
    ObjHeader* headerView;
}
@property (nonatomic, retain) NSString* objID;
@property (nonatomic, retain) Obj* obj;

@property (nonatomic, retain) ObjHeader* headerView;

- (void)loadObj; // Loads Obj via async request
- (void)setupView; // This method runs after a async request is successful 

- (void)viewDidLoad
{   
    [self loadObj];
    [super viewDidLoad];
}

- (void)loadObj {
    // Start request
    ASIFormDataRequest* request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:OBJ_URL]];
    [request setPostValue:self.objID forKey:@"ObjID"];

    // Successful request
    [request setCompletionBlock:^{
        self.obj = [[Obj alloc] initWithString:[request responseString]];
        [self setupView];
    }];

    [request setFailedBlock:^{
        NSError *error = [request error];
        NSLog(@"Error on obj request with error %@",[error localizedDescription]);
    }];

    [request startAsynchronous];
}

    - (void)setupView {
        // Header setup
        self.headerView = [[ObjHeader alloc] initWithObj:self.obj];
        self.tableView.tableHeaderView = self.headerView;



        // Title 
        self.title = self.obj.name; // SUCCESSFUL ACCESS HERE
        UIBarButtonItem* optsButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction 
                                                                                     target:self 
                                                                                     action:@selector(showOpts:)] 
                                       autorelease];

        self.navigationItem.rightBarButtonItem = optsButton;

    }
- (void)showOpts:(id)sender {
    NSLog(@"%@",self.obj.name); // EXC_BAD_ACCES 
}

Obj Class

@interface Obj : NSObject {

@public
    NSString* name;
}

@property (nonatomic, readonly, retain) NSString* name;
- (id)initWithString:(NSString*)responseString;

- (id)initWithString:(NSString*)responseString {
    self = [super init];
    if(self) {
        NSArray* json = [[NSString stringWithString:responseString] JSONValue];
        name    = [json valueForKeyPath:@"Obj.Name"];
    } // END IF
    return self;
}

ObjHeader class - I'm pretty sure this is irrelevant because the problem still occurs when I don't use it! :P

@class Obj;
@interface ObjHeader : UIView {

@public
    UILabel* nameLabel;
}
@property (nonatomic, readonly, retain) UILabel* nameLabel;

- (id)initWithObj:(Obj*)obj;


- (id)initWithObj:(Obj*)obj
{
    CGRect frame = CGRectMake(0, 0, 320.0f, 480.0f / 4.0);
    self = [super initWithFrame:frame];
    if (self) {

        const CGFloat labelWidth = 320.0f - 15.0f;

        // Setup Name label
        const CGFloat nameFontSize = 17.0f;
        CGRect nameFrame = CGRectMake( 70.0f, 10.0f, 
                                      labelWidth, nameFontSize);
        nameLabel = [[UILabel alloc] initWithFrame:nameFrame];
        nameLabel.text = obj.name;
        nameLabel.font = [UIFont fontWithName:SYSFONT size:nameFontSize];
        [self addSubview:nameLabel];
    }
    return self;
}


I think there's a problem here:

 name    = [json valueForKeyPath:@"Obj.Name"];

That's an autoreleased object, so you want either self.name at the beginning or a retain on the end.


It might be a problem of infinite loop. Check whether you have same method name and an instance var? like if you declare an instance var named myValue and create a method named

- (void)setMyValue;

check if this is the case or an infinite while loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消