How to solve Swift warning: Constant 'value' inferred to have type 'AnyClass?' (aka 'Optional AnyObject.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, ...
Inferred to have type 'AnyClass', which may be unexpected
You actually mean AnyClass
here, so you just need to tell the compiler that:
if let runningTests: AnyClass = NSClassFromString("XCTestCase") {
return false
}
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
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)
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.
Variable 'offerCardsShuffled' inferred to have type '()', which may be unexpected
Your shuffle
method modifies the original collection, it does not return a new collection.
Change:
var offerCardsShuffled = offerCards.shuffle()
to:
offerCards.shuffle()
and replace the use of offerCardsShuffled
with offerCards
in the remaining code.
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
Reading info.plist: inferred to have type AnyObject which may be unexpected
Xcode generally warns about variables which are inferred as AnyObject
orAnyObject?
. You can add a type annotation (as Xcode suggests) to silence the warning:
if let info : AnyObject = ...
I can only speculate why Xcode behaves like this, but one reason could
be that you are "forced" to be aware that info
might be of any object type
and not perhaps the one that you expect.
So even if info.integerValue
compiles,
it may crash at runtime if the object is not of
the expected type. In your case, if the dictionary value is not a NSNumber
or NSString
then info.integerValue
with throw an exception.
The safe way is to do an optional cast:
if let info = NSBundle.mainBundle().objectForInfoDictionaryKey("v") as? NSNumber {
value = info.integerValue
} else {
value = 0
}
Do to the automatic re-mapping between NSNumber
and Int
you can write that also as
if let info = NSBundle.mainBundle().objectForInfoDictionaryKey("v") as? Int {
value = info
} else {
value = 0
}
Or as a one-liner using the nil-coalescing operator ??
:
let value = NSBundle.mainBundle().objectForInfoDictionaryKey("v") as? Int ?? 0
Related Topics
Hittest Prints Ar Entity Name Even When I am Not Tapping on It
Calculate the Number of Dimensions of a Multi-Dimensional Array in Swift
How to Reduce the Opacity of the Shadows in Realitykit
Http Request Swift Providing Parameters
Can Not Cast Value of Type Nstaggedpointerstring to Nsdictionary
Making a Http Post Request in Swift 2
Code Highlighting in Latex for Swift
How to Calculate the Energy Per Bin in a Dft
How to Use Tabs to Evenly Space Out Description Strings in Swift
How to Segue Values When My Viewcontroller Is Embedded in an Uinavigationcontroller
Deep Copy of Cmimagebuffer or Cvimagebuffer
Swift 3 Issue with Cvararg Being Passed Multiple Times
How to Access Modifiers of a View in Swiftui
Need Explanation About Random Function Swift
Swift Generics: Non-Nominal Type Does Not Support Explicit Initialization