Recently, in the program, you need to declare a one-dimensional array in the C language in the objective-C class:
@interface CArrayTest : NSObject{@private BOOL testArray[4]; }@end
Declare attributes
@property(nonatomic,assign)BOOL testArray[4];
Will promptError: Property can not have array or function type boll [4]
========================================================== ====================================
Correct practice: do not declare the property attribute, that is, do not use the system's set get method, manually add the set and get Methods
#import <Foundation/Foundation.h>@interface CArrayTest : NSObject{@private BOOL testArray[4]; }- (void)setTestArray:(BOOL*)aTestArray;- (BOOL *)testArray;@end
Implementation in carraytest. m
#import "CArrayTest.h"@implementation CArrayTest- (void)setTestArray:(BOOL*)aTestArray{ if(aTestArray != NULL) { for(int i = 0; i < 4; ++i) { testArray[i] = aTestArray[i]; } }}- (BOOL *)testArray{ return testArray;}@end
Test code:
// Test code: carraytest * test = [[carraytest alloc] init]; bool TMP [4] = {yes, no, yes, yes}; test. testarray = TMP; // or [test settestarray: TMP]; // output for (INT I = 0; I <4; ++ I) {If (Yes = test. testarray [I]) // or [test testarray] [I]; nslog (@ "yes"); else nslog (@ "no ");}
It is worth noting that:
In objective-C, the returned type cannot be an array of C language. Of course, the array name in C language is actually a corresponding type pointer pointing to the first address of the array,
So we use an array of the bool type, but the parameters and return values in the set and get methods must be bool * (pointer, pointing to the first address of the array)