Add a prefix

To avoid naming conflicts, in the OC era we used to prefix methods with snp_/sd_ like prefixes. In Swift we have a more elegant approach:

struct OD<Base> {
    var base: Base
    init(_ base: Base) {
        self.base = base
    }
}

protocol ODCompatible {
    var od: OD<Self> {get}
    static var od: OD<Self>.Type {get}}extension ODCompatible {
    var od: OD<Self> {
        OD(self)}static var od: OD<Self>.Type {
        OD<Self>.self}}extension String: ODCompatible {}
extension OD where Base= =String {
    // Add method
    func test(a) {
        print("\ [self): :\ [self.base)")}}"A".od.test() // OD<String>(base: "A")::A
Copy the code

Quick exchange value

For this, we can quickly write:

// Common method:
func swapMe1<T> (a: inout T.b: inout T) {
    let temp = a
    a=b
    b = temp 
}
Copy the code

If we need to introduce a temporary variable temp, can we swap two values without passing a third bucket?

// Use multiple groups:
func swapMe2<T> (a: inout T.b: inout T) {
    (a,b) = (b,a)
}
Copy the code

DiscardableResult can discard the result

Swift is a very strict language, and the compiler will alert you when the return value of a function is not used. We can tell the compiler not to raise a warning by declaring the function as a throwable result using @discardableresult:


func woo(a) -> String {
    return "oldbird.run"
}

@discardableResult
func bar(a) -> String {
    return "Follow Oldbirds public account"
}

woo() // WARNING: Result of call to 'woo()' is unused

// It is also possible to assign a placeholder _ to avoid warnings
_ = woo()

bar()
Copy the code

Access control

Access control can limit the level of access that code in other source files or modules can have to your code. You can explicitly set access levels for individual types (classes, structs, enumerations), as well as for their properties, functions, initializers, primitive types, subscripts, and so on.

Swift provides four different access levels for entities in code: Open, public, internal, Fileprivate, and Private.

  • openandpublicA level allows entities to be accessed by all entities in the same module source file, and outside the module, all entities in the source file can be accessed by importing the module. Normally, you would useopenorpublicLevel to specify the framework’s external interface.openCan only operate on classes and members of classespublicThe main difference lies inopenQualified classes and members can be inherited and overridden outside the module.
  • internalLevels allow entities to be accessed by any entity in the same module source file, but not by entities outside the module. Typically, if an interface is only used within an application or framework, you can set it tointernalLevel.
  • fileprivateRestrict entities to being accessible only within the files they define. This can be used if some of the implementation details of a feature only need to be used in a filefileprivateTo hide it.
  • privateRestrict an entity to only the scope it is defined in, and within the same fileextensionAccess. This can be used if some of the details of the functionality are only needed in the current scopeprivateTo hide it.
  • Unless specifically specified, the default access level for an entity isinternal.

For more control details, refer to the translation documentation

Use where in the for loop

For simple loops, using WHERE is very expressive.

func archiveMarkedPosts(a) {
    for post in posts where post.isMarked {
        archive(post)
    }
}

func healAllies(a) {
    for player in players where player.isAllied(to: currentPlayer) {
        player.heal()
    }
}
Copy the code