Constant 'Result' Inferred to Have Type (), Which May Be Unexpected

constant 'result' inferred to have type (), which may be unexpected

Guessing from the errors, I expect that performOperation() is supposed to return Double? (optional double) while if fact it returns nothing.

I.e. it's signature is probably:

func performOperation(operation: String) {
// ...
}

.. while in fact it should be:

func performOperation(operation: String) -> Double? {
// ...
}

Reason why I think so is that this line: if let result = brain.performOperation(operation) is call "unwrapping the optional" and it expects that the assigned value is an optional type. Later you assign the value that you unwrap to the variable that seems to be of Double type.

By the way, the shorter (and more readable) way to write the same is:

displayValue = brain.performOperation(operation) ?? 0.0

constant ... inferred to have type '()?', which may be unexpected

Your developerButton is the result of the text assignment text = "developer", which returns Void (nothing, ()). The assignment is conditional and that's why you are getting Void? (or ()?).

Split your two assignments correctly.

let developerButton = UIButton(type: .system)
developerButton.titleLabel?.text = "developer"

Also note that you should use setTitle instead of setting title text directly:

let developerButton = UIButton(type: .system)
developerButton.setTitle("developer", for: .normal)

swift constant inferred to have type () which may be unexpected

how do I otherwise cause three async things to happen in parallel and then await them

You use a task group, as explained in the Structured Concurrency video.

https://developer.apple.com/documentation/swift/3814991-withtaskgroup?changes=__1

Each call to the group's async method is an opportunity for you to call an asynchronous method to be run in parallel. When all calls are finished, the await on the task finishes and we proceed.

Alternatively I suppose you could declare the type of your a, b, and c to be () as suggested by the FixIt. But that seems rather a cheap approach. Better to do this properly.

So for example I made up a thing that behaves like I imagine your Mutex might behave:

class Waiter {
func go(after:TimeInterval, f: @escaping ()->()) async {
await withUnsafeContinuation { (c:UnsafeContinuation<Void,Never>) in
DispatchQueue.global().asyncAfter(deadline:.now()+after) {
f()
c.resume()
}
}
}
}

Then this compiles and runs fine:

    let w = Waiter()
async let x:() = w.go(after: 1) { print(1) }
async let y:() = w.go(after: 1) { print(2) }
async let z:() = w.go(after: 1) { print(3) }
let _ = [await x, await y, await z]

However, I still think a task group is nicer.

    let w = Waiter()
print("start")
await withTaskGroup(of:Void.self) { group in
for i in [1,2,3] {
group.async {
await w.go(after:1) { print(i) }
}
}
}
print("done")

Constant 'spacesLeft' inferred to have type '()', which may be unexpected Swift

For the first warning, subtract returns Void, so use subtracting:

let spacesLeft = allSpaces.subtracting(playerOneMoves.union(playerTwoMoves))

For the second error, advancedBy is deprecated, you may change like this:

if spacesLeft.count > 0 {
nextMove = spacesLeft[spacesLeft.index(spacesLeft.startIndex, offsetBy: Int(arc4random_uniform(UInt32(spacesLeft.count))))]
}

Constant 'weight' inferred to have type '()', which may be unexpected

The let weight = ... line contains two assignments which is not supported (it never was).

Probably you want this

@IBAction func weightSliderChanged(_ sender: UISlider) {
let weight = String(format: "%.2f", sender.value)

print(weight)

weightLabel.text = "\(weight) kg"
}

Constant inferred to have type '()', which may be unexpected - Replacing dispatch_once in Swift

This will definitely execute the block once and you can specify the type as Void so that compiler does not complain.

let executeStartup: Void = {
self.executeUnsafeStartupWithConfig(config: MPConfig.config.configWithAppKey(appKey: appKey, withAppId: appId, withAccountId: accountId, forProduction: inProduction), authToken: authToken)
}()

You can also use lazy var executeStartup: Void as this will also ensure the block is executed once.

How to solve Swift warning: Constant 'value' inferred to have type 'AnyClass?' (aka 'OptionalAnyObject.Type'), which may be unexpected?

You can silence the warning by modifying your code to:

func configure(with classesReuseRegistry: [String: AnyClass?]) {
var collectionView = UICollectionView() // Temp collection var for testing purposes

for classDict in classesReuseRegistry {
collectionView.register(classDict.value, forCellWithReuseIdentifier: classDict.key)
}
}

I think that the warning is caused by having AnyClass, which is a type alias, as dictionary value, and not a value type like Int, String, ...

Swift 4: Variable 'value' inferred to have type 'Void', which may be unexpected?

That's because the function newUserRef.setValue(...) does not return anything. Change your code to this:

let value = ["Image\(self.number)": "\(downloadUrlArray)"]
newUserRef.setValue(value)
addImageURLToDatabase(uid: uid!, values: value)

Why the warning: Constant name inferred to have type (), which may be unexpected?

The reason is that you are using the wrong function (former sortInPlace):

In Swift 3

  • sort() has been renamed to sorted()
  • sortInPlace() has been renamed to sort()

Therefore it's

let sortedEmployees = employees.sorted { (e1:Employee, e2:Employee) -> Bool in
e1.lastName < e2.lastName
}

Source: Swift Evolution: Apply API Guidelines to the Standard Library



Related Topics



Leave a reply



Submit