Swift learns the second practice--swift project time Movie
A previous OC Practice project was re-written with Swift, as the compatibility of the update to Swift for the Xcode version is also changing, and this version applies to xcode6.1.
This project uses Swift to encapsulate HTTP in the iOS official SDK, using Swift's method of loading the network images asynchronously. Practice the method of using Swift to operate the interface layout, jump interface and so on.
The following is the core code for the encapsulated download class:
Private var httpconnection:nsurlconnection?class zyhhttprequset: nsobject, Nsurlconnectiondatadelegate{ var requesturl:string? var downloaddata:nsmutabledata=nsmutabledata () var isDownloadSuccess:Bool? var delegate:ZYHHttpRequestDelegate? class func Requestformurl (url:nsstring)->zyhhttprequset{ var Oldrequest:zyhhttprequset?=zyhhttprequestmanager.sharedhttprequestmanager (). Requestforkey (URL) if (Oldrequest != nil) {       PRINTLN ("This task exists") return oldRequest! } //New Download Task &Nbsp; var request:zyhhttprequset=zyhhttprequset () request.requestUrl=url Request.startrequesturl (URL) Zyhhttprequestmanager.sharedhttprequestmanager (). AddTask (Request, key: url) return request } func stop () { if httpconnection != nil { httpconnection?. Cancel () httpconnection = nil } } //Start Download Request private func startrequestURL (url:nsstring) { if httpconnection != nil { httpconnection!. Cancel () httpConnection==nil } //Creating a Connection object var request=nsurlrequest (Url: nsurl (string: url)!) httpconnection=nsurlconnection (request: request, Delegate: self) } //method func connection in the rewrite protocol (connection: nsurlconnection, Didreceiveresponse response: nsurlresponse) { downloaddata.length=0 } func connection (Connection: nsurlconnection, didreceivedata data: nsdata ) { downloaddata.appenddata (data) } func connectiondidfinishloading (connection: nsurlconnection) { isDownloadSuccess = true delegate!. Zyhhttprequestsuccsee (self) Zyhhttprequestmanager.sharedhttprequestmanager (). Removetaskfromurl (self.requesturl!) } func connection (connection: nsurlconnection, Didfailwitherror error: nserror) { println ("Load failed ") println (Error) self.isdownloadsuccess=false &nbSp; zyhhttprequestmanager.sharedhttprequestmanager (). Removetaskfromurl (self.requestUrl!) } }protocol zyhhttprequestdelegate{ func zyhhttprequestsuccsee ( Request:zyhhttprequset)}
Project section:
GitHub Source Address: Https://github.com/ZYHshao/SwiftMovie
One of the mistakes, welcome advice, Hope in the exchange, continuous progress!
Swift learns the second practice--swift project time Movie