1

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322

1 Answers1

0

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!

Carelinkz
  • 936
  • 8
  • 27
  • This won't work when `event.location == nil.` You should probably check `[string length] == 0`, which works for nil and empty strings. – Richard J. Ross III Jun 18 '12 at 19:32
  • Thanks for the illuminating hints. Unfortunately, while the empty/nil string issue is no doubt also a problem, even outside of any comparison or exception check altogether, I still get the specified build error. that is, with just this statement: "[eachEvent addObject:[event location]];" I get the "incompatible type for argument 1 of AddObj" build error. So… first issue seems to be initializing the eachEvent object properly. I gave the whole code I'm using there, so what am I doing wrong in that respect? thanks. – Gajah Duduk Jun 18 '12 at 21:01
  • ...and in case these thoughts are not what you need: it's always handy to know what is actually in the operands, so try `NSLog (@"operand1: %@",[event location]);`.... – Carelinkz Jun 18 '12 at 21:15
  • also, using the code given by radiopaque above for condition, the error is now "cannot convert to a pointer type" on the "if" line. "the incompatible type" error remains on the addObject statement. – Gajah Duduk Jun 18 '12 at 21:15
  • Try that NSLog thing. I am thinking that the compiler does *not* consider [event location] to be an NSString object! – Carelinkz Jun 18 '12 at 21:18
  • I agree it doesn't consider it to be an NSString object. so i threw in that log line, and the application now crashes. :☺ geez. Can I coerce it somehow? – Gajah Duduk Jun 18 '12 at 21:23
  • Doesn't the compiler think that `event` is a generic pointer (id), i.e., not something with a location property? I am not sure why other properties work fine, but I do think you could safely initialize event as `CalEvent *event`. **Also if you are not printing a pointer, you should not use %@ but %d or something.** In any case, you now know where the problem lies :-) – Carelinkz Jun 18 '12 at 21:26
  • so brilliant! changing id event; to CalEvent *event; worked just perfectly. thank you kindly, good sir! – Gajah Duduk Jun 19 '12 at 10:00