0

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?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • Setting ivars directly through `->`? Yeah, no; don't do that. Totally breaks encapsulation and is completely nonstandard out of a very narrow use case. – bbum Mar 07 '14 at 23:42
  • That's what the example of converting structs showed... so I followed it... it works, but if there is a better way, please show me... – SpokaneDude Mar 07 '14 at 23:46

2 Answers2

0

Looking at the code, I have the impression that you are not quite sure whether you are writing C++ or Objective-C code.

if(schedule.occursOnDate(checkDate))

How do you call an Objective-C method again?

for(NSMutableSet *schedule in schedules)

Are you sure about this? schedules is an NSMutableSet*. So you are saying that the elements of your schedules mutable set are again mutable sets? That is quite an unusual thing to have, but if you say so...

appt = [self generateAppointment:checkDate andSchedule: [schedules removeObject:schedules]];

This is more than weird. What do you think is

[schedules removeObject:schedules]

going to do? You expect a set to have itself as an element?

My advice: Go to bed. Have ten hours of sleep. After a good breakfast, have a look at your code again.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • re the sleep... good idea... been too close to this for too long... and I do know better about calling Obj-C methods... caught up in trying to figure out what the C# code was doing and wasn't thinking...thank you for your time, I appreciate it. SD – SpokaneDude Mar 07 '14 at 23:49
0

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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Ahhh... makes perfect sense... and I don't think I'll have any problems accessing the properties this way...BTW, I normally @synthesize my properties because I don't like using the underscore... just personal preference. anyway thank you... I'm gonna work on it tomorrow and I'll get back to you then. – SpokaneDude Mar 08 '14 at 00:04