简体   繁体   中英

Objective-C equivalent for Smalltalk's at: aKey ifAbsentPut: aBlock?

Consider the following code:

rankedGames at: rank ifAbsentPut: [SortedCollection sortBlock: [:one :two | one name < two name]].

I've only seen this "convenience" method used a couple of times in Smalltalk code, and then there is that SortedCollection there with no direct Obj-C equivalent. What is the Objective-C equivalent?

What is the Objective-C equivalent?

There isn't a direct equivalent. NSMutableArray gives you an ordered collection that can be sorted, so that's probably the closest to SortedCollection . I don't know of a single-line equivalent of at:ifAbsentPut: , so you'd normally do that in a few lines:

// assume rankedGames is an array of mutable arrays
NSMutableArray *games = rankedGames[rank];
if (games == nil) {
    games = [NSMutableArray array];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM