KMM iOS mismatch type ChallengeHandler - kotlin-multiplatform

Type mismatch: inferred type is ChallengeHandler /* = (NSURLSession, NSURLSessionTask, NSURLAuthenticationChallenge, (NSURLSessionAuthChallengeDisposition /* = Long */, NSURLCredential?) -> Unit) -> Unit */ but ChallengeHandler /* = (NSURLSession, NSURLSessionTask, NSURLAuthenticationChallenge, (NSURLSessionAuthChallengeDisposition /* = Int */, NSURLCredential?) -> Unit) -> Unit */ was expected
Basically above error logs happen when migrate to latest client ktor 2.0.3 and using kotlin 1.7.10.
Is this a bug from ktor? how can we deal with this issue? should we just postponed update latest ktor until this issue fixed?
I think platform.darwin.NSInteger is recognized as Long, thus it got mismatch.
when you search inside NSURLSessionAuthChallengeDisposition this is platform.darwin.NSInteger but somehow IDE recognized as Long not Int, so I guess this is a bug?
Run the build also failure by same error above
Can't do anything. tried to create extension to use Int, still same..
Tried to cast manually to Int also still error.
Can't build too because of this error.
Anyone have solutions?

This can actually compiles just fine on native iOS. Should just ignore the error and provide an explanation in the comments section.

Related

Xcode 10 Swift build error: "Converting non-escaping value to 'T' may allow it to escape"

I'm using the Swift-VectorBoolean library, which is currently on Swift 3.2, not yet updated for Swift 4.2, but should still work on Xcode 10. Running this on Xcode 9, it works fine. On Xcode 10, it gives an error that I'm not sure how to fix. This is the function in question:
typealias MyPathApplier = #convention(block) (UnsafePointer<CGPathElement>) -> Void
// Note: You must declare MyPathApplier as #convention(block), because
// if you don't, you get "fatal error: can't unsafeBitCast between
// types of different sizes" at runtime, on Mac OS X at least.
private func myPathApply(_ path: CGPath!, block: MyPathApplier) {
let callback: #convention(c) (UnsafeMutableRawPointer, UnsafePointer<CGPathElement>) -> Void = { (info, element) in
let block = unsafeBitCast(info, to: MyPathApplier.self)
block(element)
}
path.apply(info: unsafeBitCast(block, to: UnsafeMutableRawPointer.self), function: unsafeBitCast(callback, to: CGPathApplierFunction.self))
}
The error is on that last line, path.apply, highlighting the first unsafeBitCast:
Converting non-escaping value to 'T' may allow it to escape
I'm not sure how to modify this code to remove the error, or if this is an issue with Xcode 10. It should be able to compile Xcode 3.2 code.. even updating the code base to Swift 4 in Xcode 9, it still has the same issue.
EDIT: Changing this with the answer from #Vyacheslav allows it to compile, but it gets a runtime error:
"Fatal error: Can't unsafeBitCast between types of different sizes"
There is a comment provided above the function which I didn't include in the sample above:
// Note: You must declare MyPathApplier as #convention(block), because
// if you don't, you get "fatal error: can't unsafeBitCast between
// types of different sizes" at runtime, on Mac OS X at least.
I still don't understand what needs to change here to ensure it builds, and we also don't get a runtime error.
Use
private func myPathApply(_ path: CGPath!,
block: #escaping #convention(block) (UnsafePointer<CGPathElement>) -> Void) {
}
#noescape is now by default

Cannot assign value of type '(UIImage?, PHAsset?)' to type UIImage? Swift 2.2

I updated to XCode 7.3 - swift 2.2 and I received a compile time error as mentioned in the title. It had no issues in swift 2.1. As per basics of swift, '?' variables can be compared to nil. I googled for possible solutions, but could not find the appropriate solution.
I think the problem was not in swift update but in ALCameraViewController update. Now it takes completion with two parameters
public typealias CameraViewCompletion = (UIImage?, PHAsset?) -> Void
So you need to change this
(image) -> Void in ...
to this
(image, asset) -> Void in ...
Actually now in your code Swift treats image as tuple of two values so also you can change all calls to image in your code with calls to image.0 that will address first closure parameter that is your UIImage?

Does AHEasing run in Swift?

