Cannot assign value of type 'String' to type 'AnyObject?' , Swift 3, Xcode 8 beta 6
In b6, String no longer magically bridges to NSString. String is not a class; it's a struct. You need to do the bridging by hand:
dict["key"] = "value" as AnyObject
The fact that is
still seems to be bridging is likely a bug and should be reported.
It goes without saying that [String: AnyObject]
and [String: Any]
should be used as little as possible in your code.
(Make sure to follow the link Hamish provides in the comments below.)
Cannot assign a value of type '[NSObject : AnyObject]' to a value of type '[String : AnyObject]!'
While assigning the dictionary to constant variable, you don't required to type cast because you are already creating dictionary of type [String : AnyObject]
which required for assignment to the UITextView.linkTextAttributes
.
let attributes = [NSForegroundColorAttributeName:cell.textView!.tintColor,NSUnderlineStyleAttributeName:1]
cell.textView!.textView.linkTextAttributes = attributes
AnyObject not working in Xcode8 beta6?
The warning works as intended: the false
return of TestStruct() is AnyObject
, however, does not
The prior version of this answer perceived the warning,
'is' test is always true
as the bug, and contained some discussion as to why this perceived buggy warning would manifest itself. That TestStruct() is AnyObject
evaluated to false
at runtime, however, was perceived as expected behaviour.
Given the comments to the bug report filed by the OP (SR-2420), it seems the situation is the reverse: since Xcode 8/beta 6, the is
test should always evaluate to true
, and the bug the OP:s post is the fact that TestStruct() is AnyObject
evaluates to false
during runtime.
Joe Groff writes:
This is correct, because everything bridges to
AnyObject
now....
is
/as
AnyObject
always succeed for all types now. It's behaving
as intended.
The new SwiftValue
box for conversion from Swift values to Obj-C objects
(for additional details, see discussion in the comments below, thanks @MartinR)
It seems as if Swift values that are not explicitly implemented to be bridgeable to Obj-C objects via e.g. conformance to _ObjectiveCBridgeable
(see e.g. the following Q&A for details regarding _ObjectiveCBridgeable
), will instead automatically make use of the new SwiftValue
box to allow conversion to Obj-C objects.
The initial commit message for swift/stdlib/public/runtime/SwiftValue.mm reads:
Runtime: Implement an opaque 'SwiftValue' ObjC class to hold bridged values
If there's no better mapping for a Swift value into an Objective-C
object for bridging purposes, we can fall back to boxing the value in
a class. This class doesn't have any public interface beyond being
NSObject
-conforming in Objective-C, but is recognized by the Swift
runtime so that it can be dynamically cast back to the boxed type.
Cannot assign value of type String to type UILabel
You need to pass the String
instead of setting text to label, because when you correct it and set like this sendToDetailViewController.messageLabelDos.text = sendingText
, you will get nil error because messageLabelDos
is not initialize yet, so try like this. Create one string instance in DetailViewController
and use that inside prepareForSegue
for passing String and then use that String
instance in viewDidLoad
to assign Label
to text.
class ViewController: UIViewController {
//Your other methods
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SendDataSegue" {
if let sendToDetailViewController = segue.destinationViewController as? DetailViewController {
var sendingText = metadataObj.stringValue
sendToDetailViewController.messageDos = sendingText
}
}
}
}
Inside DetailViewController
var messageDos: String = ""
override func viewDidLoad() {
super.viewDidLoad()
self.messageLabelDos.text = messageDos
}
Cannot assign value of type '() - Void' to type '(() - Void)!'
It seems your issue is related to this:
SE-0103
Try chaging the method header of your addButton(_:action:)
to:
public func addButton(_ title:String, action:@escaping ()->Void)->SCLButton {
The diagnostic messages from new betas are so confusing and inadequate as usual, but making your property simply non-Optional var action:()->Void = {}
will give you a little more useful info.
Cannot assign value of type '(UIViewController?, NSError?) - Void' to type '((UIViewController?, Error?) - Void)?'
sorry, the answer was as simple as only had to change this line:
self.localPlayer.authenticateHandler = {(viewController: UIViewController?, error: NSError?) -> Void in
to
self.localPlayer.authenticateHandler = {(viewController: UIViewController?, error: Error?) -> Void in
my bad! sorry!
Unable to compile AWS CustomIdentityProvider on xcode 8 beta 6
Cast to NSDictionary instead of a Swift Dictionary:
return AWSTask(result: tokens! as NSDictionary)
Cannot convert value of type NSMutableDictionary? to expected argument type [NSObject: AnyObject]!
You need to make a variable to hold your NSMutableDictionary
then pass it to send()
method.
let dictionary = (builder?.build())! as NSMutableDictionary
tracker?.send(dictionary as [NSObject: AnyObject]!)
UPDATE: Another clean way to use in Swift 3
guard let tracker = GAI.sharedInstance().defaultTracker else { return }
tracker.set(kGAIScreenName, value: "ViewController")
guard let builder = GAIDictionaryBuilder.createScreenView() else {
return }
tracker.send(builder.build() as [NSObject:AnyObject])
Value of type String to type [String] in Swift
It's a simple fix. If you're trying to start the array over and only have that one value (carName.text
) stored inside of it, do this: pickOption = [carName.text]
If you're trying to add carName.text
into the array, do this: pickOption.append("\(carName.text
)". Good luck with your project!
Related Topics
Can't Understand How Collision Bit Mask Works
Swift Closures Causing Strong Retain Cycle with Self
Swiftui - Possible Memory Leak
Add Switch in Uitableview Cell in Swift
Diagnosing Exc_Bad_Instruction in Swift Standard Library
"Value of Type 'Authdataresult' Has No Member 'Uid'" Error
Swiftui Foreach Based on State Int
Swiftui Background Color of List MAC Os
Can't Use Storyboard Custom Instantiated Window Controller
Swift Combine Sink Stops Receiving Values After First Error
Check If Class Conforms to Protocol
Trying to Stream Audio from Microphone to Another Phone via Multipeer Connectivity
How to Drag and Drop a Sprite in Swift 3.0
Swift: Second Occurrence with Indexof
Bool Being Seen as Int When Using Anyobject
How to Check the Network Speed Using Swift
Conditionally Use View in Swiftui
Swiftui: How to Implement a Custom Init With @Binding Variables