Objective C problem defining properties - objective-c

When I attempt to define an int as a property in Objective C i get an error (not of type object). I have tried using NSInteger and int but neither work.
code:
int seat;
#property (nonatomic, retain) int seats;

You cannot retain an int or a NSInteger as they are primitive types - not objects. Use the following instead:
#property (nonatomic, assign) int seats;

Related

NSUInteger Equivalent for use in cocos2d-x project

Is there any macro for NSUInteger implemented in cocos2d-x for use in visual c++. I need this to port a project from obj-c to c++.
And also , I have seen these variables written after the declaration of the variables in the header file :
#property (nonatomic, readonly) CGPoint stickPosition;
#property (nonatomic, readonly) float degrees;
#property (nonatomic, readonly) CGPoint velocity;
#property (nonatomic, assign) BOOL autoCenter;
#property (nonatomic, assign) BOOL isDPad;
#property (nonatomic, assign) BOOL hasDeadzone;
#property (nonatomic, assign) NSUInteger numberOfDirections;
#property (nonatomic, assign) float joystickRadius;
#property (nonatomic, assign) float thumbRadius;
#property (nonatomic, assign) float deadRadius;"
Is there use in writing these in any way in c++ and is it possible? If so How?
NSUInteger is just the biggest-sized unsigned integer. In C++, you can use
unsigned long // when building a 64-Bit OS app
unsinged int // when building a 32-Bit OS app (ie iOS, Android)
instead of it. Or, to make it convenient:
typedef unsigned long NSUInteger;
typedef unsigned int NSUInteger;
If you want to represent a property in C++, that doesn't really belong to this topic - it's not Cocoa-dependent but language-dependent, simply solvable by reading the topic on member variables of a C++ book. Here's how:
class Whatever {
public:
CGRect rect;
}
then access it like:
Whatever *instance = new Whatever();
CGRect r = instance->rect;

Objective C: member variables and arrays

i have this variables (int and double-array)
.h-File
#interface MyCLass : NSObject
{
int myInt;
double paramStack[100];
}
#property (nonatomic, assign) int myInt;
//#property (nonatomic, assign) double paramStack; //<- ?
.m-File
#synthesise myInt;
//#synthesize paramStack; //<- ?
I want the int and the double-array-variable accessible from other classes via properties.
For the int-var. it looks fine, but the array throws errors at .m-file (#synthsize) and at h.file (#property (nonatomic, assign) double paramStack).
How can i define
"#property (nonatomic, assign) double paramStack;" as a double-array?
Thanks
Make the property with a pointer:
#property(nonatomic, assign) double *paramStack;
You can just use it like this:
NSLog(#"%f", self.paramStack[20]);
This is mainly because an array cannot be returned, but a pointer can. I.E. this getter would be impossible and that's why you cannot create an array property:
- (double[100])paramStack;

Making the getter/setter of an int?

#property (retain) int myInteger;
That throws me an error because apparently int is not considered an object... but I want to get the advantage of creating a getter/setter method with the #synthetize thing, but with an int. How could I achieve so? Is there an equivalent?
#property (assign) int chunkID;
or
#property (readonly) int chunkID;
You cannot retain a primitive type like integers. Only objects can be retained...
Use this:
#property (nonatomic, assign) int chunkID;
assign is the default so you might want to leave it out.
You need to use the assign type of property because you are dealing with a primitive object type (i.e. int). This kind of type can't be retained.
Only subclasses of NSObject can be retained / released.

In Objective-C Can I Treat a Property As An Array?

In good ole C I can do this:
int array[5];
int *iptr = array;
I have an Obj-C class with an ivar:
float *m_quad;
exposed via a synthesized #propery:
#property (nonatomic) float *quad;
Is there anyway to do this:
float f = myClass.quad[2];
Assuming of course I have malloc'd and set values for m_quad?
Thanks,
Doug
Should work of the bat. I have an ivar and property
float diffuseColor_[4];
#property (readonly, nonatomic) float* diffuseColor;
The property is not synthesized, but using a simple accessor I can write
- (float*) diffuseColor
{
return diffuseColor_;
}
object.diffuseColor[3] = 1;

Obj-C selector not recognized on property?

I'm getting a NSObject doesNotRecognizeSelector error when trying to set a property and I'm not sure why.
The error occurs on the first line of setWithNSColor. I'm a bit confused how a property that's properly synthesized could be not recognized.
#interface ScopeColor : NSObject {
NSString *colorIntegerString;
float redColor;
float greenColor;
float blueColor;
NSString *name;
}
#property (readwrite, assign) NSString *colorIntegerString;
#property (readwrite, assign) float redColor;
#property (readwrite, assign) float greenColor;
#property (readwrite, assign) float blueColor;
#property (readwrite, assign) NSString *name;
-(void)setWithNSColor:(NSColor *)inColor
{
self.redColor=[inColor redComponent];
self.greenColor=[inColor greenComponent];
self.blueColor=[inColor blueComponent];
}
Are you sure it is your class and not NSColor that is raising the exception? If the NSColor object does not belong to the NSCalibratedRGBColorSpace or NSDeviceRGBColorSpace asking for redComponent, etc. will raise an exception.
Are you synthesizing redColor, greenColor, and blueColor somewhere outside of the included code? Also, primitive values (such as floats) don't need the assign keyword.