Problem with SQLite.swift after migration to Swift 5 - sqlite.swift

I use SQLite.swift and after upgrading to Swift 5 an error appears in the library. Please help me rewrite the method.
Error:
'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead
Code:
public var datatypeValue: Blob {
return withUnsafeBytes { (pointer: UnsafePointer<UInt8>) -> Blob in
return Blob(bytes: pointer, length: count)
}
}

Till SQLite.swift doesn't release any update with the fix you could try modify manually the SQLite/Foundation.swift for fromDatatypeValue(_ dataValue: Blob) function and the computed property datatypeValue in this way:
public static func fromDatatypeValue(_ dataValue: Blob) -> Data {
return Data(dataValue.bytes)
}
public var datatypeValue: Blob {
return withUnsafeBytes { (pointer: UnsafeRawBufferPointer) -> Blob in
return Blob(bytes: pointer.baseAddress!, length: count)
}
}

Related

correct use of retryWhen operator with RxSwift 4.0.0

with RxSwift 3.6.1 I made this extension to ObservableType to get a new token after an error request:
public extension ObservableType where E == Response {
public func retryWithToken() -> Observable<E> {
return retryWhen { error -> Observable<Response> in
return error.flatMap({ (error) -> Observable<Response> in
if let myApiError: MyApiError = error as? MyApiError {
if (myApiError == MyApiError.tokenError) {
return Session.shared.myProvider.request(.generateToken)
} else {
return Observable.error(myApiError)
}
}
return Observable.error(error)
})
}
}
}
and then I can use it:
Session.shared.myProvider.rx
.request(.mySampleRequest)
.filterSuccessfulStatusCodes()
.retryWithToken()
.subscribe { event in
....
}.disposed(by: self.disposeBag)
but with RxSwift 4.0.0 now the sequence expect a
PrimitiveSequence<SingleTrait, Response>
someone can explain to me how to do the same with RxSwift 4.0.0? I try with an extension to PrimitiveSequence but I've some compilation errors.
I believe that has nothing to do with RxSwift but is a Moya change. MoyaProvider.rx.request returns Single which is a typealias for PrimitiveSequence which is not an ObservableType.
You declare your function upon the ObservableType.
So just do asObservable() before retryWithToken()

Crash fetching row value for custom type

I've defined an enum and made it conform to Value like so:
enum Name: Int {
//cases
}
extension Name: Value {
static var declaredDatatype: String {
return Int.declaredDatatype
}
static func fromDatatypeValue(_ intValue: Int) -> Name {
return Name(rawValue: intValue)!
}
var datatypeValue: Int {
return self.rawValue
}
}
Unfortunately I get the follow exception:
fatal error: unexpectedly found nil while unwrapping an Optional value
It happens in this function:
public func get<V: Value>(_ column: Expression<V>) -> V {
return get(Expression<V?>(column))!
}
The function in my enum extension is not called so I think this is a more general bug in SQLite.swift, but it only happens with my custom type.
Im using Xcode 8.3.3 and SQLite.swift ec7b589.

PromiseKit segmentation fault: 11

I'm trying to get a basic promise working with PromiseKit. However the following code wont compile:
import Foundation
import PromiseKit
class MyClass {
var myInt: Int?
func sample() -> Promise<AnyObject> {
return Promise { fulfill, reject in
fulfill(1)
}.then { data -> Int in
return 3
}
}
init() {
sample().then { data -> Void in
debugPrint("got data: \(data)")
}
}
}
This is the error I get:
command failed due to signal: segmentation fault: 11
This is pretty frustrating. Has anyone encountered this?
This is because Int is not AnyObject
func sample() -> Promise<AnyObject> {
return Promise { fulfill, reject in
fulfill(1)
}.then { data -> Int in
return 3
}
}
This is most likely fixed in Swift 3, however either of these will fix the compile:
func sample() -> Promise<Int> {
return Promise { fulfill, reject in
fulfill(1)
}.then { data -> Int in
return 3
}
}
Or:
func sample() -> Promise<AnyObject> {
return Promise { fulfill, reject in
fulfill(1)
}.then { data -> NSNumber in
return 3
}
}

Class declaration cannot close over value 'fulfill' defined in outer scope - Swift 2.0

I'm trying to convert my app from Swift 1.2 to Swift 2.0 and I'm encountering the following error:
class B {
func test() -> Promise<A> {
return Promise<A> { (fulfill, reject) -> Void in
anotherPromise.then { _ -> Void in
return fulfill(A()) // Class declaration cannot close over value 'fulfill' defined in outer scope
}
}
}
}
How can I make B (or the then closure) capture fulfill properly?
PS: Promise comes from PromiseKit, and I'm running Xcode 7 beta 1.
You should be able to workaround this by assigning fulfill to a local that you capture instead.
class B {
func test() -> Promise<A> {
return Promise<A> { (fulfill, reject) -> Void in
let innerFulfill = fulfill // close on this instead
anotherPromise.then { _ -> Void in
return innerFulfill(A()) // Class declaration cannot close over value 'fulfill' defined in outer scope
}
}
}
}

Swift #autoclosure broke compatibility in v1.2 for enum cases

I just update Xcode to 6.3 and the Swift code that used to compile in Xcode 6.2 is not compiling now.
import Foundation
public enum Result<T> {
case Success(#autoclosure() -> T)
case Failure(NSError)
case Cancelled
public init(_ value: T) {
self = .Success(value)
}
public init(_ error: NSError) {
self = .Failure(error)
}
public init() {
self = .Cancelled
}
public var failed: Bool {
switch self {
case .Failure(let error):
return true
default:
return false
}
}
public var error: NSError? {
switch self {
case .Failure(let error):
return error
default:
return nil
}
}
public var value: T? {
switch self {
case .Success(let value):
return value()
default:
return nil
}
}
}
This line:
case Success(#autoclosure() -> T)
yields an error: 'autoclosure' attribute is only allowed on parameters, not on enum cases
Just removing #autoclosure doesn't solve the problem.
Correct. This has been removed, explicitly to prevent the case you provide. Autoclosures were not intended to be used this way, and the Swift team intentionally removed the ability to do so. In a Result type, this is dangerous because the closure will be re-evalated every time it is accessed. If there are side-effects in the closure, this can have quite surprising impacts. Even if it's just non-trivial, it can have surprising performance impacts.
The correct tool here is Box. See Rob Rix's Result for a good implementation of this type.
I think that I have found the solution. You only need to change 2 lines in your original implementation to make it work:
case Success(() -> T)
And
public init(#autoclosure(escaping) _ value: () -> T) {
For those who only want a simply solution without having to import external frameworks:
public enum Result<T> {
case Success(Box<T>)
case Failure(String)
public init(_ value: T) {
self = .Success(Box(value))
}
public init(_ error: String) {
self = .Failure(error)
}
}
// Due to current swift limitations, we have to include this Box in Result.
final public class Box<T> {
public let unbox: T
public init(_ value: T) { self.unbox = value }
}
public func success<T>(value: T) -> Result<T> {
return .Success(Box(value))
}
public func failure<T>(error: String) -> Result<T> {
return .Failure(error)
}

Resources