3
votes

I have an Obj-C method similar to this:

-(void)getUserDefaults:(BOOL *)refreshDefaults
{
    PostAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    if (refreshDefaults) {
        [appDelegate retrieveDefaults];
    }
}

When I call it like this I get no warning:

[self getUserDefaults:NO];

When I call it like this I get a warning:

[self getUserDefaults:YES];

warning: passing argument 1 of 'getUserDefaults:' makes pointer from integer without a cast

NOTE: I always call the method passing NO first, then sometime later I pass YES

Can anyone fill me in on what the issue is here? Thanks.

5

5 Answers

17
votes

BOOL is a primitive type, not a class, so your method declaration should be

-(void)getUserDefaults:(BOOL)refreshDefaults;

instead. The reason you're getting the warning you are is that 0 (which is what NO is defined to be) is a valid pointer value without a cast, equivalent to NULL, but 1 (which is what YES is defined to be) isn't a valid pointer value without a cast.

15
votes

getUserDefaults takes a BOOL*. You don't get a warning when you pass NO because NO is 0 and 0 is NULL, which is a legal BOOL*. YES is 1 and the same conversion isn't automatically safe.

You should make getUserDefaults take a plain BOOL, instead of a pointer.

5
votes

I have never seen Obj-C code before, but I'm pretty sure it's because the 'refreshDefaults' is a pointer to BOOL whereas it should be a BOOL.

I don't know about the typing rules in Obj-C, but it looks like NO is defined as 0 and the compiler has no problems converting that to a pointer value (a NULL pointer.) That would not be the case with integer '1'. (Also note "if (refreshDefaults)" is valid code that checks the pointer refreshDefaults against the NULL pointer)

3
votes

Cool thanks! That makes sense, and fixed it! Here is the updated code for reference:

-(void)getUserDefaults:(BOOL)refreshDefaults
{
PostAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

if (refreshDefaults) {
    [appDelegate retrieveDefaults];
}
}
1
votes

In the first case, NO probably defaults to 0 - the null pointer. This is a valid Bool*. Yes will default to 1, and so when it is cast to a Bool* you are passing the integer 1 in - which is then turned into a Bool*.