Skip to content

Commit 3f6a70b

Browse files
author
nghialv
committed
update to Swift1.2
1 parent bda7e2f commit 3f6a70b

7 files changed

+36
-42
lines changed

Net/GCD.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99

1010
typealias GCDClosure = () -> ()
11-
typealias GCDApplyClosure = (UInt) -> ()
11+
typealias GCDApplyClosure = (Int) -> ()
1212
typealias GCDOnce = dispatch_once_t
1313

1414
enum QueueType {
@@ -223,7 +223,7 @@ class gcd
223223
*
224224
*/
225225
class func apply(queueType: QueueType, interators: UInt, closure: GCDApplyClosure) {
226-
dispatch_apply(interators, queueType.getQueue(), closure)
226+
dispatch_apply(Int(interators), queueType.getQueue(), closure)
227227
}
228228

229229
/**

Net/Net.swift

+4-8
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,19 @@ class Net : NSObject, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NS
216216
// MARK: NSURLSessionTaskDelegate
217217
func URLSession(session: NSURLSession!, task: NSURLSessionTask!, didCompleteWithError error: NSError!) {
218218
if (error != nil) {
219-
if task is NSURLSessionDownloadTask {
220-
let downloadTask = task as NSURLSessionDownloadTask
219+
if let downloadTask = task as? NSURLSessionDownloadTask {
221220
let downloader = downloaders[downloadTask]
222221
downloader?.didComplete(nil, error: error)
223222
downloaders.removeValueForKey(downloadTask)
224223
}
225-
else if task is NSURLSessionUploadTask {
226-
let uploadTask = task as NSURLSessionUploadTask
224+
else if let uploadTask = task as? NSURLSessionUploadTask {
227225
let uploader = uploaders[uploadTask]
228226
uploader?.didComplete(error)
229227
uploaders.removeValueForKey(uploadTask)
230228
}
231229
}
232230
else {
233-
if task is NSURLSessionUploadTask {
234-
let uploadTask = task as NSURLSessionUploadTask
231+
if let uploadTask = task as? NSURLSessionUploadTask {
235232
let uploader = uploaders[uploadTask]
236233
uploader?.didComplete(nil)
237234
uploaders.removeValueForKey(uploadTask)
@@ -241,10 +238,9 @@ class Net : NSObject, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NS
241238

242239
func URLSession(session: NSURLSession!, task: NSURLSessionTask!, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
243240
// upload progress
244-
if task is NSURLSessionUploadTask {
241+
if let uploadTask = task as? NSURLSessionUploadTask {
245242
let progress = Double(totalBytesSent) / Double(totalBytesExpectedToSend)
246243

247-
let uploadTask = task as NSURLSessionUploadTask
248244
let uploader = uploaders[uploadTask]
249245
uploader?.updateProgress(Float(progress))
250246
}

Net/NetHelper.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ class NetHelper
9090
var valueType: String?
9191
var filenameClause = ""
9292

93-
if value is NetData {
94-
let netData = value as NetData
93+
if let netData = value as? NetData {
9594
valueData = netData.data
9695
valueType = netData.mimeType.getString()
9796
filenameClause = " filename=\"\(netData.filename)\""

Net/NetResponseData.swift

+25-25
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ class ResponseData
2727
* @return json dictionary
2828
*/
2929
func json(error: NSErrorPointer = nil) -> NSDictionary? {
30-
let httpResponse = urlResponse as NSHTTPURLResponse
31-
if httpResponse.statusCode == 200 {
32-
let jsonData = NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments, error: error) as NSDictionary
33-
return jsonData
30+
if let httpResponse = urlResponse as? NSHTTPURLResponse {
31+
if httpResponse.statusCode == 200 {
32+
let jsonData = NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments, error: error) as! NSDictionary
33+
return jsonData
34+
}
35+
else if error != nil {
36+
error.memory = NSError(domain: "HTTP_ERROR_CODE", code: httpResponse.statusCode, userInfo: nil)
37+
}
3438
}
35-
else if error != nil {
36-
error.memory = NSError(domain: "HTTP_ERROR_CODE", code: httpResponse.statusCode, userInfo: nil)
37-
}
38-
3939
return nil
4040
}
4141

