Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nil is not compatible with the expected argument type '[NSObject : AnyObject]'

Tags:

ios

swift

I am using XCode 7.2 and Swift 2.2. I have a function that accepts a dictionary([NSObject : AnyObject]) as a parameter. However, I want to call this function without using that dictionary or in other words by making the parameter nil. If I do that, this is thrown:

nil is not compatible with the expected argument type '[NSObject : AnyObject]'

My code is

self.silentPostData(
  persist.getObject(
    mdmiosagent_Constants.SERVERNAMEKEY) as String, 
    serverport: persist.getObject(mdmiosagent_Constants.SERVERPORTKEY) as String,
    serverurl:  mdmiosagent_Constants.NATIVE_APP_SERVLET,
    parameters: nil ,
    urldata: jsonData
  )
)

The parameter in conflict is named parameters in the code. Thank you in advance.

like image 721
Maneesh Sharma Avatar asked Mar 21 '16 11:03

Maneesh Sharma


1 Answers

Only nullable types (such as optionals or types that conform to the protocol NilLiteralConvertible) can be nil, or compared to nil. Therefore, you have following options:

1. Make the parameter optional

convert the parameter of the function to [NSObject : AnyObject]?

2. Pass an empty dictionary

just call:

self.silentPostData(
  persist.getObject(
    mdmiosagent_Constants.SERVERNAMEKEY) as String, 
    serverport: persist.getObject(mdmiosagent_Constants.SERVERPORTKEY) as String,
    serverurl:  mdmiosagent_Constants.NATIVE_APP_SERVLET,
    parameters: [:],
    urldata: jsonData
  )
)
like image 147
Daniel Avatar answered Sep 19 '22 08:09

Daniel