-1

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
4
  • You have mentioned in your question about "how to add text in array" but in last you said all works fine and you want to check something.....what exactly you want to do??
    – Krunal
    Commented Apr 6, 2012 at 7:27
  • @Goti I want to check the contents of my array after clicking my UIButton.
    – Hans
    Commented Apr 6, 2012 at 7:35
  • @elppa thanks for editing the layout. How did you do it look much better? :)
    – Hans
    Commented Apr 6, 2012 at 7:57
  • Select your code and click on code sample button like {}
    – Deepesh
    Commented Apr 6, 2012 at 7:59

4 Answers 4

1

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!

- (IBAction)addNewCard:(id)sender {
    [self.nameOfCards addObject:self.cardNameTextField.text];
    NSLog(@"my array content: %@", self.nameOfCards);
}

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.

0
1

Change

   - (IBAction)addNewCard:(id)sender {
       [_nameOfCards addObject:self.cardNameTextField.text];
   }

to

   - (IBAction)addNewCard:(id)sender {
       [self.nameOfCards addObject:self.cardNameTextField.text];
   }

after that, you can see your array in NSLog like;

   NSLog(@"%@", self.nameOfCards);
1
- (IBAction)addNewCard
{
    [your_Array addObject:self.Your_textfeild.text];
}

Array should be NSmutable Array.

0

use NSLog to see the contents of the array i.e. NSLog(@"%@",_nameOfCards);

7
  • I've placed this code inside my IBAction: NSLog(@"contents of my array %@", _nameOfCards); yet the console says, contents of my array = (null)
    – Hans
    Commented Apr 6, 2012 at 7:30
  • did you alloc and intialize the array in the viewDidLoad?
    – adi27
    Commented Apr 6, 2012 at 7:31
  • you know of any reason why it gives me a null value?
    – Hans
    Commented Apr 6, 2012 at 7:36
  • ohh, ahm I forgot to include this in my post. This one is included in my .m file - (NSMutableArray *)nameOfCards { if (!_nameOfCards) _nameOfCards = [[NSMutableArray alloc] init]; return _nameOfCards; }
    – Hans
    Commented Apr 6, 2012 at 7:36
  • @Hans where are you calling nameOfCards from? Commented Apr 6, 2012 at 7:45

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.