Swift 2: How can i assign a dictionary to AnyObject?

Ehsan Razm khah

I was using swift 1.2 and everything was going fine. after upgrading my Xcode to 7 . I faced some weird problems.

My code was :

let postData : AnyObject = ["username":username , "password":password] ;

I need this variable to be AnyObject, because

    let jsonObject : AnyObject = postData ;
    let jsonString = JSONStringify(jsonObject)
    let data1 = jsonString.dataUsingEncoding(NSUTF8StringEncoding)

    let task1 = NSURLSession.sharedSession().uploadTaskWithRequest(request, fromData: data1) {
        (Data, Response, Error) -> Void in

needs a Anyobject for post Data header.

The error is

 Value of type '[String : String?]' does not conform to specified type 'AnyObject' 

can any one help me?

JeremyP

The problem is your password variable is an Optional<String>. This means the conversion from Swift dictionary to AnyObject (I think it tries to convert it to an NSDictionary) will fail.

If you do

let postData : AnyObject = ["username":username , "password":password!]

It should work unless the password is nil (check that before creating the dictionary)

If you want to be able to have null passwords in the output, you can do this in your dictionary

let postData : [String : AnyObject] = ["username":username , "password":password ?? NSNull()]

The following works

let pw: String? = "pw"
let pw2: String? = nil
var foo: [String : AnyObject] = ["bar" : pw ?? NSNull(), "baz" : pw2 ?? NSNull()]

let data = try NSJSONSerialization.dataWithJSONObject(foo, options: NSJSONWritingOptions.PrettyPrinted)

let str = NSString(data: data, encoding: NSUTF8StringEncoding)!

print(str)

And prints

{
  "baz" : null,
  "bar" : "pw"
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to assign a Dictionary to AnyObject in swift

How to add an Array<String> to a Dictionary<String, AnyObject> in Swift 2

How to convert a NSUSerdefaults AnyObject? to SWIFT Array so that I can append to it?

How can i extract data from an anyObject in Swift

Swift3, How to add AnyObject in a dictionary?

How can I assign a Swift struct by reference?

How can I save a dictionary to Firebase in swift?

How can I parse json with a dictionary in swift?

How can I assign a tuple to a dictionary key in Python?

How can I assign a variable to a value in a dictionary in Python?

How can I assign a variable from a string inside a json dictionary?

How can I assign a 2D array variable to a big floating matrix in Swift?

Swift - [NSObject : AnyObject]!' is not a subtype of 'Dictionary<String, AnyObject>

How to cast a struct to Anyobject in Swift 2?

Optional AnyObject values in Swift dictionary

How can this [AnyObject] return as AnyObject?

How can I get the sum for values in a dictionary in swift?

How can I pass enum objects as a dictionary key in Swift?

How can I have a dictionary of dictionaries with Codable in Swift4

How can I make an extension for extracting keys of a Dictionary in Swift?

how can i update the quantity values in my dictionary ios Swift

How can I use Swift’s Codable to encode into a dictionary?

How can I get key's value from dictionary in Swift?

How can I group TableView items from a dictionary in swift?

How can I use a Swift enum as a Dictionary key? (Conforming to Equatable)

How can I safely unwrap deeply nested values in a dictionary in Swift?

How can I parse this JSON Pokemon Dictionary? (swift 3)

Swift - How can I use a dictionary to give text for tableView cells?

Why can I successfully cast a Swift array struct to AnyObject?