MrNaitX
MrNaitX

Reputation: 21

Trying to access data from an array and print it out

I have made an array which I store 5 strings and 1 int into which I then store in to another array.

I'm trying to access and print out an array but it only give me this:
2016-01-11 18:47:55.429 quizgame-chrjo564[3378:145727] (null)

I've tried these alternatives:

NSLog(@"%@", [dataArray objectAtIndex:0]);

NSLog(@"%@", dataArray[0]);

Here is all my code:

#import "ViewController.h"

@interface ViewController ()
{
    NSMutableArray *_questions;
}

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self quizStart];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepQuestions {
    [self question:@"Vad heter jag?" answer1:@"Anton" answer2:@"Christian" answer3:@"Christoffer" answer4:@"Simon" correctAnswer:2];




}

- (void)question:(NSString *)q answer1:(NSString *)a1 answer2:(NSString *)a2 answer3:(NSString *)a3 answer4:(NSString *)a4 correctAnswer:(NSInteger)c {
    NSArray *tmpArray = [NSArray arrayWithObjects:
                         [NSString stringWithString:q],
                         [NSString stringWithString:a1],
                         [NSString stringWithString:a2],
                         [NSString stringWithString:a3],
                         [NSString stringWithString:a4],
                         [NSNumber numberWithInteger:c],nil];
    NSLog(@"%@", q);
    [_questions addObject:tmpArray];

}

- (void)quizStart {

    [self prepQuestions];
    NSArray *dataArray = [_questions objectAtIndex:0];

    NSLog(@"%@", [dataArray objectAtIndex:0]);

}

@end

Thanks in advance

*Updated with error after change:

2016-01-11 19:28:00.816 quizgame-chrjo564[3901:202243] - [__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance 0x75030
2016-01-11 19:28:00.822 quizgame-chrjo564[3901:202243] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [__NSCFConstantString objectAtIndex:]: unrecognized selector sent to instance 0x75030'

Upvotes: 0

Views: 29

Answers (1)

rmaddy
rmaddy

Reputation: 318804

You never initialize _questions.

Change this:

[_questions addObject:tmpArray];

to:

if (!_questions) {
    _questions = [NSMutableArray array];
}
[_questions addObject:tmpArray];

Also, here's a suggestion to make your code cleaner and easier to read.

  1. Don't needlessly use stringWithFormat:.
  2. Use modern syntax for arrays and dictionaries.
  3. Use modern syntax for number boxing.

In other words, you question:... method can be written as:

- (void)question:(NSString *)q answer1:(NSString *)a1 answer2:(NSString *)a2 answer3:(NSString *)a3 answer4:(NSString *)a4 correctAnswer:(NSInteger)c {
    NSArray *tmpArray = @[ q, a1, a2, a3, a4, @(c) ];
    NSLog(@"%@", q);
    if (!_questions) {
        _questions = [NSMutableArray array];
    }
    [_questions addObject:tmpArray];
}

Upvotes: 2

Related Questions