Join string array with separator “, ” and add “, and ” to join the last element in Swift

前端 未结 2 330
余生分开走
余生分开走 2021-01-13 01:24

I\'m messing around with parsing JSON with SwiftyJSON on a swift playground. My code is as follows:

import UIKit
import SwiftyJSON

var partyList: [String] =         


        
2条回答
  •  无人及你
    2021-01-13 02:08

    Since iOS 13.0+ / macOS 10.15+ Apple provides the ListFormatter. See also here for details.

    Arrays can be formatted as easy as:

    let elements = ["a", "b", "c"]
    result = ListFormatter.localizedString(byJoining: elements)
    

    As the function name suggests, you also get the localization for free.

提交回复
热议问题