Imme22009
Imme22009

Reputation: 4129

Objective c: setting an object attribute ; attribute not updating

Wow. I have had a total mental failure this morning stuck on this 101 problem.

In ViewController, I have this code. But after it executes, the value of [proposalInfo expanded] is still NO. Can somebody see what I'm doing wrong?

- (void)showFullProposal:(id) sender {

// update proposalinfo
ProposalInfo *proposalInfo = [self.proposalInfoArray objectAtIndex:index.section];
[proposalInfo setExpanded:YES];

}

The variables are declared as follows:

ViewController:

@interface ViewController()

@property (nonatomic, strong) NSMutableArray* proposalInfoArray;

@end

ProposalInfo.h:

@interface ProposalInfo : NSObject

@property (assign) BOOL expanded;

@end

ProposalInfo.m:

@synthesize expanded;

Please help!!

Upvotes: 0

Views: 124

Answers (2)

Rob
Rob

Reputation: 437422

If you never alloc/init your proposalInfoArray array, you could experience behavior like this (i.e. get no error, but always get NO back because when you send a message to a nil object, you get nil back). If not precisely this, it's going to be something simple like that. Check proposalInfoArray and make sure it's not nil. Also check the proposalInfo object you got back, make sure it's not nil.

To illustrate your likely problem, this reproduces the behavior you describe (e.g. expanded looks like it's NO, regardless, but you still don't get any exception):

self.proposalInfoArray = nil;                   // This obviously won't work

[self.proposalInfoArray addObject:[[ProposalInfo alloc] init]];

ProposalInfo *proposalInfo = [self.proposalInfoArray objectAtIndex:0];
NSLog(@"before=%d", proposalInfo.expanded);      // OK, IT'S "0"
proposalInfo.expanded = YES;
NSLog(@"after=%d", proposalInfo.expanded);       // HEY, IT'S STILL "0" -- BAD!

Whereas this works properly:

self.proposalInfoArray = [[NSMutableArray alloc] init];

[self.proposalInfoArray addObject:[[ProposalInfo alloc] init]];

ProposalInfo * proposalInfo = [self.proposalInfoArray objectAtIndex:0];

NSLog(@"before=%d", proposalInfo.expanded);      // OK, IT'S "0"
proposalInfo.expanded = YES;
NSLog(@"after=%d", proposalInfo.expanded);       // HEY, IT'S NOW "1" -- GOOD!

In terms of how to identify these issues in the future, use NSAssert. We would have found this problem if we had the following line of code before the objectAtIndex line:

NSAssert(self.proposalInfoArray, @"proposalInfoArray must be initialized!");

or, after the objectForIndex:

NSAssert(proposalInfo, @"proposalInfo must not be nil!");

The nice thing about NSAssert statements is that you can put them in your code, and when you build for debugging, they help you find your program logic mistakes, but when you build your final release version, they're automatically omitted, making your code more efficient. So, use NSAssert liberally!

Upvotes: 3

Mohit_Jaiswal
Mohit_Jaiswal

Reputation: 840

Imme, the following line seems to be strange:

ProposalInfo *proposalInfo = [self.proposalInfoArray objectAtIndex:index.section];

Actually, what do you have in your array, means in proposalInfoArray. Have you checked your object?

Upvotes: 2

Related Questions