The compiler doesn’t give me any error and the code runs. Just curious how can I check the contents of my array after adding a new element.
In my .h file
@interface AddCardViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *cardNameTextField;
@end
In my .m file
@interface AddCardViewController ()
@property (nonatomic, strong)NSMutableArray *nameOfCards;
@end
@implementation AddCardViewController
@synthesize cardNameTextField = _cardNameTextField;
@synthesize nameOfCards = _nameOfCards;
- (NSMutableArray *)nameOfCards
{
if (!_nameOfCards)
_nameOfCards = [[NSMutableArray alloc] init];
return _nameOfCards;
}
- (IBAction)addNewCard:(id)sender {
[_nameOfCards addObject:self.cardNameTextField.text];
}
@end
When you use lazy loading (i.e. you create the object in its getter method) you have to use the getter to access the object. Do not access the object through its instance variable!
that’s why you prefix instance variables with an underscore. It tells you to not use the instance variable directly except when it’s absolutely necessary.