Skip to content

Commit

Permalink
Upgrade for swift 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Sep 28, 2018
1 parent 0129bf8 commit 395e8b8
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 15 deletions.
9 changes: 7 additions & 2 deletions Sources/Utils/array/iterator/DemoIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import Foundation
* Demonstrates async iterator
*/
class DemoIterator:ArrayIterator<DemoItem> {
var complete:()->Void
init(array: Array<T>,onComplete:@escaping ()->Void) {
typealias Completed = ()->Void
var complete:Completed
init(array: Array<T>,onComplete:@escaping Completed) {
self.complete = onComplete
super.init(array: array)
}
Expand Down Expand Up @@ -39,3 +40,7 @@ func iterate(){
}
}
//iterate()

class ReverseableDemo{

}
4 changes: 3 additions & 1 deletion Sources/Utils/array/iterator/Iteratable.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation

/**
* TODO: ⚠️️ Why are we using a method for the hasNext call?
*/
protocol Iteratable {
associatedtype T
var index:Int {get set}/*The iteration cursor*/
Expand Down
9 changes: 9 additions & 0 deletions Sources/Utils/array/iterator/Reversable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation
/**
* TODO: ⚠️️ alt name could be ReverseIteratable 👈 next
*/
protocol Reversable:Iteratable {
func hasPrev() -> Bool
func prev() -> T
}

18 changes: 18 additions & 0 deletions Sources/Utils/array/iterator/ReverseArrayIterator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation

class ReverseArrayIterator<T>:ArrayIterator<T> {
func hasPrev() -> Bool{
return index > 0
}
/**
* Returns prev item
*/
func prev() -> T{
index -= 1
let retVal = collection[index]/*cur item*/
return retVal
}
}
extension ReverseArrayIterator:Reversable{}


3 changes: 2 additions & 1 deletion Sources/Utils/geom/context/Graphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import Cocoa
*/
public class Graphics{
lazy public var context:CGContext = {
return NSGraphicsContext.current?.cgContext ?? {fatalError("Context not available")}()/* Get the handle to the current context */
//this could be buggy in swift 4.2
return ((NSGraphicsContext.current?.cgContext ?? {fatalError("Context not available")}())!)/* Get the handle to the current context */
}()
var mode:(fill:FillMode,stroke:StrokeMode) = (.None, .None)
var gradient:GraphicsGradientKind?/* = GraphicsGradient()*//*This value exists because we will use it when doing radial and linear gradient construction and need access to matrix etc*/
Expand Down
10 changes: 6 additions & 4 deletions Sources/Utils/geom/path/cg/CGPathExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ extension CGPath {
let body = unsafeBitCast(info, to: Body.self)
body(element.pointee)
}
Swift.print(MemoryLayout.size(ofValue: body))
let unsafeBody = unsafeBitCast(body, to: UnsafeMutableRawPointer.self)
self.apply(info: unsafeBody, function: callback as! CGPathApplierFunction)
fatalError("out of order in swift 4.2")
//Swift.print(MemoryLayout.size(ofValue: body))
//let unsafeBody = unsafeBitCast(body, to: UnsafeMutableRawPointer.self)
//self.apply(info: unsafeBody, function: callback as! CGPathApplierFunction)
}
}
}

18 changes: 12 additions & 6 deletions Sources/Utils/misc/network/NetworkParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ class NetworkParser{
* PARAM: httpBody: some servers requires the params to be encoded as data
*/
static func data(url:URL, httpMethod:HTTPMethodType = .get, httpBody:Data? = nil, completion: @escaping URLQuery = defaultURLQueryComplete) {
let session:URLSession = URLSession.shared
var request = URLRequest.init(url: url)
request.httpMethod = httpMethod.rawValue//get or post
var urlRequest = URLRequest.init(url: url)
urlRequest.httpMethod = httpMethod.rawValue//get or post
if let httpBody = httpBody {
request.httpBody = httpBody
urlRequest.httpBody = httpBody
}
let task:URLSessionTask = session.dataTask(with: request as URLRequest) { (data, response, error) in
data(urlRequest: urlRequest)
}
/**
* New (Used for Custom URLRequests)
*/
static func data(urlRequest:URLRequest, completion: @escaping URLQuery = defaultURLQueryComplete) {
let session:URLSession = URLSession.shared
let task:URLSessionTask = session.dataTask(with: urlRequest as URLRequest) { (data, response, error) in
completion(data, response, error)
}
task.resume()
Expand Down Expand Up @@ -85,7 +91,7 @@ extension NetworkParser{
/**
* Default callback method for data(url: URL)
*/
static var defaultURLQueryComplete:URLQuery = { data, response, error in
static var defaultURLQueryComplete:URLQuery = { (data:Data?, response:URLResponse?, error:Error?) in
if let data = data, let str = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as String? {
Swift.print("str: \(str)")
}else{
Expand Down
4 changes: 3 additions & 1 deletion Sources/Utils/misc/range/RangeExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ extension Range {
*/
init(_ start:Bound,_ end:Bound){/*Conveninence initializer*/
//TODO: try ->return self.indices.count or self.underestimateCount()
self.init(start..<end/*<-CountableRange<Int>*/)//which->converts to Range<Int>
Swift.print("RangeExtension.init coud be broken in 4.2 ⚠️️")
self.init(uncheckedBounds: (( start, end)))///*<-CountableRange<Int>*/ //which->converts to Range<Int
//self.init(start..<end, end)<
}
var start:Bound {return self.lowerBound}//(0..<4).lowerBound -> 0
var end:Bound {return self.upperBound}//(0..<4).upperBound -> 4
Expand Down

0 comments on commit 395e8b8

Please sign in to comment.