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

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;

Related

Creating an enum from NSDictionary

Suppose I have an enum as follows:
typedef enum {
FooGoods = 0,
FooInsurance = 1,
} FooCategory;
And I have it as property and ivar in another object
#property (nonatomic) FooCategory *category;
When I create that other object, I am filling with values from an NSDictionary So I do something like this:
[baz setCategory:[aDictionary objectForKey:#"Category"]];
However because my enum is not a pointer, I get an error.
Doing this get a warning as well, "Incompatible integer to pointer conversion":
[baz setCategory:[[aDictionary objectForKey:#"Category"] intValue]];
Any other way that is error/warning free?
The following is not correct
#property (nonatomic) FooCategory *category;
should be changed to
#property (nonatomic) FooCategory category;
Now this should work without warnings
baz.category = (FooCategory)[aDictionary objectForKey:#"Category"] intValue];

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.

Make sense create a property for a BOOL instance var?

Make sense create a property for a BOOl instance var?
Is it only tied to notation?
One use of #property is, we can access the property from other classes. For instance, consider you are declaring the BOOL property like the following,
#property (nonatomic) BOOL myState;
Now you can access myState property from other class. Otherwise it is not possible to access the BOOL variable from other classes. If you are not going to access this BOOL variable from other classes, you may omit the #property declaration.
BOOLs can be properties like any other variable. You should declare them as assigned, however because retain/copy have little to no meaning for a BOOL.
#interface MyObject : NSObject
#property (nonatomic, assign) BOOL myState;
#end
#implementation MyObject
#synthesize myState = _myState;
- (id) init
{
if (( self = [super init])) {
self.myState = NO;
}
return self;
}
#end

Objective C problem defining properties

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;