I'm trying to use AHEasing in my Swift project. I've installed it via cocoapods and included this line in my bridging header:
#import <AHEasing/CAKeyframeAnimation+AHEasing.h>
I am able to access the function in CAKeyframeAnimation+AHEasing.h just fine, but when I try
var alphaAnimation = CAKeyframeAnimation.animationWithKeyPath("alpha", function: QuadraticEaseInOut, fromValue: 1.0, toValue: 0.0)
I get the error
Cannot invoke 'animationWithKeyPath' with an argument list of type '(String, function: (Double) -> Double, fromValue: Double, toValue: Double)'
After a little digging, I confirmed that QuadraticEaseInOut is indeed being converted to a (Double) -> Double closure, while the type for that parameter, AHEasingFunction, is being converted into a CFunctionPointer<((Double) -> Double)>.
Is there a way to convert between these 2 types? Has anyone successfully used AHEasing in Swift? Is there another work around that I'm not seeing?
I've heard reports from some people that this is a Swift 1.2 issue, that is no longer a problem in Swift 2.
Either way, I just worked around the problem by implementing the functionality that I needed from AHEasing entirely in Swift.

captureStillImageAsyncronouslyFromConnection - Cannot convert the expression's type

I'm fresh to Swift so I'm still ironing out some of the finer details; I apologize if I'm missing something obvious. I've browsed around and found solutions to similar errors in different method calls, but those answers haven't helped me in figuring out my issue. I make this call
captureOutput.captureStillImageAsynchronouslyFromConnection(port) {
(imageDataSampleBuffer: CMSampleBufferRef?, error: NSError?) -> Void in
// TODO
}
which is flagged with the following error:
Cannot convert the expression's type '(#lvalue AVCaptureInputPort, (CMSampleBufferRef?, NSError?) -> Void)' to type 'AVCaptureConnection!'
The issue is clearly with my closure. Any help is greatly appreciated.
The issue is clearly with my closure
Actually, no. The issue is clearly with port. This argument is supposed to be an AVCaptureConnection - but you are supplying an AVCaptureInputPort instead.

How do I "generify" a closure type alias in Swift?

In order to make my code easier to read, I am using type aliases in Swift for various types of closures. I have the following basic set of closures:
public typealias FailureClosure = (error: NSError?) -> Void
public typealias ProgressClosure = (progress: Float32) -> Void
public typealias BasicClosure = () -> Void
I would like to add a closure typealias that supports generic arrays, but I can't seem to figure out the syntax for it. This is as far as I am able to get, but I get the compile time error "Use of undeclared type 'T'"
public typealias ArrayClosure = <T>(array:[T]?) -> Void
Does anybody know how to do this? Or even if it is possible?
No, this is not currently possible. If it were possible, the syntax you'd expect would be:
public typealias ArrayClosure<T> = (array:[T]?) -> Void
You would then use it like ArrayClosure<Int>. But it's not currently legal.
That said, I don't really recommend these type aliases. They obscure more than they illuminate. Compare this signature:
func foo(onError: FailureClosure)
with:
func foo(onError: NSError? -> Void)
To save just a couple of characters, you force the caller to guess what FailureClosure passes. The error or progress tags don't really help you (you still need to use progress in ...).
The one case that makes a lot of sense is around progress, but I think the type you want there is:
public typealias Progress = Float32
Don't get me wrong here, type aliasing can be very helpful when it creates a new type. Progress is a type that happens to be implemented as as float. But much of what you're doing (definitely with ArrayClosure, and to a lesser extent with the others) is just creating new syntax without creating a new type, and that is more often confusing than helpful.
To call out your specific example, and why overuse of type aliases can cause you to overcomplicate your design:
func foo(failure: ((error: NSError?) -> ())? = nil)
You're right that this is really complicated. Compare:
func foo(failure: NSError -> Void = {_ in return})
Two big changes here. There's no reason to have a failure block that takes an optional error. Always pass an error (if there's no error, why would failure be called?). And there's no reason for the failure block to be optional. If you really want a default value, just make the default value do nothing. Two optionals gone, and all the consuming and implementing code gets simpler. Always think carefully about whether something absolutely must be optional. Optionals add a lot of complexity; don't add them lightly.
Personally, I'd probably do this with an overload instead in many cases:
func foo(#failure: NSError -> Void) { ... }
func foo() {
foo(failure:{ _ in return })
}
I just think it's a little easier to understand what's going on. But either way is fine.
EDIT (Dec 2014): After writing Swift for a few more months, I've grown more fond of #David's approach in the comments below, which is to use an optional for the closure, but not for the error. Particularly given Swift's optional chaining syntax (failure?()), it often does wind up being clearer.
func foo(failure: (NSError -> Void)? = nil)
Swift 4.1 supports generic type aliases. You can use this feature to provide a name for function with generic parameters.
You may have to use such declaration:
public typealias ArrayClosure<T> = ([T]?) -> Void

Resources