Swift學習第二練——Swift項目時光電影

來源:互聯網
上載者:User

標籤:

Swift學習第二練——Swift項目時光電影

    很早以前的一個OC的練習項目,用swift重新寫了一遍,因為xcode版本的更新對swift的相容度也在不斷改變,此版本適用於xcode6.1。

    這個項目中,用swift將iOS官方SDK中的HTTP進行了封裝,使用了swift編寫的非同步載入網狀圖片的方法。練習了用swift操作介面布局,跳轉介面等的方法。

    下面是封裝的下載類的核心代碼:

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("該任務存在")            return oldRequest!        }        //建立下載任務        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        }    }        //開始下載請求    private func startRequestUrl(url:NSString){        if httpConnection != nil {            httpConnection!.cancel()            httpConnection==nil        }        //建立連線物件        var request=NSURLRequest(URL: NSURL(string: url)!)        httpConnection=NSURLConnection(request: request, delegate: self)            }    //重寫協議中的方法    func connection(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("載入失敗")        println(error)        self.isDownloadSuccess=false        ZYHHttpRequestManager.sharedHttpRequestManager().removeTaskFromUrl(self.requestUrl!)    }                }protocol ZYHHttpRequestDelegate{    func ZYHHttpRequestSuccsee(request:ZYHHttpRequset)}

項目部分:


github源碼地址:https://github.com/ZYHshao/SwiftMovie

其中錯誤之處,歡迎指教,希望在交流中,不斷進步!

Swift學習第二練——Swift項目時光電影

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.