「Swift Sample」の版間の差分
ナビゲーションに移動
検索に移動
(→処理) |
|||
68行目: | 68行目: | ||
print(stringResult!) | print(stringResult!) | ||
+ | } | ||
+ | </pre> | ||
+ | ==[[正規表現]]== | ||
+ | <pre> | ||
+ | func parseArp(arpResult: String?) { | ||
+ | if let input = arpResult { | ||
+ | do { | ||
+ | let pattern = #"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"# | ||
+ | let regex = try NSRegularExpression(pattern: pattern, options:[]) | ||
+ | |||
+ | for subinput in input.split(separator: "\r\n") { | ||
+ | let line = String(subinput) | ||
+ | let maches = regex.matches(in: line, options: [], range: _NSRange(0..<line.count)) | ||
+ | |||
+ | print(line) | ||
+ | |||
+ | for mach in maches { | ||
+ | for i in 0 ..< mach.numberOfRanges { | ||
+ | let start = line.index(line.startIndex, offsetBy: mach.range(at: i).location) | ||
+ | let end = line.index(start, offsetBy: mach.range(at: i).length) | ||
+ | let text = String(line[start..<end]) | ||
+ | print(text) | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } catch { | ||
+ | print("RegEx fail.") | ||
+ | } | ||
+ | } else { | ||
+ | print("arp -a result : error") | ||
} | } | ||
</pre> | </pre> |
2021年4月6日 (火) 15:00時点における版
| Swift | SwiftUI |Xcode | Mac |
目次
Swift Sample
Network
データ取得
let url = URL(string: "https://www.typea.info/blog/")! let task = URLSession.shared.dataTask(with: url) { data, response, error in if let error = error { print("\(error)\n") return } guard let data = data, let response = response as? HTTPURLResponse else { print("no data or no response.") return } if response.statusCode == 200 { if let text = String(bytes: data, encoding: .utf8) { print(text) } } } task.resume()
MacOSでエラーの場合
Error Domain=NSURLErrorDomain Code=-1003
- https://www.poly-rhythm.com/hostname-could-not-be-found/
- https://developer.apple.com/documentation/xcode/adding_capabilities_to_your_app
UI
バックグラウンドからUIを操作する
- observableobj が、ObservableObject の派生クラス
- contentフィールドに、@Published アノテーション
- Viewで、@ObservedObjectを付与しインスタンスを生成
- 上記で、バックグラウンドから、observableobj.contentを操作すると、UIはメインスレッドから触るように怒られる。
Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.
- DispatchQueue.main.syncで囲む
DispatchQueue.main.sync { observableobj.content = text }
処理
サブプロセスを起動
- プロセスを起動し、arp -a を呼び出し、出力
func arp() { let outputPipe = Pipe() func shell(path:String ,args: String...) -> Int32 { let task = Process() task.launchPath = path task.arguments = args task.standardOutput = outputPipe task.standardError = outputPipe task.launch() task.waitUntilExit() return task.terminationStatus } let _ = shell(path:"/usr/sbin/arp",args: "-a") let theTaskData = outputPipe.fileHandleForReading.readDataToEndOfFile() let stringResult = String(data: theTaskData, encoding: .utf8) print(stringResult!) }
正規表現
func parseArp(arpResult: String?) { if let input = arpResult { do { let pattern = #"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"# let regex = try NSRegularExpression(pattern: pattern, options:[]) for subinput in input.split(separator: "\r\n") { let line = String(subinput) let maches = regex.matches(in: line, options: [], range: _NSRange(0..<line.count)) print(line) for mach in maches { for i in 0 ..< mach.numberOfRanges { let start = line.index(line.startIndex, offsetBy: mach.range(at: i).location) let end = line.index(start, offsetBy: mach.range(at: i).length) let text = String(line[start..<end]) print(text) } } } } catch { print("RegEx fail.") } } else { print("arp -a result : error") }
© 2006 矢木浩人