Calendar Store event location to array object

355 Views Asked by At

This is my first post here. I am only a little experienced with Obj-C. I develop in the very deprecated AppleScript Studio in XCode 3 on Lion, but have created a few Obj-C libraries for use in my software. These libraries are designed to pass values from and back to AppleScript. One of these accesses the Calendar Store. Forgive me if my terminology is hopelessly off. :)

The problem is with trying to put the "event location" into an object in a mutable array which can be returned to AppleScript. I can get most every property of an event in this way successfully, other than the location.

Here's the (partial) code, with the build errors I'm getting in comments:

NSEnumerator *eventEnumerator = [[calStore eventsWithPredicate:predicate] objectEnumerator];
   id event;
   while (event = [eventEnumerator nextObject]) {
NSMutableArray *eachEvent = [NSMutableArray arrayWithCapacity:10];


// the following, and most other properites not included here, works fine:

if ([event notes] == nil) {
[eachEvent addObject:[NSString stringWithFormat:@""]];
}
else
{
[eachEvent addObject:[event notes]]; //#06
}



@try {
if ([event location] == nil {
// error:
// invalid operands to binary == (have 'CGFloat' and 'struct NSNull *')

// incompatible type for argument 1 of 'addObject:'


[eachEvent addObject:[NSString stringWithFormat:@""]];
}
else
{
[eachEvent addObject:[event location]]; //#10
// error: incompatible type for argument 1 of 'addObject:'
}
}
@catch(NSException *exception) {
[eachEvent addObject:[NSString stringWithFormat:@"-10-"]];  // #10 dummy for location 
}

// now add all properties of this event (eachEvent) to all events information (eventInformation)
[eventInformation addObject:eachEvent];
   }
   return eventInformation;
}

I tried instead of if ([event location] = nil { to use if ([event location] == [NSNull null]) with the same error.

Can anyone shed any light on this, hopefully with correct code?

1

There are 1 best solutions below

8
On

You should try to avoid testing an NSString against NULL, nil or whatever. This is mostly something you'd use in C or C++. In objective C, you could do:

...
if ( [ [event location] IsEqualToString:@""] ) {
...

However, this will only work if [event location] is not equal to nil as pointed out by Richard, below. In objective-C, no error is raised when you try to run something like [nil IsEqualToString:@""], it just returns nil. The if/then will therefore not be executed. His alternative is safer:

...
if ( [ [event location] length ] == 0 ) {
...

PS: See the docs on the apple web site or your XCode for information about the IsEqualTo range of methods. You can compare a string against a whole lost of interesting other things.

PPS: Another thing may be that you are using [eachEvent location] on an event that has not had the location property initialized. This is, really the situation where Richard's correction comes in handy.

Let me know if it still does not work!