Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Swift 2 and Xcode 7 GM fixes #144

Open
wants to merge 4 commits into
base: Swift-2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 97 additions & 81 deletions ExSwift/Array.swift

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions ExSwift/Dictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ internal extension Dictionary {

// If element has already been added to dictionary, append to it. If not, create one.
if result.has(groupKey) {
result[groupKey]!++
result[groupKey]! += 1
} else {
result[groupKey] = 1
}
Expand Down Expand Up @@ -333,7 +333,7 @@ internal extension Dictionary {

for (key, value) in self {
if test(key, value) {
result++
result += 1
}
}

Expand Down
27 changes: 13 additions & 14 deletions ExSwift/ExSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class ExSwift {

// Workaround for the now illegal (T...) type.
let adaptedFunction = unsafeBitCast(function, Function.self)

if times-- <= 0 {
times -= 1
if times <= 0 {
return adaptedFunction(params)
}

Expand All @@ -52,15 +52,15 @@ public class ExSwift {
- parameter function: Function to wrap
- returns: Wrapper function
*/
public class func after <T> (n: Int, function: Void -> T) -> (Void -> T?) {
/*public class func after <T> (n: Int, function: Void -> T) -> (Void -> T?) {
func callAfter (args: Any?...) -> T {
return function()
}

let f = ExSwift.after(n, function: callAfter)

return { f([nil]) }
}
}*/

/**
Creates a wrapper function that invokes function once.
Expand Down Expand Up @@ -97,14 +97,14 @@ public class ExSwift {
- parameter function: Function to wrap
- returns: Wrapper function
*/
public class func once <T> (function: Void -> T) -> (Void -> T) {
/*public class func once <T> (function: Void -> T) -> (Void -> T) {
let f = ExSwift.once {
(params: Any?...) -> T in
return function()
}

return { f([nil]) }
}
}*/

/**
Creates a wrapper that, when called, invokes function with any additional
Expand Down Expand Up @@ -245,29 +245,28 @@ extension ExSwift {
*/
internal class func bridgeObjCObject <T, S> (object: S) -> [T] {
var result = [T]()
let reflection = reflect(object)
let reflection = Mirror(reflecting: object)
let mirrorChildrenCollection = AnyRandomAccessCollection(reflection.children)

// object has an Objective-C type
if let obj = object as? T {
// object has type T
result.append(obj)
} else if reflection.disposition == .ObjCObject {
} else if reflection.subjectType == NSArray.self {

// If it is an NSArray, flattening will produce the expected result
if let array = object as? NSArray {
result += array.flatten()
} else if let bridged = reflection.value as? T {
} else if let bridged = mirrorChildrenCollection as? T {
result.append(bridged)
}

} else if reflection.disposition == .IndexContainer {
} else if object is Array<T> {
// object is a native Swift array

// recursively convert each item
(0..<reflection.count).each {
let ref = reflection[$0].1

result += Ex.bridgeObjCObject(ref.value)
for (_, value) in mirrorChildrenCollection! {
result += Ex.bridgeObjCObject(value)
}

}
Expand Down
11 changes: 6 additions & 5 deletions ExSwift/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ public extension NSArray {
*/
func flatten <OutType> () -> [OutType] {
var result = [OutType]()
let reflection = reflect(self)

for i in 0..<reflection.count {
result += Ex.bridgeObjCObject(reflection[i].1.value) as [OutType]
let mirror = Mirror(reflecting: self)
if let mirrorChildrenCollection = AnyRandomAccessCollection(mirror.children) {
for (_, value) in mirrorChildrenCollection {
result += Ex.bridgeObjCObject(value) as [OutType]
}
}

return result
}

Expand Down
2 changes: 1 addition & 1 deletion ExSwift/NSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ extension NSDate: Strideable {
}

public func advancedBy(n: NSTimeInterval) -> Self {
return self.dynamicType(timeIntervalSinceReferenceDate: self.timeIntervalSinceReferenceDate + n)
return self.dynamicType.init(timeIntervalSinceReferenceDate: self.timeIntervalSinceReferenceDate + n)
}
}
// MARK: Arithmetic
Expand Down
10 changes: 5 additions & 5 deletions ExSwift/Range.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal extension Range {
- parameter function: Function to call
*/
func times (function: () -> ()) {
each { (current: T) -> () in
each { (current: Element) -> () in
function()
}
}
Expand All @@ -26,7 +26,7 @@ internal extension Range {

- parameter function: Function to invoke
*/
func times (function: (T) -> ()) {
func times (function: (Element) -> ()) {
each (function)
}

Expand All @@ -35,7 +35,7 @@ internal extension Range {

- parameter function: Function to invoke
*/
func each (function: (T) -> ()) {
func each (function: (Element) -> ()) {
for i in self {
function(i)
}
Expand All @@ -46,8 +46,8 @@ internal extension Range {

- returns: Each element of the range in an array
*/
func toArray () -> [T] {
var result: [T] = []
func toArray () -> [Element] {
var result: [Element] = []
for i in self {
result.append(i)
}
Expand Down
38 changes: 19 additions & 19 deletions ExSwift/Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal extension AnySequence {

- returns: First element of the sequence if present
*/
var first: T? {
var first: Element? {
let generator = self.generate()
return generator.next()
}
Expand All @@ -26,7 +26,7 @@ internal extension AnySequence {
- parameter call: Function to call for each element
- returns: True if call returns true for any element of self
*/
func any (call: (T) -> Bool) -> Bool {
func any (call: (Element) -> Bool) -> Bool {
let generator = self.generate()
while let nextItem = generator.next() {
if call(nextItem) {
Expand All @@ -42,7 +42,7 @@ internal extension AnySequence {
- parameter index:
- returns: Object at index in sequence, nil if index is out of bounds
*/
func get (index: Int) -> T? {
func get (index: Int) -> Element? {
let generator = self.generate()
for _ in 0..<(index - 1) {
generator.next()
Expand All @@ -56,7 +56,7 @@ internal extension AnySequence {
- parameter range:
- returns: Subsequence in range
*/
func get (range: Range<Int>) -> AnySequence<T> {
func get (range: Range<Int>) -> AnySequence<Element> {
return self.skip(range.startIndex).take(range.endIndex - range.startIndex)
}

Expand All @@ -74,7 +74,7 @@ internal extension AnySequence {
return index
}
}
index++
index += 1
}
return nil
}
Expand All @@ -85,7 +85,7 @@ internal extension AnySequence {
- parameter n: Number of elements to skip
- returns: Sequence from n to the end
*/
func skip (n: Int) -> AnySequence<T> {
func skip (n: Int) -> AnySequence<Element> {
let generator = self.generate()
for _ in 0..<n {
generator.next()
Expand All @@ -99,8 +99,8 @@ internal extension AnySequence {
- parameter include: Function invoked to test elements for inclusion in the sequence
- returns: Filtered sequence
*/
func filter(include: (T) -> Bool) -> AnySequence<T> {
return AnySequence(lazy(self).filter(include))
func filter(include: (Element) -> Bool) -> AnySequence<Element> {
return AnySequence(self.filter(include))
}

/**
Expand All @@ -109,7 +109,7 @@ internal extension AnySequence {
- parameter exclude: Function invoked to test elements for exlcusion from the sequence
- returns: Filtered sequence
*/
func reject (exclude: (T -> Bool)) -> AnySequence<T> {
func reject (exclude: (Element -> Bool)) -> AnySequence<Element> {
return self.filter {
return !exclude($0)
}
Expand All @@ -121,7 +121,7 @@ internal extension AnySequence {
- parameter condition: A function which returns a boolean if an element satisfies a given condition or not
- returns: Elements of the sequence starting with the element which does not meet the condition
*/
func skipWhile(condition:(T) -> Bool) -> AnySequence<T> {
func skipWhile(condition:(Element) -> Bool) -> AnySequence<Element> {
let generator = self.generate()
let checkingGenerator = self.generate()

Expand All @@ -144,10 +144,10 @@ internal extension AnySequence {
- parameter item: The item to search for
- returns: true if self contains item
*/
func contains<T:Equatable> (item: T) -> Bool {
func contains<Element:Equatable> (item: Element) -> Bool {
let generator = self.generate()
while let nextItem = generator.next() {
if nextItem as! T == item {
if nextItem as! Element == item {
return true
}
}
Expand All @@ -160,7 +160,7 @@ internal extension AnySequence {
- parameter n: Number of elements to take
- returns: First n elements
*/
func take (n: Int) -> AnySequence<T> {
func take (n: Int) -> AnySequence<Element> {
return AnySequence(TakeSequence(self, n))
}

Expand All @@ -170,7 +170,7 @@ internal extension AnySequence {
- parameter condition: A function which returns a boolean if an element satisfies a given condition or not.
- returns: Elements of the sequence up until an element does not meet the condition
*/
func takeWhile (condition:(T?) -> Bool) -> AnySequence<T> {
func takeWhile (condition:(Element?) -> Bool) -> AnySequence<Element> {
return AnySequence(TakeWhileSequence(self, condition))
}

Expand All @@ -179,8 +179,8 @@ internal extension AnySequence {

- returns: Each element of the sequence in an array
*/
func toArray () -> [T] {
var result: [T] = []
func toArray () -> [Element] {
var result: [Element] = []
for item in self {
result.append(item)
}
Expand All @@ -203,8 +203,8 @@ public struct TakeSequence<S: SequenceType>: SequenceType {
public func generate() -> AnyGenerator<S.Generator.Element> {
var count = 0
var generator = self.sequence.generate()
return anyGenerator {
count++
return AnyGenerator {
count += 1
if count > self.n {
return nil
} else {
Expand All @@ -229,7 +229,7 @@ public struct TakeWhileSequence<S: SequenceType>: SequenceType {
public func generate() -> AnyGenerator<S.Generator.Element> {
var generator = self.sequence.generate()
var endConditionMet = false
return anyGenerator {
return AnyGenerator {
let next: S.Generator.Element? = generator.next()
if !endConditionMet {
endConditionMet = !self.condition(next)
Expand Down
9 changes: 4 additions & 5 deletions ExSwift/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public extension String {
return nil
}

let range = Range(start: advance(startIndex, range.startIndex), end: advance(startIndex, range.endIndex))
let range = startIndex.advancedBy(range.startIndex) ..< startIndex.advancedBy(range.endIndex)

return self[range]
}
Expand Down Expand Up @@ -91,9 +91,7 @@ public extension String {
- returns: Array of substrings
*/
func explode (separator: Character) -> [String] {
return split(self.characters, isSeparator: { (element: Character) -> Bool in
return element == separator
}).map { String($0) }
return self.characters.split { $0 == separator }.map { String($0) }
}

/**
Expand Down Expand Up @@ -215,7 +213,8 @@ public extension String {
- parameter charset: Chars to use in the random string
- returns: Random string
*/
static func random (var length len: Int = 0, charset: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -> String {
static func random (length: Int = 0, charset: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -> String {
var len = length

if len < 1 {
len = Int.random(max: 16)
Expand Down