I am trying to initalize my BOOL variable to YES but its giving me this warning.. not quite sure what to do.. it still seems to be working fine but just wondering how I can get rid of the warning.
I have initalize the variable in the header like this
//.h
BOOL *removeActivityIndicator;
//..
#property (nonatomic, assign) BOOL *removeActivityIndicator;
Then I try to set it to YES like so (this is also where I get the warning)
self.removeActivityIndicator = YES;
The warning says :
incompatible integer to pointer conversion passing 'BOOL' (aka
'signed char') to paramater of type 'BOOL *' (aka 'signed char *')
The warning is correct; you've declared the variable as a BOOL * (a pointer to a BOOL), which is almost certainly not what you want. Remove the * from the declaration.
removeActivityIndicator is a char pointer, and you assigns a char to it, so either:
change it to be BOOL removeActivityIndicator;
Dereference it: *(self.removeActivityIndicator) = YES;
You've made a pointer to a BOOL, which is a primitive type. Remove the extra * in front of remoteActivityIndicator.
Related
I have a class "ABC" and its method which returns non autoreleases object of that class.
#interface ABC:NSObject
+(ABC *)aClassMethodReturnsObjectWhichNotAutoreleased;
#end
#implementation ABC
+(ABC *)aClassMethodReturnsObjectWhichNotAutoreleased{
ABC *a = [[ABC alloc]init];
return a;
}
#end
If I have a protocol Foo.
#Protocol Foo
#required
-(void)abc;
#end
My ABC class is "not" confirming Foo protocols.
1st call
id<Foo> obj = [ABC aClassMethodReturnsObjectWhichNotAutoreleased]; //show warning
It shows warning "Non Compatible pointers.." thats good.Abc did not confirm protocol Foo
BUT
2nd call
id<Foo> obj = [NSArray arrayWithObjects:#"abc",#"def",nil]; // It will "not" show warning as it will return autorelease object.NSArray don't confirm protocol Foo
In first call compiler gives warning and in second call compiler is not giving any warning.I think that is because i am not returning autorelease object.
Why is compiler not giving warning in 2nd call as NSArray is also not confirming FOO
Thanks in advance
In your first example, the return value is a specific type so the compiler can verify the assignment.
In the second example, the NSArray arrayWithObjects: method has a return type of id. You can assign an object of type id to a variable of any type. The compiler has no way to verify that what you are doing is truly correct or not.
This issue has nothing to do with autoreleased objects. It's all about the data types. id is a kind of catch-all type that can be anything.
This is probably pilot error on my part, but I am a little confused why this does not return an int (as thats the type of the property identified by the key path). Does valueForKeyPath: return an object instead, can anyone explain.
// Simple Object
#interface Hopper : NSObject
#property(nonatomic, assign) int mass;
#end
// Test
Hopper *hopper = [[Hopper alloc] init];
[hopper setMass:67];
NSLog(#"HOPPER: %d", [hopper valueForKeyPath:#"mass"]);
.
WARNING: Conversion specifies type 'int' but the argument has type 'id'
Yes, it returns an objc object:
- (id)valueForKeyPath:(NSString *)keyPath;
Details for automatic conversions from non-objc objects to objc objects (e.g. NSNumber and NSValue) is covered in Accessor Search Patterns for Simple Attributes.
Therefore, you would use the objc object format specifier %#:
NSLog(#"HOPPER: %#", [hopper valueForKeyPath:#"mass"]);
valueForKeyPath returns an object. int and char types are not objects. Access the property via the . operator or similar.
NSLog(#"HOPPER: %d", [hopper mass]);
NSLog(#"HOPPER: %d", hopper.mass);
Edit: Didn't fully read example code, updated answer
I want to declare properties with ints and bools, for example:
#property(nonatomic,retain) bool signOutgoingFax;
The error I get is:
property 'signOutgoingFax' with 'retain' attribute must be of object type
You do not retain BOOL int or float. Simply use
#property(nonatomic) bool signOutgoingFax;
The point here is that the variable is declared as "BOOL", not "BOOL *" (this would be a pointer), and hence you should not use retain.
I have some code added in viewWillAppear;
curr_rep_date = [tmpRptDt
stringByReplacingOccurrencesOfString:[NSString
stringWithFormat:#"%d",tmpYrVal]
withString:[NSString
stringWithFormat:#"%d",(tmpCurrYearInt-2)]];
When I build, I get the following warning;
warning: incompatible Objective-C types assigning 'struct NSArray *', expected 'struct NSMutableArray * }
Also
warning: assignment makes pointer from integer without a cast for:
replist_rptdt_dict =
PerformXMLXPathQuery(xmlData,
#"//XX/Period[#XX]");
Please let me know the reason.
Thanks.
replist_rptdt_dict = PerformXMLXPathQuery(xmlData, #"//XX/Period[#XX]");
First, the Objective-C standard is to use camel cased english names for variables. replist_rptdt_dict is confusing (it almost sounds like you have a list dictionary something what huh?).
warning: incompatible Objective-C
types assigning 'struct NSArray *',
expected 'struct NSMutableArray *' }
This will happen if you have:
- (NSArray *) foo;
...
{
NSMutableArray *bar = [someObject foo];
}
That is, bar is a more specific type -- a subclass -- than foo's return value. The compiler is complaining because your code is quite likely going to crash if you send, say, removeObjectAtIndex: to what is quite likely an immutable array.
I have one file viewcontroller.h and .m and viewcontroller1.h and .m
In viewcontroller1.m file ,
i write function like BOOL rechable = [viewcontroller functionrechable];
it gives me warning like warning:initialization makes integer from pointer without a cast
how to remove this warning???
is it any way to do it?
This is telling you that your defining reachable as a BOOL type which really resolves to an integer typye, yet the [viewcontroller functionrechable] message is returning a pointer. You can remove the warning either by casting the return type of the function to BOOL or int, or changing the type of reachable to a pointer.
What is the definition for the method [viewcontroller functionrechable]....
The solution is most likely:
BOOL rechable = (BOOL) [viewcontroller functionrechable];
But I would need the definition to be sure.