For Question About a Warning In Objective-c Coding - iphone

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.

Related

object not confirming protocol does not give any warning

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.

Instance method not found (return type defaults to id)

I am taking a warning from Xcode. Here is the code
DeviceList *dList = (DeviceList* )[[User thisUser] devices];
[dList getListId];
The warning states that instance method -getListId is not found. However the method exists in my source code
- (NSString*) getListId
{
T
if ( ... != nil)
{
return ...;
}
else
{
return #"";
}
}
I cannot figure out what the problem is when I am calling the method.
have you added a declaration for this method in the .h file, and if so, have you imported the .h into the file you are trying to call this method?
this error is basically the compiler saying it can't find the method declaration, so it doesn't know what to expect the return type to be.
in your DeviceList.h , make sure you have
#interface DeviceList : Parent
- (NSString*) getListId;
..
..
#end
the warning occurs when your method is not declared in your header file and you try to call it outside your (self) class.

Return type from valueForKeyPath:?

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

warning when using BOOL variable in objective-c

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.

Use of asterisk in variable names

The book "iPhone Programming. The Big Nerd Ranch Guide" cites the following method (page 96)
(void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *) views {
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 250, 250);
[mv setRegion:region animated:YES];
}
I'm confused because of the asterisk usage. The line that begins with "MKAnnotationView" and the following one can be represented in an abstract fashion by:
ObjectType variableName = [object message];
Questions:
In the first case an asterisk precedes the variable name, but not in the second. Why?
In the case where the asterisk is used, should not be the pointer the assigned to nil?
Thanks.
I tend to think of it as what variable types require an asterisk, not what variable names require an asterisk. Objective C doesn't allow you to allocate objects on the stack like so:
// Declare an NSObject. Won't work.
NSObject myObject;
Instead, all objects must be dynamically allocated on the heap using pointers like so:
// Declare a pointer to an NSObject. Will work.
NSObject* myObject = [[NSObject alloc] init];
id is a special Objective C keyword that just means "A pointer to some Objective C object". This may or may not inherit from NSObject and is dynamically typed. What's important to note is that, while there is no asterisk, this is still a pointer to an object:
// Same as before. Will work.
id myObject = [[NSObject alloc] init];
The only difference is that the compiler has no information about what myObject is.
As a finishing note, id <MKAnnotation> is exactly the same as a regular id, but with some extra information for the compiler. Read it as "a pointer to some Objective C object that behaves like an MKAnnotation". MKAnnotation, in this case, is the name of a Protocol whose required methods you are declaring that particular id to implement.
id is already defined as a pointer to a struct. If you look at its definition in objc.h, you would that id is defined as,
typedef struct objc_object {
Class isa;
} *id;
Since it is already a pointer to an objc_object, you can create pointers to objects without using the asterisk as,
id myObject;
Also saying that an object is type id gives the compiler absolutely no information about the object except its class which comes from the isa property.
An NSObject on the other hand is defined as,
#interface NSObject <NSObject> {
Class isa;
}
To create a pointer to an object of NSObject or one of its subclass (such as MKAnnotationView), you would declare it as,
NSObject *myObject;
MKAnnotationView *myObject;
We are putting the asterisk here to denote that it is a pointer.
Specifying the protocol(s) next to the type gives the compiler more information for static-type checking.
You should check out this article for a brief introduction to the differences between id and NSObject. For an in-depth understanding, checkout this article on the Objective-C runtime.
ObjectType is normally something like "pointer to a MKAnnotationView", which is represented in Objective-C as it is in C: "MKAnnotationView *". Exceptions include the "id" type, various integer and floating point types (including their typedefs), enums (which are really integer types), and some small structs like CGRect.