objective-cstructnsmutableset

How to reference objects in NSMutableSets


I have a small C# library that I am trying to port (actually re-writing the C# code using it as a guide) to Obj-C. The purpose of the library is to generate recurring appointments for an iOS calendar app. I'm having problems porting C# structs to Obj-C objects. This is what I have for one of the structs that holds the appointment info:

@interface Appointment : NSObject  {
@public
    NSDate *apptStartTime;
    NSDate *apptEndTime;
    NSString *key;
}
@end

One of the methods I wrote accepts a date, a set of schedules (also a port of a C# struct) and the appointment's list (I'm using a NSMutableSet which contains the Appointment interface above). If I can get the appointments method working, I'm pretty sure I can figure out the remainder (I think). This is the code that adds appointments to the NSMutableSet:

-(void) addAppointmentsForDate:(NSDate *)checkDate andSchedules: (NSMutableSet *)schedules andAppt:(NSMutableSet *)appointments {

Appointment *appt = [[Appointment alloc]init];

for(NSMutableSet *schedule in schedules)  {

    if(schedule.occursOnDate(checkDate))   {
        appt = [self generateAppointment:checkDate andSchedule: [schedules removeObject:schedules]];
        [appointments addObject: appt];

        }
    }
}


-(Appointment *) generateAppointment: (NSDate *) checkDate andSchedule: (Schedule *) schedule  {

Appointment *appt = [[Appointment alloc]init];

appt->apptStartTime = schedule->timeOfAppointment;
appt->apptEndTime = nil;  //  TODO  
appt->key = schedule->key;

return appt;

}

I'm getting build errors on the if statement:

Sending 'void' to parameter of incompatible type 'Schedule *'

I have never used NSMutableSets before, nor have I tried to port from C# before. I'm having a time with the port of the C# struct's, as you can see. I have read all of the Apple docs on sets, and several docs that explain the differences between C# and Obj-C.

Can somebody please either explain what I'm doing wrong, or point me to some good docs that can give me an example of referencing elements within sets?


Solution

  • Instead of this:

    @interface Appointment : NSObject  {
    @public
        NSDate *apptStartTime;
        NSDate *apptEndTime;
        NSString *key;
    }
    @end
    

    please please please write

    @interface Appointment : NSObject
    @property (readwrite, nonatomic, strong) NSDate* startTime;
    @property (readwrite, nonatomic, strong) NSDate* endTime;
    @property (readwrite, nonatomic, strong) NSString* key;
    @end
    
    1. Don't make instance variables public. Instance variables should never, ever be accessed directly outside code belonging to that class.

    2. Always start instance variables with an underscore character, like _startTime. That way any access to an instance variable stands out. (The code above will create instance variables for you).

    3. Use accessors unless you have a very, very good reason not to.