NSHTTPCookie.cookieWithProperties Return nil
If you are trying to create a NSHTTPCookie using the cookieWithProperties function and getting back nil instead of a NSHTTPCookie instance, the dictionary that you are passing to the function must be wrong or incomplete.
// Wrong properties dictionary var properties = [NSHTTPCookieName: "YOUR-COOKIE-NAME", NSHTTPCookieValue: "Cookie Value"] // nil cookie var cookie = NSHTTPCookie.cookieWithProperties(wrongProperties)
After executing this code, the cookie will be nil. This occurs because the properties dictionary does not contain the right keys and values. The documentation of the function cookieWithProperties says: "To successfully create a cookie, you must provide values for (at least) the NSHTTPCookiePath, NSHTTPCookieName, and NSHTTPCookieValue keys, and either the NSHTTPCookieOriginURL key or the NSHTTPCookieDomain key.".
Following exactly what I just quoted, our code will look like this:
var properties = [ NSHTTPCookieOriginURL: "http://example.com", NSHTTPCookiePath: "/", NSHTTPCookieName: "foo", NSHTTPCookieValue: "bar" ] var cookie = NSHTTPCookie.cookieWithProperties(properties)
The execution of the code above will end up with the cookie being an instance of NSHTTPCookie.
Cheers.
















