Errors whenever I access a property - objective-c

I have a class JWGeoGame with these properties:
#interface JWGeoGame : NSObject{
JWCountry *hidden,*last,*prev;
}
#property (nonatomic) JWCountry *hidden;
#property (nonatomic) JWCountry *last;
#property (nonatomic) JWCountry *prev;
//some methods
#end
Whenever I try to set any of them, however, I get a cryptic error:
Implicit conversion of Objective-C pointer type 'Class' to C pointer type 'struct objc_class *' requires a bridged cast.
This error occurs in ech of the following scenarios:
self.prev=self.last;
self.prev=nil;
When I try to use them for comparisons:
if (guess == self.hidden)
return FOUND;
I get another error:
Member reference type 'struct objc_class *' is a pointer, did you mean ->?
but I get this error when I try -> instead:
Member reference base type 'Class' is not a structure of union.
Clearly, I'm doing something wrong here, but I can't figure out what, no matter how many examples I look at. How can I fix this?
EDIT:
JWCountry is currently just a skeleton:
#interface JWCountry : NSObject{
NSInteger index;
}
#property NSInteger index;
#end

You're possibly using dot syntax to get at properties from a class method, but self inside a class method is the actual class object itself, not an instance of the class, and hence doesn't have those properties.
To fix it, make sure that you only access properties of self in instance methods. If you need something like class level storage, use static variables.

Related

How to do pointer work with accessor methods in Objective-C

