2

Whenever I try to add something to my array in the method nothing gets added to the array. I'm wanting it to when you press the button it will add a new object to the array.

//  JBNumberGeneration.h

#import <Cocoa/Cocoa.h>


@interface JBNumberGeneration : NSObject {
 IBOutlet NSTextField *displayLabel;
 int randNum;
 int level;
 int i;
 NSMutableArray* userNumSequence;
}

-(IBAction)logSequenceNumber:(id)sender;

@end


//  JBNumberGeneration.m

#import "JBNumberGeneration.h"


@implementation JBNumberGeneration

-(IBAction)logSequenceNumber:(id)sender{
  NSString *titleOfButton = [sender title];
 int sequenceNumber = [titleOfButton integerValue];

 int count = [userNumSequence count];
 i++;

 [userNumSequence addObject:[NSNumber numberWithInteger:sequenceNumber]];

 NSLog(@"Array size: %i", count);
    }
    @end

1 Answer 1

2

Have you initialized the array somewhere using something like userNumSequence = [NSMutableArray arrayWithCapacity:0]; I could not see that in your code as that would cause nothing to get added.

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

5 Comments

Ok I added it in but it seems to create the array and then delete after the function is finished.
Added it in after "int sequenceNumber = [titleOfButton integerValue]; "
initialize the array in init for the class JBNumberGeneration
- (id) init { if (self = [super init]) { userNumSequence = [NSMutableArray arrayWithCapacity:0]; } return self; }
Thank you for your help, I'm quite new to objective-C.

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.