views:

112

answers:

1

Hi

I have a bunch of NSStrings in a NSMutableArray. I have added them to the array and now I want to loop through the array, getting each object in it (the strings) and set them to a common value @"No Lessons".

This is how I have the loop so far: But how can I get the object then set it? Currently its fairly simple, just getting looping through the array, not changing anything.

NSInteger *count = [monLessonArrayA count];
    for (int i = 0; i < count; i++) {
        [monLessonArrayA objectAtIndex:i];
    }

Any help much appreciated, thanks.

EDIT:

Turns out there is a larger issue somewhere. This is the code I am using:

NSMutableArray* lessonArrayFuncTwo(id a, id b, id c, id d, id e, id f) {
    NSMutableArray* lessonsArray = [[NSMutableArray alloc] init];
    [lessonsArray addObject:a];
    [lessonsArray addObject:b];
    [lessonsArray addObject:c];
    [lessonsArray addObject:d];
    [lessonsArray addObject:e];
    [lessonsArray addObject:f];
    return lessonsArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

monLessonArrayA = lessonArrayFuncTwo(monP1A, monP2A, monP3A, monP4A, monP5A, monP6A);
    monLessonArrayB = lessonArrayFuncTwo(monP1B, monP2B, monP3B, monP4B, monP5B, monP6B);
    tueLessonArrayA = lessonArrayFuncTwo(tueP1A, tueP2A, tueP3A, tueP4A, tueP5A, tueP6A);
    tueLessonArrayB = lessonArrayFuncTwo(tueP1B, tueP2B, tueP3B, tueP4B, tueP5B, tueP6B);
    wedLessonArrayA = lessonArrayFuncTwo(wedP1A, wedP2A, wedP3A, wedP4A, wedP5A, wedP6A);
    wedLessonArrayB = lessonArrayFuncTwo(wedP1B, wedP2B, wedP3B, wedP4B, wedP5B, wedP6B);
    thuLessonArrayA = lessonArrayFuncTwo(thuP1A, thuP2A, thuP3A, monP4A, thuP5A, thuP6A);
    thuLessonArrayB = lessonArrayFuncTwo(thuP1B, thuP2B, thuP3B, thuP4B, thuP5B, thuP6B);
    friLessonArrayA = lessonArrayFuncTwo(friP1A, friP2A, friP3A, friP4A, friP5A, friP6A);
    friLessonArrayB = lessonArrayFuncTwo(friP1B, friP2B, friP3B, friP4B, friP5B, friP6B);

    NSInteger count = [monLessonArrayA count];
    for (int i = 0; i < count; i++) {
        [monLessonArrayA replaceObjectAtIndex:i withObject:@"test"];
    }
}

So now here I am using a function to simply put the strings into several arrays, then it comes back tot he loop, where it loops through the arrays and puts the text into the objects. Can you see any issues?

Upon loading the app crashes giving a SIGBRT error.

+5  A: 

Rather than figure out how to replace objects at particular indexes in a mutable array, I think you should take a step back and reconsider the design of your application.

It appears that you're trying to design an application that manages "lessons" for a week, and are trying to do it by brute-force: Representing the week's lessons as a collection of arrays of strings.

Rather than doing it that way, you should probably consider what alternative representations you could use to simplify the application. For example, rather than represent a particular day's lessons as strings in particular arrays at particular indexes, you could instead represent scheduled lessons as instances of a Lesson class that not only has the name of the lesson, but also its day and whether it's a morning or afternoon lesson:

typedef enum {
    LessonMonday=1,
    LessonTuesday=2,
    LessonWednesday=3,
    LessonThursday=4,
    LessonFriday=5,
} LessonDay;

typedef enum {
    LessonSessionA=1,
    LessonSessionB=2,
} LessonSession;

@interface Lesson : NSObject
@property (readwrite, copy) NSString *title;
@property (readwrite) LessonDay day;
@property (readwrite) LessonSesson session;

- (id)initWithTitle:(NSString *)title day:(LessonDay)day session:(LessonSession)session;
@end

Now you only need to represent lessons that are actually scheduled, say in a scheduledLessons collection. Your user interface can tell when a lesson is scheduled and render that session as empty without you having to "fill in" an empty lesson; if you want to later schedule a lesson for a particular slot, just create a new instance of Lesson to represent the lesson in that slot and store it. This also makes it easier to do things like change an existing lesson's slot without shoving data all around: You can just change the title of a lesson, or the day of a lesson, or the session, without disturbing other lessons. (Obviously in something like this you would want to check for conflicts as well, but again, that's straightforward.)

After you understand the basics of how to model something like this in code I'd strongly urge you to take a look at using Core Data for it rather than doing it by hand. Once you start dealing with things like persistence and searching, it will just be much easier to have Core Data actually implement the bulk of your data model for you than either doing it the way you showed in your question, or the way I'm showing you above. For example, using Core Data I'd probably model this as three entities — Lesson, Day and Session — where a Day is associated with two Sessions and a Session is associated with a Lesson.

Chris Hanson