2

I would like to define some constants and was thinking of using a #define construct, as follows:

#define kUpdateTeamNotification CFSTR("kUpdateTeamNotification")

My issue is that when I go to use it:

[[NSNotificationCenter defaultCenter] postNotificationName:kUpdateTeamNotification object:team];

I'm getting an Incompatible pointer types warning. I was under the impression CFSTR is essentially the same as @"" strings. Am I wrong in my understanding?

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Aaron Bratcher
  • 6,051
  • 2
  • 39
  • 70

1 Answers1

6

CFString and NSString are toll-free bridged so they are the same thing. (CFSTR is a macro to create a CFString). However you have to explicitly signal this to the compiler as the pointers have different types. Moreover, in ARC you will have to use a bridged cast, as you are crossing the boundaries between objects and C structs.

Here's how you use a bridged cast

[[NSNotificationCenter defaultCenter] postNotificationName:(__bridge NSString *)kUpdateTeamNotification object:team];

More info on bridged casts can be found here: NSString to CFStringRef and CFStringRef to NSString in ARC?


However you might want to use a NSString literal as opposed to a CFStringRef and also to use a NSString *const (as explained in Constants in Objective-C) as opposed to a #define.

So your constant would become

Header file (.h)

FOUNDATION_EXPORT NSString *const kUpdateTeamNotification;

Implementation file (.m)

NSString *const kUpdateTeamNotification = @"kUpdateTeamNotification";
Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • "you have to signal this to the compiler as you are crossing the boundaries between ARC managed objects and C structs." - sorry for the nitpicking, but that's not quite related to ARC. Rather, you have to always do the cast no matter which memory management technique you use, because there are two incompatible pointer types being used. –  Oct 17 '13 at 18:58
  • Yes, I totally oversimplified my statement. The bridged cast is needed for ARC, whereas a cast is needed in any case. I'll edit my post to be more precise – Gabriele Petronella Oct 17 '13 at 19:00