moozzyk / SignalR-Client-Swift

Swift SignalR Client for Asp.Net Core SignalR server
MIT License
356 stars 136 forks source link

NSNull as invoke param support #288

Closed shotkee closed 12 months ago

shotkee commented 12 months ago

Some solution for "requires that 'NSNull' conform to 'Encodable'" error for hub connection invoke method for param output like { "some_param1": "1231", "some_param2": null} please

image
moozzyk commented 12 months ago

Please show exactly what you're trying to do. I don't quite understand the relation between invoke and { "some_param1": "1231", "some_param2": null}. Are you trying to send a struct as a parameter? If so, show how you defined your struct.

shotkee commented 12 months ago

I suppose to use invoke method like this

self.connection?.invoke(method: "SomeServerMethod", "123", NSNull(),  "123") { error in }

for invocation data like : Optional

moozzyk commented 12 months ago

NSNull is a foreign concept in Swift and is not supported by default by Encodable, JSONEncoder etc. I haven't tried but I think using just nil doesn't work because the compiler is not able to figure the generic type. This is why your workaround with creating a variable works - it tells the compiler what type it should expect. I admit it is pretty ugly. But you should be able to reduce the ugliness by invoking this method like this:

self.connection?.invoke(method: "SomeServerMethod", "123", nil as String?,  "123") { error in }
shotkee commented 12 months ago

thank you for explanation