views:

12080

answers:

6

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  A: 

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)

aib
+1  A: 

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*.

Nathaniel Flath
+14  A: 

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.

Chris Hanson
Pointers to primitives are perfectly legal.
Andrew Medico
Yes, but he's not passing in a pointer to BOOL, he's just passing in a BOOL, which is why he's getting that error.
dancavallaro
+12  A: 

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.

Andrew Medico
+2  A: 

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];
}
}
macinjosh
Couple of quick things: 1) you should edit your original question if you want to show working code vs. non-working; this isn't a forum. Second, if somebody helped you, you should give them credit by accepting their answer :)
Jason Coco
A: 

Hi, In Objective-c, I have one function which accepts int as parameter, viz: -(void)Sum:(int)i{ Sum = i+10; }

where 'Sum' is static variable declared globally, int he same .m file. now, I am calling this 'Sum' function from some other function as follows: -(void)Funct{ int i = 10; [self Sum:i]; }

After [self Sum:i]; i get warning saying: "assignment makes integer from pointer without a cast"

I am not getting where is the mistake. Please let me know as soon as possible.

Thanx.

Pravara
That's not an answer to this question. You should post it as its own question.
Peter Hosey