首先是简单的WebView
引用于Webkit
代码极其简单~
可以配合顶部栏隐藏来实现一个网页的阅读
隐藏方法:
1 2 3 4 override func viewWillAppear (_ animated : Bool ) { super .viewWillAppear(animated) navigationController? .setNavigationBarHidden(true , animated: true ) }
关于Alamo
先贴基操及网址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 func alamofireGet1 () -> Void { let dataRequest:DataRequest = Alamofire .request("http://www.jianshu.com/u/23dd8d9701bf" ) print (dataRequest) dataRequest.response { (DDResponse ) in let stateCode = DDResponse .response? .statusCode print (stateCode! ) print (DDResponse .data! ) let dataStr = String .init (data: DDResponse .data! , encoding:String .Encoding .utf8) print (dataStr! ) let requestSwf = DDResponse .request print (requestSwf! ) print (requestSwf? .allHTTPHeaderFields as Any ) print (requestSwf? .cachePolicy as Any ) print (requestSwf? .httpBody as Any ) print (requestSwf? .httpMethod as Any ) let responseSwf = DDResponse .response print (responseSwf! ) print (DDResponse .error as Any ) print (DDResponse .metrics! ) } dataRequest.responseData { (data) in print (data) } dataRequest.responseJSON { (dataResponse) in print (dataResponse) print (dataResponse.request! ) print (dataResponse.response! ) print (dataResponse.data! ) print (dataResponse.result) print (dataResponse.timeline) } dataRequest.responseString { (dataResponse) in print (dataResponse) print (dataResponse.request! ) print (dataResponse.response! ) print (dataResponse.data! ) print (dataResponse.result) print (dataResponse.timeline) } dataRequest.responsePropertyList { (dataResponse) in print (dataResponse) print (dataResponse.request! ) print (dataResponse.response! ) print (dataResponse.data! ) print (dataResponse.result) print (dataResponse.timeline) } let dispathQu = DispatchQueue .main dataRequest.response(queue: dispathQu) { (ddataResponse) in print (ddataResponse) print (ddataResponse.request! ) print (ddataResponse.response! ) print (ddataResponse.data! ) print (ddataResponse.error! ) } print (dataRequest.progress) }
日报源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import Alamofireclass Alama { static func alamofireGet (someclosure :@escaping () -> Void ) { let dataRequest: DataRequest = Alamofire .request("https://news-at.zhihu.com/api/4/news/latest" ) dataRequest.responseJSON { d in do { let datas = try JSONDecoder ().decode(dataGet.self , from: d.data! ) for story in datas.stories { titles.append(story.title) imageUrls.append(story.image ?? "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1573901419402&di=4e251c785265a57a38477c2fa9385dc9&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201705%2F05%2F20170505194734_cGCea.thumb.700_0.jpeg" ) hints.append(story.hint) someclosure() } } catch { print ("error" ) } } } } Alama .alamofireGet(someclosure: tableView.reloadData)
写的时候纠结了一两天,为了一个异步处理发愁,简单来说就是网络请求的数据没有到数组中但是tableView就已经初始化了
就很难过,之后看了几遍看到了一个方法:**tableView.reload()**我的妈,我就怎么没有想到,于是乎,傻乎乎的加了个代码
1 2 Alama .alamofireGet()tableView.reload()
拜托这没用啊,就是在数据来之前我自己又刷新了一遍。。。
好吧。。。
于是乎今天请教J哥,好吧,闭包,那就看闭包,逃逸闭包,解决;
附尾随闭包及闭包知识复习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 var completionHandlers: [() -> Void ] = []func someFunctionWithEscapingClosure (completionHandler : @escaping () -> Void ) { completionHandlers.append(completionHandler) } func someFunctionWithNonescapingClosure (closure : () -> Void ) { closure() } class SomeClass { var x = 10 func doSomething () { someFunctionWithEscapingClosure { self .x = 100 } someFunctionWithNonescapingClosure { x = 200 } } } let instance = SomeClass ()instance.doSomething() print (instance.x)completionHandlers.first? () print (instance.x)