瀏覽代碼

Merge pull request #78 from JJSaccolo/master

Xcode 6 beta 7 fixes (source + iOS Example)
Mattt Thompson 11 年之前
父節點
當前提交
72206e5726
共有 4 個文件被更改,包括 37 次插入41 次删除
  1. 17 21
      Example/DetailViewController.swift
  2. 12 12
      Source/Alamofire.swift
  3. 7 7
      Tests/ParameterEncodingTests.swift
  4. 1 1
      Tests/RequestTests.swift

+ 17 - 21
Example/DetailViewController.swift

@@ -32,7 +32,7 @@ class DetailViewController: UITableViewController {
         willSet {
             if self.request != nil {
                 self.request?.cancel()
-                self.refreshControl.endRefreshing()
+                self.refreshControl?.endRefreshing()
                 self.headers.removeAll()
                 self.body = nil
                 self.elapsedTime = nil
@@ -50,7 +50,7 @@ class DetailViewController: UITableViewController {
 
     override func awakeFromNib() {
         super.awakeFromNib()
-        self.refreshControl.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
+        self.refreshControl?.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
 
     }
 
@@ -69,7 +69,7 @@ class DetailViewController: UITableViewController {
             return
         }
 
-        self.refreshControl.beginRefreshing()
+        self.refreshControl?.beginRefreshing()
 
         let start = CACurrentMediaTime()
         self.request?.responseString { (request, response, body, error) in
@@ -83,13 +83,13 @@ class DetailViewController: UITableViewController {
             self.body = body
 
             self.tableView.reloadData()
-            self.refreshControl.endRefreshing()
+            self.refreshControl?.endRefreshing()
         }
     }
 
     // MARK: UITableViewDataSource
 
-    override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
+    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         switch Sections.fromRaw(section)! {
         case .Headers:
             return self.headers.count
@@ -100,7 +100,7 @@ class DetailViewController: UITableViewController {
         }
     }
 
-    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
+    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
 
         switch Sections.fromRaw(indexPath.section)! {
         case .Headers:
@@ -108,39 +108,35 @@ class DetailViewController: UITableViewController {
             let field = self.headers.keys.array.sorted(<)[indexPath.row]
             let value = self.headers[field]
 
-            cell.textLabel.text = field
-            cell.detailTextLabel.text = value
+            cell.textLabel!.text = field
+            cell.detailTextLabel!.text = value
 
             return cell
         case .Body:
             let cell = self.tableView.dequeueReusableCellWithIdentifier("Body") as UITableViewCell
 
-            cell.textLabel.text = self.body
+            cell.textLabel!.text = self.body
 
             return cell
-        default:
-            return nil
         }
     }
 
     // MARK: UITableViewDelegate
 
-    override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
+    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
         return 2
     }
 
-    override func tableView(tableView: UITableView!, titleForHeaderInSection section: Int) -> String! {
+    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String {
         switch Sections.fromRaw(section)! {
         case .Headers:
-            return self.headers.isEmpty ? nil : "Headers"
+            return self.headers.isEmpty ? "" : "Headers"
         case .Body:
-            return self.body == nil ? nil : "Body"
-        default:
-            return nil
+            return self.body == nil ? "" : "Body"
         }
     }
 
-    override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
+    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
         switch Sections.fromRaw(indexPath.section)! {
         case .Body:
             return 300
@@ -149,12 +145,12 @@ class DetailViewController: UITableViewController {
         }
     }
 
-    override func tableView(tableView: UITableView!, titleForFooterInSection section: Int) -> String! {
+    override func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String {
         switch Sections.fromRaw(section)! {
         case .Body:
-            return self.elapsedTime == nil ? nil : "Elapsed Time: \(self.elapsedTime!) sec"
+            return self.elapsedTime == nil ? "" : "Elapsed Time: \(self.elapsedTime!) sec"
         default:
-            return nil
+            return ""
         }
     }
 }

+ 12 - 12
Source/Alamofire.swift

@@ -70,8 +70,8 @@ public enum ParameterEncoding {
                 }
             }
 
-            if encodesParametersInURL(Method.fromRaw(request.HTTPMethod)!) {
-                let URLComponents = NSURLComponents(URL: mutableRequest.URL, resolvingAgainstBaseURL: false)
+            if encodesParametersInURL(Method.fromRaw(request.HTTPMethod!)!) {
+                let URLComponents = NSURLComponents(URL: mutableRequest.URL!, resolvingAgainstBaseURL: false)
                 URLComponents.query = (URLComponents.query != nil ? URLComponents.query! + "&" : "") + query(parameters!)
                 mutableRequest.URL = URLComponents.URL
             } else {
@@ -335,7 +335,7 @@ public class Manager {
 
         // MARK: NSURLSessionDownloadDelegate
 
-        func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didFinishDownloadingToURL location: NSURL!) {
+        func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
             if let delegate = self[downloadTask] as? Request.DownloadTaskDelegate {
                 delegate.URLSession(session, downloadTask: downloadTask, didFinishDownloadingToURL: location)
             }
@@ -343,7 +343,7 @@ public class Manager {
             self.downloadTaskDidFinishDownloadingToURL?(session, downloadTask, location)
         }
 
-        func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
+        func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
             if let delegate = self[downloadTask] as? Request.DownloadTaskDelegate {
                 delegate.URLSession(session, downloadTask: downloadTask, didWriteData: bytesWritten, totalBytesWritten: totalBytesWritten, totalBytesExpectedToWrite: totalBytesExpectedToWrite)
             }
@@ -351,7 +351,7 @@ public class Manager {
             self.downloadTaskDidWriteData?(session, downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
         }
 
-        func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
+        func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
             if let delegate = self[downloadTask] as? Request.DownloadTaskDelegate {
                 delegate.URLSession(session, downloadTask: downloadTask, didResumeAtOffset: fileOffset, expectedTotalBytes: expectedTotalBytes)
             }
@@ -412,13 +412,13 @@ public class Request {
 
     public func authenticate(HTTPBasic user: String, password: String) -> Self {
         let credential = NSURLCredential(user: user, password: password, persistence: .ForSession)
-        let protectionSpace = NSURLProtectionSpace(host: self.request.URL.host, port: 0, `protocol`: self.request.URL.scheme, realm: nil, authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
+        let protectionSpace = NSURLProtectionSpace(host: self.request.URL.host!, port: 0, `protocol`: self.request.URL.scheme, realm: nil, authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
 
         return authenticate(usingCredential: credential, forProtectionSpace: protectionSpace)
     }
 
     public func authenticate(usingCredential credential: NSURLCredential, forProtectionSpace protectionSpace: NSURLProtectionSpace) -> Self {
-        self.session.configuration.URLCredentialStorage.setCredential(credential, forProtectionSpace: protectionSpace)
+        self.session.configuration.URLCredentialStorage?.setCredential(credential, forProtectionSpace: protectionSpace)
 
         return self
     }
@@ -727,7 +727,7 @@ extension Request {
 
         return { (temporaryURL, response) -> (NSURL) in
             if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {
-                return directoryURL.URLByAppendingPathComponent(response.suggestedFilename)
+                return directoryURL.URLByAppendingPathComponent(response.suggestedFilename!)
             }
 
             return temporaryURL
@@ -747,7 +747,7 @@ extension Request {
 
         // MARK: NSURLSessionDownloadDelegate
 
-        func URLSession(session: NSURLSession!, downloadTask: NSURLSessionDownloadTask!, didFinishDownloadingToURL location: NSURL!) {
+        func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
             if self.downloadTaskDidFinishDownloadingToURL != nil {
                 let destination = self.downloadTaskDidFinishDownloadingToURL!(session, downloadTask, location)
                 var fileManagerError: NSError?
@@ -792,14 +792,14 @@ extension Request: DebugPrintable {
     func cURLRepresentation() -> String {
         var components: [String] = ["$ curl -i"]
 
-        let URL = self.request.URL!
+        let URL = self.request.URL
 
         if self.request.HTTPMethod != "GET" {
             components.append("-X \(self.request.HTTPMethod)")
         }
 
         if let credentialStorage = self.session.configuration.URLCredentialStorage {
-            let protectionSpace = NSURLProtectionSpace(host: URL.host, port: URL.port ?? 0, `protocol`: URL.scheme, realm: URL.host, authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
+            let protectionSpace = NSURLProtectionSpace(host: URL.host!, port: URL.port ?? 0, `protocol`: URL.scheme, realm: URL.host, authenticationMethod: NSURLAuthenticationMethodHTTPBasic)
             if let credentials = credentialStorage.credentialsForProtectionSpace(protectionSpace)?.values.array {
                 if !credentials.isEmpty {
                     if let credential = credentials[0] as? NSURLCredential {
@@ -818,7 +818,7 @@ extension Request: DebugPrintable {
             }
         }
 
-        for (field, value) in self.request.allHTTPHeaderFields {
+        for (field, value) in self.request.allHTTPHeaderFields! {
             switch field {
             case "Cookie":
                 continue

+ 7 - 7
Tests/ParameterEncodingTests.swift

@@ -52,7 +52,7 @@ class AlamofireURLParameterEncodingTestCase: XCTestCase {
 
     func testURLParameterEncodeOneStringKeyStringValueParameterAppendedToQuery() {
         var mutableRequest = self.request.mutableCopy() as NSMutableURLRequest
-        let URLComponents = NSURLComponents(URL: mutableRequest.URL, resolvingAgainstBaseURL: false)
+        let URLComponents = NSURLComponents(URL: mutableRequest.URL!, resolvingAgainstBaseURL: false)
         URLComponents.query = "baz=qux"
         mutableRequest.URL = URLComponents.URL
 
@@ -138,7 +138,7 @@ class AlamofireURLParameterEncodingTestCase: XCTestCase {
     }
 
     func testURLParameterEncodeGETParametersInURL() {
-        var mutableRequest = self.request.mutableCopy() as NSMutableURLRequest!
+        var mutableRequest = self.request.mutableCopy() as NSMutableURLRequest
         mutableRequest.HTTPMethod = Method.GET.toRaw()
 
         let parameters = ["foo": 1, "bar": 2]
@@ -150,13 +150,13 @@ class AlamofireURLParameterEncodingTestCase: XCTestCase {
     }
 
     func testURLParameterEncodePOSTParametersInHTTPBody() {
-        var mutableRequest = self.request.mutableCopy() as NSMutableURLRequest!
+        var mutableRequest = self.request.mutableCopy() as NSMutableURLRequest
         mutableRequest.HTTPMethod = Method.POST.toRaw()
 
         let parameters = ["foo": 1, "bar": 2]
         let (request, error) = self.encoding.encode(mutableRequest, parameters: parameters)
 
-        XCTAssertEqual(NSString(data: request.HTTPBody, encoding: NSUTF8StringEncoding), "bar=2&foo=1", "HTTPBody is incorrect")
+        XCTAssertEqual(NSString(data: request.HTTPBody!, encoding: NSUTF8StringEncoding), "bar=2&foo=1", "HTTPBody is incorrect")
         XCTAssertEqual(request.valueForHTTPHeaderField("Content-Type")!, "application/x-www-form-urlencoded", "Content-Type should be application/x-www-form-urlencoded")
         XCTAssertNotNil(request.HTTPBody, "HTTPBody should not be nil")
     }
@@ -202,7 +202,7 @@ class AlamofireJSONParameterEncodingTestCase: XCTestCase {
         XCTAssert(request.valueForHTTPHeaderField("Content-Type")!.hasPrefix("application/json"), "Content-Type should be application/json")
         XCTAssertNotNil(request.HTTPBody, "HTTPBody should not be nil")
 
-        let JSON = NSJSONSerialization.JSONObjectWithData(request.HTTPBody, options: .AllowFragments, error: nil) as NSObject!
+        let JSON = NSJSONSerialization.JSONObjectWithData(request.HTTPBody!, options: .AllowFragments, error: nil) as NSObject!
         XCTAssertNotNil(JSON, "HTTPBody JSON is invalid")
         XCTAssertEqual(JSON as NSObject, parameters as NSObject, "HTTPBody JSON does not equal parameters")
     }
@@ -248,7 +248,7 @@ class AlamofirePropertyListParameterEncodingTestCase: XCTestCase {
         XCTAssert(request.valueForHTTPHeaderField("Content-Type")!.hasPrefix("application/x-plist"), "Content-Type should be application/x-plist")
         XCTAssertNotNil(request.HTTPBody, "HTTPBody should not be nil")
 
-        let plist = NSPropertyListSerialization.propertyListWithData(request.HTTPBody, options: 0, format: nil, error: nil) as NSObject
+        let plist = NSPropertyListSerialization.propertyListWithData(request.HTTPBody!, options: 0, format: nil, error: nil) as NSObject
         XCTAssertNotNil(plist, "HTTPBody JSON is invalid")
         XCTAssertEqual(plist as NSObject, parameters as NSObject, "HTTPBody plist does not equal parameters")
     }
@@ -270,7 +270,7 @@ class AlamofirePropertyListParameterEncodingTestCase: XCTestCase {
         XCTAssert(request.valueForHTTPHeaderField("Content-Type")!.hasPrefix("application/x-plist"), "Content-Type should be application/x-plist")
         XCTAssertNotNil(request.HTTPBody, "HTTPBody should not be nil")
 
-        let plist = NSPropertyListSerialization.propertyListWithData(request.HTTPBody, options: 0, format: nil, error: nil) as NSObject!
+        let plist = NSPropertyListSerialization.propertyListWithData(request.HTTPBody!, options: 0, format: nil, error: nil) as NSObject!
         XCTAssertNotNil(plist, "HTTPBody JSON is invalid")
         XCTAssert(plist.valueForKey("date") is NSDate, "date is not NSDate")
         XCTAssert(plist.valueForKey("data") is NSData, "data is not NSData")

+ 1 - 1
Tests/RequestTests.swift

@@ -30,7 +30,7 @@ class AlamofireRequestInitializationTestCase: XCTestCase {
         let request = Alamofire.request(.GET, URL)
 
         XCTAssertNotNil(request.request, "request should not be nil")
-        XCTAssertEqual(request.request.URL!, NSURL(string: URL), "request URL should be equal")
+        XCTAssertEqual(request.request.URL, NSURL(string: URL), "request URL should be equal")
         XCTAssertNil(request.response, "response should be nil")
     }