The following array x is not large, but it is my example array.
float x[4] = {1.0f, 2.0f, 4.0f, 8.0f};
Say, I want to use the array in my ViewController BSViewController.h/.m . I have included BSParam.h in BSViewController.h, but I don’t know what else to do to use x in BSViewController.m
Below is the code so far for my class which will containt my constant arrays.
//
// BSParam.h
#import <Foundation/Foundation.h>
@interface BSParam : NSObject
@end
//
// BSParam.m
#import "BSParam.h"
@implementation BSParam
#include <stdio.h>
float x[4] = {1.0f, 2.0f, 4.0f, 8.0f};
@end
1) If you want to make it available outside of your class, use the
externkeyword to make it available:2) Anyhow, this solution will make the variable
xglobal, and available anywhere in your application, without any context/namespace to access it, so that’s not the optimal solution.To make global values accessible to other parts of your application, you better use the Singleton pattern, or use a class method for that.
For example:
So that you can use
[BSParam x]to access your array, instead of justx, adding some namespace to access thisxarray, avoiding the risk to mess with other local variables or whatever.