0

I want to create a global array, I was looking in to NSMutableArray because you can index it. I need to be able to call specific lines of the array and display via a label the 1st part of the array and compare users input with the second part.

Example is: User sees, press "x" (this is the first part of the array) and if the click one button it will compare that button id to the second part of the array.

Im not familiar with arrays in objective C, links or code snips will help!

2

3 Answers 3

1

It sound like a bit like you have a question answer format...

What you could do is have an NSArray of Question the objects that may look something like

@interface PSQuestion : NSObject

@property (nonatomic, copy)   NSString *title;
@property (nonatomic, assign) NSInteger answer;

@end

@implementation PSQuestion

@synthesize title  = _title;
@synthesize answer = _answer;

@end

Nw you can have an indexed array of objects that contain both components you require

// Configure questions
PSQuestion *question1 = [[PSQuestion alloc] init];
question1.title  = @"Click X";
question1.answer = 2;
Sign up to request clarification or add additional context in comments.

1 Comment

is there a way, i can make the question number a int? so like PSQuestion *question[num] I need to have a big data set and randomize the question. and How can I make it global?
0

I need to be able to call specific lines of the array and display via a label the 1st part of the array and compare users input with the second part.

No problem! NSArray or it's mutable counterpart can store strings just as easily as any other object. It's as simple as [mymutablearray addObject:@"mystring"];.

User sees, press "x" (this is the first part of the array) and if the click one button it will compare that button id to the second part of the array.

To compare two arrays use [_array1 isEqualToArray:array2];, to compare strings within an array to a button label, use

[[array1 objectAtIndex:0]isEqualToString:myButton.titleLabel.text];

2 Comments

for comparing, I need to have like x and y linked somehow, thats why I dont want to have two different arrays to compare
An NSDictionary then? You have an object and a key.
0

Looks like you only need a one dimensional array of objects that have two elements. Define a class that has the two parts of which you speak, and then make an NSMutableArray full of instances of that class.

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.