Basic problem statement:
I have a very good reason for doing some pointer fanciness in an app where I need to pass a decimal by reference.
So I have a class which stores many a decimal, so let's say is has a property as such:
#property (nonatomic) double myDecimalValue;
I want to pass it by reference to some other class.
[someOtherObject sendMyDecimalByReference:&myDecimalValue];
But, a problem emerges! The way that actually has to be written (because it's a property) is
[someOtherObject sendMyDecimalByReference:&decimalOrigin.myDecimalValue];
This fails to compile in objective-c
I get around it by writing the following
- (double *) myDecimalValueRef;
[someOtherObject sendMyDecimalByReference:[decimalOrigin myDecimalValue]];
Except I have dozens of these decimals and I don't want to write that stupid wrapper function for every value.
Is there a shorthand way to do this in Objective-C using just the Getter functions?
Let's just assume I have a great reason for not using NSNumber.
Thanks!
Can you use and access an instance variable directly instead of a property? Then it would work the same way as a C struct member...
#interface SomeClass : NSObject
{
#public
double myDecimalValue;
}
// Keep the getter or not, assuming you synthesize it in a way that uses the
// myDecimalValue ivar
#property (nonatomic) double myDecimalValue;
#end
Then you could access it this way:
[someOtherObject sendMyDecimalByReference:&decimalOrigin->myDecimalValue];
Of course, the robustness is limited, someOtherObject has to remain a valid object when you actually dereference the value, and if you have subclasses/superclasses, you will have to take this design into account...
While far from ideal, you could make the ivars public. I hate even saying that, but it is a possible solution.
Another option is to create a pair of properties for each decimal. Create regular property and also a reference property:
#interface Foo : NSObject
double _myDecimal;
#property (nonatomic, assign) double myDecimal;
#property (nonatomic, assign) double *myDecimalRef;
#end
#implementation Foo
#synthesize myDecimal = _myDecimal;
- (double *)myDecimalRef {
return &_myDecimal;
}
- (void)setMyDecimalRef(double *)val {
_myDecimal = *val;
}
#end
Now you can things like:
[someOtherObject sendMyDecimalByReference:decimalOrigin.myDecimalRef];
Or something like this:
*decimalOrigin.myDecimalRef = 3.14;
Have you looked at the NSValue class? It allows you to pass pointers around as objects.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSValue_Class/Reference/Reference.html

Objective C: Compiler warning with enum

In myObject.h:
typedef enum {
GET,
POST
} HTTPMethods;
And then inside the #interface definition, a property:
#property (nonatomic) HTTPMethods *httpMethod;
In myClass.m, I have the #import of myObject.h and then:
myObject *obj = [[myObject alloc] init];
obj.httpMethod = POST;
This seems to work, but the compiler yells at me:
`Incompatible integer to pointer conversion assigning to 'HTTPMethods *' from 'int'.
Where am I going wrong here?
An enum is a built-in type, and not an object. As such, you probably want to store the integral value itself and not a pointer.
#property (nonatomic, assign) HTTPMethods httpMethod;
There's a big hint in the error message!
In C and Objective C, an enum is a user defined data type. You've written HTTPMethods *, which means "a pointer to an enum", whereas it looks like you just wanted an enum.
So change your property to this:
#property (nonatomic) HTTPMethods httpMethod;
For more info, google "C pointers" and you'll find information like this: http://pw1.netcom.com/~tjensen/ptr/pointers.htm

Type or property (NSUInteger (aka unsigned int)) does not match type of ivar (int)

I want to declare a NSInteger variable in my ViewController, but I get an error.
In my ViewController.h I have
#interface MainViewController : UIViewController{
NSInteger currentSection;
//Other stuff
}
#property (assign,nonatomic) NSUInteger currentSection;
#end
and in my ViewController.m I have:
#implementation MainViewController
//Other stuff
#synthesize currentSection;
But I get this error:
Type of property 'currentSection' ('NSUinteger' (aka 'unsigned int')) does not match type of ivar 'currentSection' ('int')
Why do I get this error with one simple instruction?
I'm using Xcode 4.4 with ARC.
You are asking the compiler to synthesize a NSUInteger property with a NSInteger ivar. You have to decide what the type of the property should be and adapt the type of the ivar accordingly.
Note that unsigned integer are not equivalent to (implicitly signed) integer. Casting one to the other might give you unexpected results.

Passing argument 4 of 'obj_setProperty' from incompatible pointer type

I am getting the above compiler error in XCode, and I cannot work out what's going on.
#import <UIKit/UIKit.h>
// #import "HeaderPanelViewController.h"
#import "HTTPClientCommunicator.h"
#import "WebSocket.h"
#class HeaderPanelViewController;
#protocol ServerDateTimeUpdating
-(void)serverDateTimeHasBeenUpdatedWithDate:(NSString *) dateString andTime:(NSString *) timeString;
#end
#interface SmartWardPTAppDelegate : NSObject <UIApplicationDelegate, WebSocketDelegate> {
}
#property (nonatomic, retain) id<ServerDateTimeUpdating> *serverDateTimeDelegate;
....
#end
Then in this line
#synthesize serverDateTimeDelegate;
in ApplicationDelegate.m I am getting the error "Passing argument 4 of 'obj_setProperty' from incompatible pointer type". I did a bit of research and found that 'retain' only works on class types, which is fair enough. If I actually remove the 'retain' from the line
#property (nonatomic, retain) id<ServerDateTimeUpdating> *serverDateTimeDelegate;
it does compile without complaints. However, I think, that's the wrong thing to do. Surely my 'id' is a class type and surely it should be retained in the setter. BTW, here is the declaration of my HeaderPanelViewController which implements the aforementioned protocol:
#interface HeaderPanelViewController : UIViewController<ServerDateTimeUpdating> {
}
...
#end
Also, if I actually do remove the retain I get a problem later down the track when I actually call the setter to register my HeaderPanelViewController as the delegate:
// Register this instance as the delegate for ServerDateTimeUpdating
// Retrieve the ApplicationDelegate...
ApplicationDelegate *applicationDelegate = (ApplicationDelegate *) [UIApplication sharedApplication].delegate;
// ...and register this instance
applicationDelegate.serverDateTimeDelegate = self;
The last line causes the XCode error message "Passing argument 1 of 'setServerDateTimeDelegate' from incompatible pointer type".
Your problem is the property declaration:
#property (nonatomic, retain) id<ServerDateTimeUpdating> *serverDateTimeDelegate;
If you command-double click "id", you'll see it defined as:
typedef struct objc_object {
Class isa;
} *id;
In other words, id is already an object reference. Therefore, the * right before serverDateTimeDelegate is unnecessary and wrong. Having it there means a pointer to an object reference, when you really just want an object reference.
Your problem is here:
#property (nonatomic, retain) id<ServerDateTimeUpdating> *serverDateTimeDelegate;
id is already a pointer type, so you declaring serverDateTimeDelegate as a pointer (*) effectively makes the property a pointer to a pointer.
Get rid of the * and everything should work fine.

error: property 'myBoolVariableName' with 'retain' attribute must be of object type

I have a BOOL value inside my #interface definition in my .h file. Here it is below. It has the same problem whether it's a pointer or not.
#interface myCustomViewController : UIViewController <UIWebViewDelegate> {
{
//...more iboutlets defined above
BOOL *myBoolVariableName;
}
When I compile, I get "error: property 'myBoolVariableName' with 'retain' attribute must be of object type" on the line for the import of my .h file.
I found this page here about an integer / nsnumber:
http://discussions.apple.com/thread.jspa?threadID=1846927
So, it seems I can't use BOOL values inside an #interface definition. What can I use instead?
What should I do for BOOL / boolean values?
I'm guessing that later in your interface you have something like this:
#property (retain) BOOL *myBoolVariableName;
That means make a property who's value is a pointer to a BOOL, and use retain semantics.
Your problem is that BOOL * is a pointer to a byte of memory, not a pointer to an object. And retain is something that applies only to objects.
Here's how you can make a BOOL property.
#interface myCustomViewController : UIViewController <UIWebViewDelegate> {
BOOL myBoolVariableName;
}
#property myBoolVariableName;
#end
The important differences are that the variable is declared as "BOOL", not "BOOL *" and the property doesn't have (retain).
I have faced similar situation. I solved it like,
#interface myCustomViewController : UIViewController {
BOOL myBoolVariableName;
}
#property BOOL myBoolVariableName;
#end
I refer one answer by Jon Hess but I got one error in it. Type name requires a specifier or qualifier.
My previous version was like,
#property myBoolVariableName;
So I added BOOL and solved the issue.