Properties of C Array


#1

Hi

It’s possible to create properties of C Arrays in Obj. C? The Compiler means i can only create properties for objects?
Thanks!


#2

Directly no, but indirectly yes by embedding.

You can embed a C-array inside a C-struct, then declare a property of type of that struct.

Here is a tiny, example:

#import <Foundation/Foundation.h>

typedef struct Foo Foo;
typedef struct Bar Bar;

struct Foo
{
   char v [16];
};

struct Bar
{
   char v [32];
};

@interface FooBar: NSObject

@property (nonatomic, assign) Foo foo;
@property (nonatomic, assign) Bar bar;

@end

int main (int argc, const char * argv[])
{
   FooBar *s = [[FooBar alloc] init];
   Foo foo;
   Bar bar;
   s.foo = foo;
   s.bar = bar;
   s.bar = foo; // error: incompatible type for argument 1 of ‘setBar:’

   // You can assign struct to struct too, effectively copying the contents
   Foo foo1;
   Bar bar1;
   foo1 = foo;
   bar1 = bar;
   bar1 = foo; // error: incompatible types in assignment

   return 0;
}

@implementation FooBar

@synthesize foo, bar;

@end

#3

Thank you, perfect solution :slight_smile: