十款不容错过的Swift iOS开源项目及介绍

前端之家收集整理的这篇文章主要介绍了十款不容错过的Swift iOS开源项目及介绍前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.十款不容错过的Swift iOS开源项目.

http://www.csdn.net/article/2014-10-16/2822083-swift-ios-open-source-projects

2.缓存框架 Haneke:

Haneke是一款使用Swift语言编写的,轻量级的iOS通用缓存。它为UIImage、NSData、JSON和String提供记忆和LRU磁盘缓存或其他像数据可以读取或写入的任何其他类型。特别地是,Haneke更擅长处理图像。使用要求:iOS 8.0+、Xcode 6.0。

https://github.com/Haneke/HanekeSwift

3.Alamofire网络库基础教程:

http://www.jianshu.com/p/f1208b5e42d9

http://www.jianshu.com/p/77a86824fa0f

github地址:https://github.com/Alamofire/Alamofire

使用:
- HTTP Methods

public enum Method: String {
    case OPTIONS,GET,HEAD,POST,PUT,PATCH,DELETE,TRACE,CONNECT
}

---------------------------

Alamofire.request(.POST,"https://httpbin.org/post")

Alamofire.request(.PUT,"https://httpbin.org/put")

Alamofire.request(.DELETE,"https://httpbin.org/delete")
  • GET Request With URL-Encoded Parameters
Alamofire.request(.GET,"https://httpbin.org/get",parameters: ["foo": "bar"])
// https://httpbin.org/get?foo=bar
  • POST Request With URL-Encoded Parameters
let parameters = [
    "foo": "bar","baz": ["a",1],"qux": [
        "x": 1,"y": 2,"z": 3
    ]
]

Alamofire.request(.POST,"https://httpbin.org/post",parameters: parameters)
// HTTP body: foo=bar&baz[]=a&baz[]=1&qux[x]=1&qux[y]=2&qux[z]=3
  • POST Request with JSON-encoded Parameters
let parameters = [
    "foo": [1,2,3],"bar": [
        "baz": "qux"
    ]
]

Alamofire.request(.POST,parameters: parameters,encoding: .JSON)
// HTTP body: {"foo": [1,"bar": {"baz": "qux"}}
  • HTTP Headers
let headers = [
    "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==","Accept": "application/json"
]

Alamofire.request(.GET,headers: headers)
         .responseJSON { response in
             debugPrint(response)
         }

上传upload:

  • Supported Upload Types
File
    Data
    Stream
    MultipartFormData
  • Uploading a File
let fileURL = NSBundle.mainBundle().URLForResource("Default",withExtension: "png")
Alamofire.upload(.POST,file: fileURL)
  • Uploading with Progress
Alamofire.upload(.POST,file: fileURL)
         .progress { bytesWritten,totalBytesWritten,totalBytesExpectedToWrite in
             print(totalBytesWritten)

             // This closure is NOT called on the main queue for performance
             // reasons. To update your ui,dispatch to the main queue.
             dispatch_async(dispatch_get_main_queue()) {
                 print("Total bytes written on main queue: \(totalBytesWritten)")
             }
         }
         .validate()
         .responseJSON { response in
             debugPrint(response)
         }
  • Uploading MultipartFormData
Alamofire.upload(
    .POST,multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(fileURL: unicornImageURL,name: "unicorn")
        multipartFormData.appendBodyPart(fileURL: rainbowImageURL,name: "rainbow")
    },encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload,_,_):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    }
)

下载Downloading

Supported Download Types

Request
Resume Data
  • Downloading a File
Alamofire.download(.GET,"https://httpbin.org/stream/100") { temporaryURL,response in
    let fileManager = NSFileManager.defaultManager()
    let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory,inDomains: .UserDomainMask)[0]
    let pathComponent = response.suggestedFilename

    return directoryURL.URLByAppendingPathComponent(pathComponent!)
}

使用默认的下载的目录:Using the Default Download Destination

let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory,domain: .UserDomainMask)
Alamofire.download(.GET,"https://httpbin.org/stream/100",destination: destination)
  • Downloading a File w/Progress
Alamofire.download(.GET,destination: destination)
         .progress { bytesRead,totalBytesRead,totalBytesExpectedToRead in
             print(totalBytesRead)

             // This closure is NOT called on the main queue for performance
             // reasons. To update your ui,dispatch to the main queue.
             dispatch_async(dispatch_get_main_queue()) {
                 print("Total bytes read on main queue: \(totalBytesRead)")
             }
         }
         .response { _,error in
             if let error = error {
                 print("Failed with error: \(error)")
             } else {
                 print("Downloaded file successfully")
             }
         }
  • Accessing Resume Data for Failed Downloads访问下载失败的恢复数据
Alamofire.download(.GET,destination: destination)
         .response { _,data,_ in
             if let
                 data = data,
                 resumeDataString = NSString(data: data,encoding: NSUTF8StringEncoding)
             {
                 print("Resume Data: \(resumeDataString)")
             } else {
                 print("Resume Data was empty")
             }
         }
The data parameter is automatically populated with the resumeData if available.数据参数自动填充resumeData如果可用。
let download = Alamofire.download(.GET,destination: destination)
download.response { _,_ in
    if let
        resumeData = download.resumeData,resumeDataString = NSString(data: resumeData,encoding: NSUTF8StringEncoding)
    {
        print("Resume Data: \(resumeDataString)")
    } else {
        print("Resume Data was empty")
    }
}
原文链接:https://www.f2er.com/swift/323454.html

猜你在找的Swift相关文章