Use HTTPURLResponse in place of URLResponse

Tags: http swift3
Question!

I want to call an API and its a part of my app, but I want to make sure I get the status code of the site I am calling into (to prevent runtime errors). I can't get the status code for URLResponse, but I can get it for HTTPURLResponse. How do I use the latter in place of the former?

    let requestURLString = "my_http_url"
    let requestURL = URL(string: requestURLString)
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)
    let ETAreq = session.dataTask(with: requestURL!)
    {

        (data, response (this is the URLResponse), error) in

        if error == nil
        {

            //switch (this is where I will put switch of status code)

        }

    }


Answers

Though, URLResponse and HTTPURLResponse are renamed with losing NS-prefix. They still are classes and HTTPURLResponse is a subclass of URLResponse.

HTTPURLResponse

You can use as? or as!:

    if
        error == nil,
        let httpResponse = response as? HTTPURLResponse
    {
        switch httpResponse.statusCode {
        case 200:
            //...
            break
        //...
        default:
            break
        }
    } else {
        //error case here...
    }
By : OOPer


One possible solution:

/* just instead of writing 64 tags in a single row */
document.body.onload = function() {
  var out = '';
  for(var i=1; i<=8; i++)
for(var j=1; j<=8; j++)
  out += '<button class="col'+j+' c'+i+'"></button>';
  document.getElementById('board').innerHTML = out;
}
html,
body {
  height: 100%;
  width: 100%;
}
button {
  width: 12.5%;
  height: 12.5%;
  /* make sure that padding/border doesn't add up to dimensions */
  box-sizing: border-box;
  /* remove vertical spaces between lines */
  vertical-align: bottom;
}
button::-moz-focus-inner
/*really needed and working*/
{
  border: 0;
  padding: 0;
}
.c1 {
  /*For coloring the buttons*/
  background-color: #cdaf95;
}
/*Here is the code for classes c2 -c7*/

.c8 {
  background-color: #5e2612;
}
.col1 {
  padding: 0;
  border: 0;
}
/*Here is code for classes col2-col7*/

.col8 {
  padding: 0;
  border: 0;
}
/*Board is the id of the division*/

#board {
  height: 50%;
  width: 50%;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: 400px;
  max-height: 400px;
  position: absolute;
  margin: auto;
  background-color: white;
  /* make minimum height of lines of buttons as small as possible */
  line-height: 1px;
}
<body bgcolor="black">
  <style>
  </style>
  <div id="board">
  </div>
</body>

The main trick is to remove inter-tag whitespace characters, including line breaks (as already mentioned) and to make the default line height less the height of the buttons (since they behave as inline-block elements, they can't make the line lower than its default height, only higher).

But wouldn't it be better to use Flexbox for this layout?



You can use this to remove paddings on all items

*{    
    padding:0;
    margin:0;
}


This video can help you solving your question :)
By: admin