Latest 2.0.0
Homepage https://github.com/WeZZard/Swift-Extended-Library
License MIT
Platforms ios 8.0, osx 10.9
Authors

SwiftExt is aiming to offer native Swift alternatives to Cocoa/CocoaTouch and missing conveniences in Swift standard library.

Get Started

Manually Install

  • Download SwiftExt
  • Drag SwiftExt’s Xcode project file to your workspace
  • Add SwiftExt to yourEmebed Binaries field in your target’s general page if you are building an app. Or add SwiftExt to your Linked Frameworks and Libraries field in your target’s general page if you are building a framework.
  • Add import SwiftExt to your Swift source file
  • Enjoy your journey of SwiftExt

Brief Introduction

Bitmask Utilities

For a given RawRepresentable conformed type:

struct Bitmask: OptionSetType {
  typealias RawValue = UInt
  var rawValue: RawValue
  init(rawValue: RawValue) { self.rawValue = rawValue }

  static var offset0 = Bitmask(rawValue: 1 << 0)
  static var offset1 = Bitmask(rawValue: 1 << 1)
  static var offset2 = Bitmask(rawValue: 1 << 2)
}

Enumerate RawRepresentable with Type of Unsigned Integers as RawValue

You can enumerate a value of the type:

let aBitmask = Bitmask.offset0

Like:

enumerate(aBitmask, withOptions: [.HandleOccurred]) {
  (bitmask, bit) -> Bool in
  let shouldStop = false
  print("(bitmask) - (bit)")
  return shouldStop
}

More enumerate options:

public struct BitmaskTraversalOptions: OptionSetType {
    public typealias RawValue = UInt
    public let rawValue: RawValue

    public static var HandleOccurred
    public static var HandleNotOccurred
    public static var ReverselyEnumerate
}

Get OptionSetType Conformed Type Value’s Raw Bits, First Bit and Last Bit

let rawBitOffsets = aBitmask.rawBitOffsets

let firstBitOffset = aBitmask.firstBitOffset

let lastBitOffset = aBitmask.lastBitOffset

Bitmask and Dictionary Fallback-Able Fetching

For a given dictionary:

let aDictionary: [Bitmask : Int] = [.offset0 : 0, .offset2 : 2]

If you fetch value for the key .offset1 with functions below, you will get 0, which pairs with the key .offset0.

let aFallbackValue = valueForBitmask(.offset1, inDictionary: aDictionary)

let anotherFallbackValue = aDictionary.valueForKey(.offset1, fallback: .RightShift)

Sometimes, you might need to call this function with its module name:

SwiftExt.valueForBitmask(...)

Collection Types Enhancement

Diff

You can now diff two identical collection type values like:

for eachDiff in diff(arrayA, arrayB) {
    switch eachDiff {
    case let .Insert(index, element):
        // Do whatever you want
    case let .Delete(index, element):
        // Do whatever you want
    case let .Update(index, oldElement, newElement):
        // Do whatever you want
    case let .Move(fromIndex, fromElement, toIndex, toElement, updated):
        // Do whatever you want
    case let .NoChange(index, element):
        // Do whatever you want
    }
}

Additionally, you can handle content changes while the diffing when you specified a content comparator.

for eachDiff in diff(arrayA, arrayB, changed: aContentComparator) {
    switch eachDiff {
    case let .Insert(index, element):
        // Do whatever you want
    case let .Delete(index, element):
        // Do whatever you want
    case let .Update(index, oldElement, newElement):
        // Do whatever you want
    case let .Move(fromIndex, fromElement, toIndex, toElement, updated):
        // Do whatever you want
    case let .NoChange(index, element):
        // Do whatever you want
    }
}

Merge

You can now merge two identical collection type values like:

let arrayA = [0,2,4,6,8]
let arrayB = [1,3,5,7,9]

let result = arrayA.merge(arrayB)

which yields an array:

[0,1,2,3,4,5,6,7,8,9]

The merge could be faster if your elements conformed to the Comparable protocol.

Intersect

You can now intersect two identical collection type values like:

let arrayA = [0,2,4,6,8]
let arrayB = [1,3,5,7,9]

let result = arrayA.intersect(arrayB)

which yields an array:

[]

Remove

You can now remove elements in a collection type value like:

var arrayA = [0,1,2,3,4]

arrayA.removeIndicesInPlace([0,2,4])

which makes the arrayA to be:

[1,3]

You can also now remove elements in a collection type value like:

var arrayB = ["A","B","C","D","E"]

arrayB.removeInPlace(["A","C","E"])

