0

I'm trying to incorporate a standard C-array of floats into a singleton so that it can be accessed globally. Is this even possible? I feel the problem lies with the creation of the array; because It will not let me simply produce an array in the same way as I would in a header file.

For example:

@interface globalVariables : NSObject{

    float hzArray[4]; //arrays of floating point variables.

}

@property(nonatomic) float hzArray[4]; //ERROR RETURNED HERE

+(globalVariables *) getVariable;

@end

returns the error:

Property cannot have array or function type 'float[4]'

I realise this is probably a very rudimentary question, but I appreciate any advice you can give me.

Thanks, Tom.

Neeku
  • 3,646
  • 8
  • 33
  • 43
Tom Wilson
  • 67
  • 6
  • Why not use an NSArray and box the floats into NSNumbers? – danielbeard Jul 09 '14 at 15:10
  • It seems the issue comes from the use of your property. I am not fully sure, but I don't think you can mix c and objc properties/variables. Try making the float array a const float hzArray[4]; and see if that works. – user2277872 Jul 09 '14 at 15:10
  • 1
    http://stackoverflow.com/questions/476843/create-an-array-of-integers-property-in-objective-c – Larme Jul 09 '14 at 15:15
  • A "singleton" is simply an API that returns the pointer to something. The "something" can be anything you want. – Hot Licks Jul 09 '14 at 15:17
  • @property(nonatomic) float *hzArray; will work, but you'll need to make the malloc and free calls yourself – robert Jul 09 '14 at 15:21
  • Larme, thanks for the link, it appears C Arrays are not supported data types for properties. I'll work on adjusting my program to work with NSArray. Thanks, Tom. – Tom Wilson Jul 09 '14 at 15:38
  • You don't need to have a property to have a singleton. – Hot Licks Jul 09 '14 at 18:12

1 Answers1

0

Before you read on, check out the question that Larme linked.

Done? Ok. The short answer here is that you can't easily have arrays as properties in Objective-C, and you probably don't want to anyways. To do what you seem to want, I strongly recommend you use an NSArray, as some of the other commenters have mentioned, and store your float values in it, wrapped in NSNumber objects. Something like:

@interface GlobalVariables : NSObject
@property (strong, nonatomic) NSArray *hzArray;
+ (GlobalVariables *)getVariable;
@end

@implementation GlobalVariables
+ (GlobalVariables *)getVariable {
    GlobalVariables *gv = [[self alloc] init];
    // init the array in some meaningful way
    gv.hzArray = @[@(1), @(2), @(3)];
    return gv;
}
@end

Note that I've renamed the class to GlobalVariables, with a capitalized first letter, because that's the convention for class names in Objective-C.

Community
  • 1
  • 1
ravron
  • 11,014
  • 2
  • 39
  • 66
  • In theory one could use an NSValue property containing an array pointer. Of course, there is no need to use properties to implement a singleton anyway. – Hot Licks Jul 09 '14 at 18:13
  • Thank you very much for your advise, i've managed to get this integrated into my program. – Tom Wilson Jul 10 '14 at 14:13