discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessing array members without the use of objectAtIndex


From: David Chisnall
Subject: Re: Accessing array members without the use of objectAtIndex
Date: Sat, 14 Apr 2018 13:46:58 +0100

Hi Tyler,

> On 14 Apr 2018, at 00:03, tyler mclean <sonarsoundapplications@gmail.com> 
> wrote:
> 
> Greetings developers,
> 
> I want to preface this question by saying that this project is
> impressive. My question is, is there a way to access members of
> NSArray or NSMutableArray like an array with the GNUstep framework or
> is that a function that currently only sits with the current Apple
> architecture of the language/libraries?
> 
> e.g
> NSMutableArray *myArray = [[NSMutableArray alloc] init]; //This will
> be an array of NSStrings
> . . .
> NSLog(@"%@", myArray[0]);
> 
> instead of
> 
> NSLog(@"%@", [myArray objectAtIndex:0]);
> 
> while this particular example does not create much bloat, repeated
> need to access members can make code substantially larger than that of
> it's counterpart on Apple's end. If so, what do I need to do to start
> using this feature. Else if not, is there a plan to add this in the
> (hopefully near) future?

Your post reminds me why I hate the new syntax - it will generate exactly the 
same amount of code as the traditional syntax, yet gives the superficial 
impression of being more efficient.  As with the property accessor syntax, it 
generates a message send using the same syntax that C uses for a simple pointer 
arithmetic operation.

Nevertheless, this is supported by GNUstep.  The following works for me with 
the FreeBSD packages:

$ . /usr/local/GNUstep/System/Library/Makefiles/GNUstep.sh 
$ cat idx.m 
#import <Foundation/Foundation.h>

int main(void)
{
        NSArray *a = @[ @"foo" ];
        NSLog(@"%@", a[0]);
        return 0;
}
$ clang `gnustep-config --objc-flags` `gnustep-config --base-libs` idx.m        
                                                      
$ ./a.out 
2018-04-14 12:40:47.094 a.out[73313:102859] foo

To use the new syntax, you must be using a vaguely recent version of clang (any 
release from the last couple of years is fine) and a vaguely recent version of 
GNUstep base (I think any of the last three releases is fine).  

You must also be using the non-fragile ABI.  Check that your gnustep-config 
line includes -fobjc-runtime=gnustep-{something}.  When targeting a fragile 
ABI, the subscripting syntax is also valid, but assumes that the pointer is a C 
array of object pointers, which is never what you actually want (so clang will, 
hopefully, warn you).  If your GNUstep install is configured to use the fragile 
ABI, please contact whoever maintains it for your distribution and let them 
know what decade it is.

David




reply via email to

[Prev in Thread] Current Thread [Next in Thread]