@@ -45,14 +45,14 @@ class ResponseData
4545
* @return UIImage
4646
*/
4747
func image(error: NSErrorPointer = nil) -> UIImage? {
48-
let httpResponse = urlResponse as NSHTTPURLResponse
49-
if httpResponse.statusCode == 200 && data.length > 0 {
50-
return UIImage(data: data)
51-
}
52-
else if error != nil {
53-
error.memory = NSError(domain: "HTTP_ERROR_CODE", code: httpResponse.statusCode, userInfo: nil)
48+
if let httpResponse = urlResponse as? NSHTTPURLResponse {
49+
if httpResponse.statusCode == 200 && data.length > 0 {
50+
return UIImage(data: data)
51+
}
52+
else if error != nil {
53+
error.memory = NSError(domain: "HTTP_ERROR_CODE", code: httpResponse.statusCode, userInfo: nil)
54+
}
5455
}
55-
5656
return nil
5757
}
5858

@@ -64,17 +64,17 @@ class ResponseData
6464
* @return
6565
*/
6666
func parseXml(delegate: NSXMLParserDelegate, error: NSErrorPointer = nil) -> Bool {
67-
let httpResponse = urlResponse as NSHTTPURLResponse
68-
if httpResponse.statusCode == 200 {
69-
let xmlParser = NSXMLParser(data: data)
70-
xmlParser.delegate = delegate
71-
xmlParser.parse()
72-
return true
73-
}
74-
else if error != nil {
75-
error.memory = NSError(domain: "HTTP_ERROR_CODE", code: httpResponse.statusCode, userInfo: nil)
67+
if let httpResponse = urlResponse as? NSHTTPURLResponse {
68+
if httpResponse.statusCode == 200 {
69+
let xmlParser = NSXMLParser(data: data)
70+
xmlParser.delegate = delegate
71+
xmlParser.parse()
72+
return true
73+
}
74+
else if error != nil {
75+
error.memory = NSError(domain: "HTTP_ERROR_CODE", code: httpResponse.statusCode, userInfo: nil)
76+
}
7677
}
77-
7878
return false
7979
}
8080
}

ios_example/example/AppDelegate.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1414
var window: UIWindow?
1515
var backgroundTransferCompletionHandler: (() -> Void)?
1616

17-
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!)
18-
-> Bool {
17+
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
1918
return true
2019
}
2120

ios_example/example/DownloadViewController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class DownloadViewController : UIViewController
3030
net = Net()
3131
net.setupSession(backgroundIdentifier: "com.nghialv.download")
3232

33-
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
33+
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
3434
net.eventsForBackgroundHandler = { [weak appDelegate](urlSession: NSURLSession) in
3535
// this will be call for every backgroud event
3636
urlSession.getDownloadingTasksCount{ downloadingTaskCount in
@@ -128,7 +128,7 @@ class DownloadViewController : UIViewController
128128
let filename = url.lastPathComponent
129129

130130
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
131-
let documentDir = urls[0] as NSURL
131+
let documentDir = urls[0] as! NSURL
132132
let desUrl = documentDir.URLByAppendingPathComponent(filename)
133133

134134
if fileManager.fileExistsAtPath(desUrl.path!) {

ios_example/example/UploadViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class UploadViewController : UIViewController
3131
net = Net()
3232
net.setupSession(backgroundIdentifier: "com.nghiav.upload")
3333

34-
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
34+
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
3535
net.eventsForBackgroundHandler = { [weak appDelegate](urlSession: NSURLSession) in
3636
// this will be call for every backgroud event
3737
urlSession.getUploadingTaskCount{ uploadingTaskCount in

0 commit comments

Comments
 (0)