0

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
  • 84,577
  • 15
  • 123
  • 161
  • `dispatch_async` is a `GCD` command, not related to networking at all. And your `postData : AmyObject ...` works fine in Xcode 7. – Adam Sep 30 '15 at 07:49
  • 1
    What are the problems? What is the error? – Dejan Skledar Sep 30 '15 at 07:49
  • @Adam thank you for the answer, wrong function has been pasted here. sorry. – Ehsan Razm khah Sep 30 '15 at 08:24
  • @DejanSkledar the problem is let postData : AnyObject = ["username":username , "password":password] ; makes Value of type '[String : String?]' does not conform to specified type 'AnyObject' Error – Ehsan Razm khah Sep 30 '15 at 08:24
  • @EhsanRazmkhah I added the error message to the question, because it is an important clue in diagnosing the problem. – JeremyP Sep 30 '15 at 10:56
  • possible duplicate of [How to assign a Dictionary to AnyObject in swift](http://stackoverflow.com/questions/29722774/how-to-assign-a-dictionary-to-anyobject-in-swift) – Sahil Kapoor Sep 30 '15 at 12:41

2 Answers2

1

I assume you are passing parameters to a request which require key as String and value can be AnyObject. Try this:

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

Also make sure username and password are not optional, and in that case, use ! to unwrap them if you are sure values wil be there.

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

or you can use

let postData: [String: AnyObject] = ["username":username ?? "", "password":password ?? ""]
Sahil Kapoor
  • 11,183
  • 13
  • 64
  • 87
1

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"
}
JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • let postData : AnyObject = ["username":username , "password":password!] still returns error. Sometimes the postdata might be null, not only the value. I need it to be full empty. something like let postData: AnyObject = "" – Ehsan Razm khah Sep 30 '15 at 11:14