Swift Cannot Convert The Expression's Type 'Void' to Type 'string!'

Cannot convert return expression of type 'Void' to return type '(String, String, String)'

You are not getting any result back because you are trying to return the values that are inside a closure, so your function finish before you have something to return. You need to return your values using a completion handler. It means you will return the values only when you call it.

func grabData1(completionHandler:@escaping(String, String, String)->())  {

db.collection("News").document("Article 1").getDocument { (document, error) in
if error == nil {
if document != nil && document!.exists{
self.headline1 = document?.get("Headline").map(String.init(describing:)) ?? nil
var article = document?.get("Article").map(String.init(describing:)) ?? nil
var image1URL = URL(string: document?.get("Image") as! String)
completionHandler (headline1, article, image1URL)
}
}
}

}

Then you call your function like this:

grabData1() { headline, article, imageURL in 
print(headline)
print(article)
print(imageURL)
}

Of course you can improve it using a data model but this is the answer for your question.

Cannot convert the expression's type 'Void' to type 'String!'

Your problem is the location.coordinate.latitude and location.coordinate.longitude parameters. I can reproduce your problem if I make those parameters Int, for example. So, try:

XNGAPIClient.sharedClient().putUpdateGeoLocationForUserID("me" as String,
accuracy: 3000 as CGFloat,
latitude: CGFloat(location.coordinate.latitude),
longitude: CGFloat(location.coordinate.longitude),
ttl: 420 as Int,
success: { (JSON: AnyObject!) in },
failure: { (error: NSError!) in })

...that is, using the CGFloat constructor rather than the down casting as for those parameters. (I'd take a guess that there's something clever going on behind the scenes for the 3000 literal that looks like it should be an Int, otherwise that one probably wouldn't work, either...)

I'd also raise a bug with Apple for the very unhelpful error message. I've seen a few of those from calls to Objective C with the wrong parameter types.

Cannot convert the expression's type 'Void' to type 'CFAllocator!'

Figured it out.

var host: CFString = NSString(string: request.URL.host)
var port: UInt32 = UInt32(request.URL.port.unsignedIntegerValue)

var readStream: Unmanaged<CFReadStream>?
var writeStream: Unmanaged<CFWriteStream>?

CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &readStream, &writeStream)

inputStream = readStream!.takeUnretainedValue()
outputStream = writeStream!.takeUnretainedValue()

inputStream!.delegate = self
outputStream!.delegate = self

inputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
outputStream!.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

inputStream!.open()
outputStream!.open()

The reason this error comes up is because all the values have to be the exact type the "CreatePairWithSocket.." function is asking for. I did some research and also found this very helpful question.

HomeKit completion block in Swift: Cannot convert the expression's type 'Void' to type 'String!'

The parameters for your completionHandler closure are reversed, they should be:

(home: HMHome!, error: NSError!)

Also note that you don't need to specify the types for the parameters, since the method signature specified them for you - thus you can simply list the parameter names you want to use, and they will automatically be assured to be of the correct type, for example:

homeManager.addHomeWithName("My House", completionHandler:{
home, error in
if error { NSLog("%@", error) }
})

--edit--

Also note that when I wrote 'you can simply list the parameter names you want to use, and they will automatically be assured to be of the correct type', that is to say that they will be typed according to the order in which they are listed - e.g. if you had used error, home in instead, then those would be your parameter names however the parameter error would be of type HMHome!, and home would be of type NSError! (since that is the order in which they appear in the parameter list for the closure in the method's signature)

captureStillImageAsyncronouslyFromConnection - Cannot convert the expression's type

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.

Computed property with UNNotificationRequest returns Cannot convert return expression of type 'Void' to return type '[UNNotificationRequest]'

You can probably use dispatch groups like as follows. What you are actually doing is waiting for all your notifications to be retrieved from a thread and then continue



var notifications: [UNNotificationRequest] {
var retrievedNotifications: [UNNotificationRequest] = []

let group = DispatchGroup()
group.enter()

// avoid deadlocks by not using .main queue here
DispatchQueue.global(attributes: .qosDefault).async {
center.getPendingNotificationRequests { notifications in
retrievedNotifications = notifications
group.leave()
}
}

// wait ...
group.wait()

return retrievedNotifications
}

Can not convert expression's type int to type void swift

There are two errors in your code. First (as already noticed by SolaWing),
the allocated pointer must be a pointer to CChar (aka Int8).
This can be done with

var machine = UnsafeMutablePointer<CChar>.alloc(...)

or

var machine = UnsafeMutablePointer<CChar>(malloc(...))

Second, the size variable must not be an optional but a an initialized
size_t variable which is passed as inout parameter to sysctlbyname():

var size = size_t(0)
sysctlbyname("hw.machine", nil, &size, nil, 0)
var machine = UnsafeMutablePointer<CChar>(malloc(size + 1))
sysctlbyname("hw.machine", machine, &size, nil, 0)
machine[size] = 0
let machineString = String.fromCString(machine)!
free(machine)
println(machineString)

Alternatively, you can create a Swift array instead of allocating memory,
this has the advantage that the memory is released automatically:

var size = size_t(0)
sysctlbyname("hw.machine", nil, &size, nil, 0)
var machine = [CChar](count: size + 1, repeatedValue: 0)
sysctlbyname("hw.machine", &machine, &size, nil, 0)
machine[size] = 0
let machineString = String.fromCString(machine)!
println(machineString)

The above code compiles with Swift 1.2 (Xcode 6.3 beta).
In Swift 1.1 (Xcode <= 6.2), size_t has to be converted to Int
at

var machine = [CChar](count: Int(size) + 1, repeatedValue: 0)

machine[Int(size)] = 0


Related Topics



Leave a reply



Submit