0

I have a constants.h file and declared:

extern NSString * check_array_editable[];

In the constants.m i set:

NSString * check_array_editable[] = { @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"9", @"11", @"14" , @"17", @"18", @"21" };

So is dont have to call an initializer, what i have to do if i use NSArray instead of the collection.

Now i import the constants.h and can access check_array_editable[]. But how will i be able to get the length and the Index of a specific item like the usage of NSArray:

 BOOL isTheObjectThere = [check_array_editable containsObject: @"my string"];
 NSUInteger indexOfTheObject = [check_array_editable indexOfObject: @"my string"];

---edit I finally decidest to work with NSArray and use a inizalise void in the constants.h

NSArray * check_array_invisible;
NSArray * check_array_editable;
NSArray * check_array_texteingabe;
NSArray * check_array_assistent;
NSArray * check_array_functionfield;
+(void)initialize{
  check_array_invisible = @[@10, @13, @34, @35];
  check_array_editable = @[@1,@2, @3, @4, @5, @6, @7, @9, @11, @14 , @17, @18, @21 ];
  check_array_texteingabe = @[@1, @2, @3, @9, @11, @14];
  check_array_assistent = @[@16, @35];
  check_array_functionfield = @[@32];
}
2red13
  • 11,197
  • 8
  • 40
  • 52
  • The NSArray initializer using Objective-C literals is as long as the C-array `NSArray * check_array_editable = @[@"1", @"2", @"3" … ];` and you have the whole convenience of the NSArray class – vadian Jul 16 '15 at 09:58

2 Answers2

1

With your declaration your array is C style array so you won't be able to use the familiar methods like containsObject and indexOfObject but you should deal with it as a C style array and search like this

NSString *s = @"1";
int i = 0;
while(check_array_editable[i]) {
    if( [check_array_editable[i] isEqualToString:s]) {
        NSLog(@"yeah");
        break;
    }
    i++;
}

Instead you can in your implementation file declare the array like this:

#define check_array_editable@[@"1", @"2", @"3", @"4"]

and now you can use the fun methods like indexOfObject

example:

NSLog( @"%zd",[arrayOfStrings indexOfObject:@"2"]);

result:

2015-07-16 11:04:47.620 PROJ[881:13857] 1

More information here , here and here

Community
  • 1
  • 1
Alaeddine
  • 6,104
  • 3
  • 28
  • 45
1

You can use sizeof to determine the number of elements in your C array. Taking the sizeof a C array:

sizeof(check_array_editable)

returns the total size of all its elements (in your example the answer is 104 [assuming a 64-bit binary]); and in your case each element is an NSString * the size of which is given by:

sizeof(NSSString *)

which will return 8 [again assuming 64-bit]. Just divide the total length by the element length:

sizeof(check_array_editable) / sizeof(NSSString *)

and you get the number of elements in the array (in your example 13).

Important: The above will only work for variables declared as arrays. A variable declared with a C pointer type, which is then set to reference dynamically allocated memory, might be usable as an array but it is not one. Taking the sizeof such a variable returns its size - the size of a pointer - not the size of what it is currently referencing.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86