Sunday, April 17, 2016

Re: trying to get last object of all arrays in an array

On Sat, Apr 16, 2016, at 01:51 AM, Fjölnir Ásgeirsson wrote:
> -[NSArray valueForKey:] simply calls through to its members, so it makes
> sense that the lastObject would bubble down to the strings themselves.
>
> You should probably be using a dictionary. If order matters; something
> like
> https://github.com/fjolnir/DatabaseKit/blob/master/Source/Utilities/DBOrderedDictionary.m
> should work.

Yes, this is important to note: NSArray overrides -valueForKey: to
effectively implement `map` for its elements. This is documented here:
<https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/#//apple_ref/occ/instm/NSArray/valueForKey:>

--Kyle Sluder

>
> —fjõlnir
>
>
> On 16 Apr 2016, at 15:18, Patrick J. Collins wrote:
>
> > If I have:
> >
> > NSArray *arr = @[
> > @[@"lol", @"omg"],
> > @[@"rofl", @"copter"],
> > ...
> > ];
> >
> > I want to be able to do:
> >
> > [arr valueForKey:@"lastObject"]
> >
> > and get back:
> >
> > @[@"omg", @"copter"]
> >
> > However, when I try this, I keep getting bad access saying that the
> > object doesnt respond to lastObject... ?
> >
> > What am I doing wrong?
> >
> >
> > Patrick J. Collins
> > http://collinatorstudios.com

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list (Objc-language@lists.apple.com)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/objc-language/anirudha.k.cpp1%40blogger.com

This email sent to anirudha.k.cpp1@blogger.com

No comments:

Post a Comment