In app purchase: sorting array of product identifiers

I have the following non-consumable products en RageProducts.swift.


public struct RageProducts {

  // MARK: identifiers product no consumible
  private static let s1 = "comic1" // name in itunnesconnect: Comic Vol 01
  private static let s2 = "comic2" // name in itunnesconnect: Comic Vol 02
  private static let s3 = "comic3" // name in itunnesconnect; Comic Vol 03
  private static let s4 = "comic4"
  private static let s5 = "comic5"
  private static let s6 = "comic6"
  private static let s7 = "comic7"
  private static let s8 = "comic8"
  private static let s9 = "comic9"
  private static let s10 = "comic10" // name in itunnesconnect: Comic Vol 10
  private static let s11 = "comic11"
  private static let s12 = "comic12"
  private static let s13 = "comic13"

  // MARK .- Set identifiers

  private static let myproductsIdentifier: Set = [RageProducts.s1,
  RageProducts.s2,
  RageProducts.s3,
  RageProducts.s4,
  RageProducts.s5,
  RageProducts.s6,
  RageProducts.s7,
  RageProducts.s8,
  RageProducts.s9,
  RageProducts.s10,
  RageProducts.s11,
  RageProducts.s12,
  RageProducts.s13]
//MARK : Reference to IAPHelper 
  public static let store = IAPHelper(productIdentifiers: RageProducts.myproductosIdentifiers)

}



Everything works fine except in the order of when they are displayed in a UITableview, it does not respect the order entered in the SET

(

Set <ProductIdentifier>
)

This shows me:


Comic Vol 01
Comic Vol 10
Comic Vol 11
Comic Vol 12
Comic Vol 13
Comic Vol 02
Comic Vol 03
.
.
.
Comic Vol 09


The correct thing should be:

Comic Vol 01
Comic Vol 02
Comic Vol 03
Comic Vol 04
Comic Vol 05
.
.
.
Comic Vol 13


Is there a way to sort them according to the order entered or how could this problem be solved?


Screenshout: https://i.stack.imgur.com/zLvRe.jpg

Sets are not ordered. So you cannot guarantee the order.


You could either

- use NSOrderedSet,

- or maybe go and use an array instead of Set ?

  • use NSOrderedSet,
  • or maybe go and use an array instead of Set ?

Personally I like to keep my model objects in a set and then deal with sorting as a presentation issue.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Quinn,

Why don't you like NSOrderedSet ?


But effectively, it is simple to sort when needing to display


        let myArrayOfIdentifiers : [String] = Array(myproductsIdentifier)
        let myWellSortedArrayOfIdentifiers = myArrayOfIdentifiers.sorted { (first, second) in
            let toSkip = "comic".count
            let firstNumString = String(first.dropFirst(toSkip))
            let firstIndex = Int(firstNumString) ?? 0
            let secondNumString = String(second.dropFirst(toSkip))
            let secondIndex = Int(secondNumString) ?? 0
            return firstIndex < secondIndex }
        print(myWellSortedArrayOfIdentifiers)


And get

["comic1", "comic2", "comic3", "comic4", "comic5", "comic6", "comic7", "comic8", "comic9", "comic10", "comic11", "comic12", "comic13"]

Why don't you like

NSOrderedSet
?

Because I consider the model to be different from the presentation of the model. Imagine you have one screen that presents this model (a set of in-app purchase) alphabetically and another screen that presents the model sorted by introduction date. Your model can only be ordered by one of these criteria, so one of these presenters is privileged and the other has to re-sort. That seems weird to me, hence my approach.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

So the point is not NSOrdered set per se, but the design pattern it involves.

Clear and well seen.

In app purchase: sorting array of product identifiers
 
 
Q