-1

I have a class, called Schedule, with 4 properties in it:

@interface Schedule : NSObject  {
}

@property (strong, nonatomic) NSDate *apptStartTime;
@property (strong, nonatomic) NSDate *apptEndTime;
@property (strong, nonatomic) NSString *clientKey;
@property (strong, nonatomic) NSString *scheduleName;

-(BOOL) occursOnDate: (NSDate *) dateOfAppointment;
@end

I have another class where I have previously created two "Schedule" objects (for testing) with data and put both in a NSMutableSet. This is the code for that:

    //  schedule1
Schedule *schedule1 = [[Schedule alloc]init];
schedule1.apptStartTime = scheduleStartDate;  //  today's date
schedule1.apptEndTime = [scheduleStartDate dateByAddingTimeInterval:60*60];  //  add 1 hour
schedule1.clientKey = @"GeoffMarsh3911";
schedule1.scheduleName = @"schedule1";

//  schedule2
Schedule *schedule2 = [[Schedule alloc]init];  //  two days after schedule1
schedule2.apptStartTime = [scheduleStartDate dateByAddingTimeInterval:timeInterval*2];  //  add two days to first appointment
schedule2.apptEndTime = [schedule2.apptStartTime dateByAddingTimeInterval:60*60];  //  add 1 hour tpo start of appointment
schedule2.clientKey = @"KellieJellyBelly3878";
schedule2.scheduleName = @"schedule2";

//  create NSMutableSet for schedule objects
NSMutableSet *setOfSchedules = [NSMutableSet setWithCapacity:5];
[setOfSchedules addObject:schedule1];
[setOfSchedules addObject:schedule2];

Now, I have another class where I try to take the schedules that are in the NSMutableSet and put them in a NSArray so I can work with them, like this:

    NSArray *scheduleList = [setOfSchedules allObjects]; 

enter image description here

The problem is I don't seem to be able to get to the fields within the scheduleList, like apptStartTime, so I can work with it (compare against another date, etc).

Is there a better way of doing this, rather than putting the NSMutableSet in a NSArray? And how do I access the individual fields in the NSArray?

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • 1
    this doesnt make any sense... a) how do you work with when it is a set? & b) a set has no order so schedul1 can be schedule2 or vice versa (once in the set) & c) why the set at all – Daij-Djan Mar 14 '14 at 23:39
  • That's a very good question... I am trying to port a C# library to Obj-C; the C# code used a *struct* for which there is no Obj-C equivalent. So I wound up making each *struct* (there are 3 of them) into classes. I didn't think that I could use an array to hold the schedule classes (don't know what I was thinking at the time)... thanks for the tips. – SpokaneDude Mar 14 '14 at 23:55

2 Answers2

1

Something like this could work:

for (Schedule *schedule in setOfSchedules) {
    NSDate *startTime = schedule.apptStartTime;
    // rest of your stuff
}

Or use the array and change the for loop to use scheduleList.

Schedule *schedule = scheduleList[someIndex];
NSDate *startTime = schedule.apptStartTime;
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks Rick... removed the NSMutableSet and put each of the classes into a NSMutableArray... much cleaner and easier to read. But thanks for the help... I always appreciate it... – SpokaneDude Mar 14 '14 at 23:57
1

You need to get the object from the NSArray like this:

Schedule *schedule = [scheduleList objectAtIndex:0];

And then access the properties of the schedule object as usual.

myTime = schedule.apptStartTime;

Although it doesn't make much sense having your objects both in the NSMutableSet and the NSArray. Maybe you could get rid of one of them?

Merlevede
  • 8,140
  • 1
  • 24
  • 39