Home > other >  iOS development starts level. How I can use for_in for request API
iOS development starts level. How I can use for_in for request API

Time:01-21

iOS development starts level. How I can use for_in for request API.

I mean I have a code :

import UIKit

var ipName = ["192.168.1.0"..."192.168.1.254"] 
var request = URLRequest(url: URL(string: "http://192.168.1.1/")!)
for i in ipName {
    print("It's, \(i)")     }
let task = URLSession.shared.dataTask(with: request) { data, response, error in
print(String(decoding: data!, as: UTF8.self))
print(response)
print(error)
}
task.resume()

and I need to query all addresses from my network and select all who are connected ("for example" out of 254 addresses).

P.S - Sorry for my English.

CodePudding user response:

You need something like this, a numeric range and then String Interpolation to build the IP

let ipRange = 0...254
for i in ipRange {
    let url = URL(string: "http://192.168.1.\(i)")!
    let task = URLSession.shared.dataTask(with: url) { data, _, error in
        if let error = error { print(error); return }
        print(String(data: data!, encoding: .utf8) ?? "No readable string")
    }
    task.resume()
}
  •  Tags:  
  • Related