Accessing array members without the use of objectAtIndex

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing array members without the use of objectAtIndex

tyler mclean
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?

Thanks,
Tyler McLean

_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: Accessing array members without the use of objectAtIndex

Andreas Fink-2
This is mainly a question of if the compiler is able to do this newer ObjC2.0 syntax.
clang does it nicely. Not sure if gcc can these days. Some older gcc versions where for sure not capable which was the reason for me to switch all to clang, given the new syntaxes saves you a lot of writing.

The syntax 

 id a =  someobject[x] ;

will translate into a call like this

a =[someobject objectForKeyedSubscript:x];

The syntax

someobject[x]  = a;


will translate into a call like this

[someobject setObject:a for keyedSubscript:x]



On 14 Apr 2018, at 01:03, tyler mclean <[hidden email]> 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?

Thanks,
Tyler McLean

_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep

Andreas Fink
------------------------------------------------------------------
Fink Telecom Services, Paradieshofstrasse 101, 4054 Basel, Switzerland
Mobile: +41-78-6677333
Skype: andreasfink    Jabber/XMPP: [hidden email] ICQ: 8239353
------------------------------------------------------------------






_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: Accessing array members without the use of objectAtIndex

David Chisnall-7
In reply to this post by tyler mclean
Hi Tyler,

> On 14 Apr 2018, at 00:03, tyler mclean <[hidden email]> 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


_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: Accessing array members without the use of objectAtIndex

David Chisnall-7
In reply to this post by Andreas Fink-2
On 14 Apr 2018, at 13:39, Andreas Fink <[hidden email]> wrote:

>
> This is mainly a question of if the compiler is able to do this newer ObjC2.0 syntax.
> clang does it nicely. Not sure if gcc can these days. Some older gcc versions where for sure not capable which was the reason for me to switch all to clang, given the new syntaxes saves you a lot of writing.
>
> The syntax
>
> id a =  someobject[x] ;
>
> will translate into a call like this
>
> a =[someobject objectForKeyedSubscript:x];
>
> The syntax
>
> someobject[x]  = a;
>
>
> will translate into a call like this
>
> [someobject setObject:a for keyedSubscript:x]

This is only true if x is an object type.  If x is an integer type then it translates into [someobject objectAtIndexedSubscript: x].  Currently, in GNUstep, -objectAtIndexedSubscript: is implemented by only NSArray and calls [self objectAtIndex:] (which the concrete subclass of NSArray then implements), so when you use the subscripting you are actually getting less efficient code than if you use -objectAtIndex:.

So, uh, yay?

David


_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: Accessing array members without the use of objectAtIndex

Riccardo Mottola-5
In reply to this post by tyler mclean
Hi,

On 2018-04-14 01:03:36 +0200 tyler mclean
<[hidden email]> wrote:

> 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?

the "bloat" is only in your source code, not in the generated code.
Is it worth the deal?
You actually make the reader/programmer harder to understand when you
are realling use a a C array with some smart pointer arithmetic and
when you are using an NSArray/NSMutableArray.
I vastly personaly vastly prefer the clear distinction: the same goes
for struct access vs. properties.
Matter of personal taste of course, but especially in sometimes
low-level code where C and Obj-C are mixed, the distinction increases
readability.

my two cents.

Riccardo


_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep
Reply | Threaded
Open this post in threaded view
|

Re: Accessing array members without the use of objectAtIndex

Richard Frith-Macdonald-9
In reply to this post by David Chisnall-7


> On 14 Apr 2018, at 13:46, David Chisnall <[hidden email]> wrote:
>
> Hi Tyler,
>
>> On 14 Apr 2018, at 00:03, tyler mclean <[hidden email]> 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.

I agree with this ... the fact that we support this syntax does not mean that anyone should use it.

If you use the -objectAtIndex: method your code is clearer and more maintainable (minimising the chance of confusing C array variables and NSArray variables) as well as being marginally more efficient and substantially more portable.



_______________________________________________
Discuss-gnustep mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/discuss-gnustep