| Computers Forum Index » Computer Languages (Objective-C) » Properties with arrays... |
|
Page 1 of 1 |
|
| Author |
Message |
| activefreeman... |
Posted: Sun Jul 12, 2009 6:20 pm |
|
|
|
Guest
|
Hello, is it possible to use the at (no spam) property compiler directive to
create getter/setter methods for an array. This is may try:
at (no spam) interface TragaperrasViewController : UIViewController {
NSArray* cols[N_COLS];
}
at (no spam) property NSArray* cols[N_COLS];
at (no spam) end
When I compile it one obtain:
error: bad property declaration
I can use an array of arrays, however it would be nice to have an
"even more efficient" method to get access to the elements of a fixed
size array, specially in the iPhone OS. |
|
|
| Back to top |
|
|
|
| Kulakov Ilya... |
Posted: Tue Jul 14, 2009 9:40 am |
|
|
|
Guest
|
On Jul 13, 1:20 am, activefreeman <challaganmad... at (no spam) gmail.com> wrote:
Quote: Hello, is it possible to use the at (no spam) property compiler directive to
create getter/setter methods for an array. This is may try:
at (no spam) interface TragaperrasViewController : UIViewController {
NSArray* cols[N_COLS];}
at (no spam) property NSArray* cols[N_COLS];
at (no spam) end
When I compile it one obtain:
error: bad property declaration
There is a problem with C array of NSArray's. If you remove [N_COLS]
from the code all will work. However, you can use something like this:
#import <Foundation/Foundation.h>
at (no spam) interface A : NSObject
{
NSArray* objects;
NSArray* container;
}
at (no spam) property(copy) NSArray* container;
at (no spam) end
at (no spam) implementation A
at (no spam) synthesize container;
- init{
if(self = [super init]){
objects = [NSArray arrayWithObjects: at (no spam) "1", at (no spam) "2", at (no spam) "3", at (no spam) "4", nil];
container = [NSArray arrayWithObjects: objects, objects, objects,
nil];
}
return self;
}
at (no spam) end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
A *a = [[A alloc] init];
for(NSArray *array in a.container){
for(NSString *str in array){
NSLog(str);
}
}
[a release];
[pool drain];
return 0;
}
I'am a new in ObjC, so I may be wrong. |
|
|
| Back to top |
|
|
|
| activefreeman... |
Posted: Tue Jul 14, 2009 5:31 pm |
|
|
|
Guest
|
Thank you for answering.
What I am searching/proposing is a way to optimize the access with
fixed length arrays by means of getter/settter methods that permit
providing the index of the element to be accessed. |
|
|
| Back to top |
|
|
|
| Kulakov Ilya... |
Posted: Wed Jul 15, 2009 8:19 am |
|
|
|
Guest
|
What objects you want to keep in an array: POD and C/C++ or ObjC
structures? |
|
|
| Back to top |
|
|
|
| activefreeman... |
Posted: Wed Jul 15, 2009 6:01 pm |
|
|
|
Guest
|
An array of ObjC/ObjeC++ objects in general such as:
NSDate* cols[N_COLS];
This is not a problem in general. However, I cannot use the at (no spam) property
facility to index the elements of this kind of fix length/high
efficient array. The only way is to use:
NSArray* cols;
And populate this container object with NSDate objects.
Btw: I don't know what a POD is. |
|
|
| Back to top |
|
|
|
| Kulakov Ilya... |
Posted: Wed Jul 15, 2009 7:49 pm |
|
|
|
Guest
|
|
| Back to top |
|
|
|
| Greg Parker... |
Posted: Thu Jul 16, 2009 1:47 am |
|
|
|
Guest
|
activefreeman <challaganmadrid at (no spam) gmail.com> writes:
Quote: An array of ObjC/ObjeC++ objects in general such as:
NSDate* cols[N_COLS];
This is not a problem in general. However, I cannot use the at (no spam) property
facility to index the elements of this kind of fix length/high
efficient array.
That's right. at (no spam) property is not designed to handle this. Your best bet
is to write ordinary accessor methods, or make the ivar at (no spam) public and
manipulate it directly.
(You could declare an at (no spam) property that returned NSDate**. But you probably
couldn't at (no spam) synthesize the methods, so there's not much benefit over simply
writing your own methods.)
--
Greg Parker gparker at (no spam) apple.com Runtime Wrangler |
|
|
| Back to top |
|
|
|
|