which makes the arrayB to be:

["B","D"]

Contains

You can now check if a sequence type value contains another identical typed value like

let arrayA = [0,1,2,3,4,5,6,7,8]
let arrayB = [0,1,2]

arrayA.contains(arrayB)

which yields:

true

Remove Duplicates and Getting Unique Elements

You can now remove duplicates in a collection type value like:

let arrayA = [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8]
let nonRedundantArrayA = arrayA.removeDuplicatesInPlace()

which yields:

[0,1,2,3,4,5,6,7,8]

You can now get the unique elements in a collection type value like:

let arrayA = [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8]
let nonRedundantArrayA = arrayA.unique()

which yields:

[0,1,2,3,4,5,6,7,8]

More Responsive Map and Reduce

One-many map (multipleMap), stoppable flatMap, map and reduce could be found in SwiftExt

Date Time

The date-time system in SwiftExt is planned to be built as a replacement of ICU based Cocoa date-time system. But since the works is much overload, currently there is TimeInterval and Date only.

TimeInterval

You can now say good bye to float pointing NSTimeInterval and say hello to Integer based TimeInterval now. It utilizes standard POSIX interface to get values.

Date

Wraps a TimeInterval value

Memory

BinaryEquivalentAccessible

public protocol BinaryEquivalentAccessible {
    typealias BinaryEquivalent
}

By conforming to BinaryEquivalentAccessible , the type will get a pointer and a mutable pointer to itself. It is your responsibility to write the BinaryEquivalent. Learn more on Memory Layout Based Hacking in Swift.

Weak

This is a value type which holds a weak reference to an object.

public struct Weak<T: AnyObject>: Hashable {
    private weak var _value: T?
    public weak var value: T? { return _value }
    public init(_ aValue: T) { _value = aValue }

    public var hashValue: Int {
        guard let value = self.value else { return 0 }
        return ObjectIdentifier(value).hashValue
    }
}

Transaction

MultiTransactionCoalescer

MultiTransactionCoalescer is used for coalescing multiple transactions. It assumes changes in each transaction and its nested transactions are described based on a same snapshot of data, but those in coming transactions and their nested transactions are not.

Considering the following update:


                   +-------------- Transaction --------------+
                   |                                         |
                   |                                         |
                   |                                         |
        Snapshot Before Update                     Snapshot After Update

--------+-----------------------------------------------------------------------
Row No. |                              Value
--------+-----------------------------------------------------------------------
        |
0       |          0                                         0
        |
1       |          1                                         1
        |                       == Delete Row 3 =>
2       |          2                                         2
        |                       == Insert Row 4 =>
3       |          3                                         4
        |
4       |          4                                         5
        |
--------+-----------------------------------------------------------------------

In the previous update example, Delete Row 3 described the row in the snapshot before the transaction and Insert Row 4 described the row in the snapshot after the transaction. Thus we can be aware of that, for an update transaction, update actions are described based on different time of the transaction – some of them are before the transaction and some of them are after.

For all supported update actions:

Insert : Described based on snapshot AFTER transaction.

Delete : Described based on snapshot BEFORE transaction.

Move : Described based on snapshot BEFORE transaction.

Update : Described based on snapshot BEFORE transaction.

Due to such a thing, there are two common conventions in many transactional update system:

1) Defers all update actions described based on the after-transaction snapshot.

2) Firstly process all update actions described based on the before-transaction snapshot by following: Update -> Move -> Deletion.

And MultiTransactionCoalescer assumes that your update system follows such two

Miscellaneous

Logical XOR

Add a logical XOR operator: ^^

Exponentiation

Add an exponentiation operator: **

License

SwiftExt is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SwiftExt",
    "version": "2.0.0",
    "summary": "A library extends Swift standard library",
    "description": "                   Swift Extended Library is aiming to offer native Swift alternatives to Cocoa/CocoaTouch and missing conveniences in Swift standard library.n",
    "homepage": "https://github.com/WeZZard/Swift-Extended-Library",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "WeZZard": "[email protected]"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    },
    "source": {
        "git": "https://github.com/WeZZard/Swift-Extended-Library.git",
        "tag": "2.0.0"
    },
    "ios": {
        "source_files": [
            "Swift-Extended-Library/**/*.swift",
            "Swift-Extended-Library-for-iOS/**/*.swift"
        ]
    },
    "osx": {
        "source_files": [
            "Swift-Extended-Library/**/*.swift",
            "Swift-Extended-Library-for-OS-X/**/*.swift"
        ]
    }
}

Pin It on Pinterest

Share This