0

I have a Save method that is supposed to save information entered in the text fields of a view controller. The method properly saves this info inside the Class variables, but I want to save the object in an array. When I try to do this , I get an error:

Use of undeclared identifier 'homeworkAssignment'; did you mean '_homeworkAssignment'?

Here is the save method

- (IBAction)Save:(UIButton *)sender {
self.homeworkAssignment = [[Homework alloc] init];
self.homeworkAssignment.className = self.ClassNameField.text;
self.homeworkAssignment.assignmentTitle = self.AssignmentTitleField.text;
self.homeworkAssignment.assignmentDiscription = self.DiscriptionTextView.text;
self.homeworkAssignment.pickerDate = self.DatePicker.date;

NSArray *MyHomeworkArray = [[NSArray alloc]initWithObjects:homeworkAssignment, nil];
                                                           //Error here ^
}

Can anyone tell me why I am able to set the homeworkAssignment object variables but get an error when trying to store the object in the array?

1
  • 2
    Do you know what self. means? Can you see the difference in how you are using homeworkAssignment on the lines which work and the one that doesn't? Commented Oct 9, 2013 at 20:49

2 Answers 2

2

You have to use self.homeworkAssignment. That is the property you've already defined and have used on every other line in that method.

Sign up to request clarification or add additional context in comments.

Comments

2

Your previous references to homeworkAssignment was through the homeworkAssignment property. Eg:

self.homeworkAssignment is equal to [self homeworkAssignment] self.homeworkAssignment = something is equal to [self setHomeworkAssignment:something]

As your property is auto synthesized, the ivar that backs is it has a leading underscore, eg _homeworkAssignment

Most of the time, you want to use the property to access the ivar, that is use self.homeworkAssignment or [self homeworkAssignment]. Notable exceptions are in your init and dealloc methods where you want to avoid any side effects of calling methods.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.