同事把我用 Swift 写的工具删掉了,然后用 OC 重写了一遍...

2015-09-29 13:12:28 +08:00
 DingSoung
4998 次点击
所在节点    Swift
13 条回复
gaitana
2015-09-29 13:24:58 +08:00
然后呢
DingSoung
2015-09-29 13:26:33 +08:00
@gaitana 搞不懂是他不习惯 Swift 还是嫌弃,不让就重复劳动了啊。囧
gaitana
2015-09-29 13:29:36 +08:00
@dingsoung 可能他不认同 swift ,也可能是他 OC 学的太精通了,不忍心看到被后来者 Swift 替换掉。其实我也怀疑 Swift 的执行效率,虽然苹果声称某些方面比 OC 还快,但商业公司的话不能听信,谷歌每次新版本也是每次比老版本快 N 倍。
DingSoung
2015-09-29 13:30:26 +08:00
@gaitana 贴代码 有点长


```C
//
// QiniuManager.swift
// summer
//
// Created by Alex D. on 15/8/12.
//

import Qiniu
class QiniuManager {

static let instance = QiniuManager()
var upManager:QNUploadManager?
private init() {
upManager = QNUploadManager()
}

//MARK: 上传文件
func upload(token:String, data:NSData, key:String, process:((key:String, percent:Float)->Void)?, complete:((info:QNResponseInfo, key:String, resp:NSDictionary?)->Void)?, cancel:(()->Bool)?) {
let opt = QNUploadOption(mime: "text/plain", progressHandler: { (key, percent) -> Void in
print(key)
process?(key: key, percent: percent)
}, params: ["x:foo" : "fooval"], checkCrc: true) { () -> Bool in
//取消
if let block = cancel?() {
return block
}else {
return false
}
}
upManager?.putData(data, key: key, token: token, complete: { (info, key, resp) -> Void in
complete?(info: info, key: key, resp: resp)
}, option: opt)
}

//MARK: 断点续传
func upload(folder:String, token:String, data:NSData, key:String, process:((key:String, percent:Float)->Void)?, complete:((info:QNResponseInfo, key:String, resp:NSDictionary?)->Void)?, cancel:(()->Bool)?) {
do {
//写标记
self.upManager = try QNUploadManager(recorder: QNFileRecorder(folder: folder))
} catch {
self.upManager = QNUploadManager()
print("启动断点续传失败\(error),自动切换到连续上传")
}
self.upload(token, data: data, key: key, process: process, complete: { (info, key, resp) -> Void in
self.upManager = QNUploadManager()
complete?(info: info, key: key, resp: resp)
}) { () -> Bool in
cancel?()
if cancel?() == true {
self.upManager = QNUploadManager()
return true
}
return false
}
}
}
```

```C
//
// QiniuManager.m
// GiftBox
//
// Created by __ on 15/9/29.
//

#define kGetQiniuTokenURL xxxxx
#import "QiniuManager.h"
@interface QiniuManager()
@property (nonatomic, strong) QNUploadManager * manager;
@property (nonatomic, copy) NSString * token;
@property (nonatomic, assign) NSTimeInterval lastTime;
@end
@implementation QiniuManager
+ (QiniuManager *)sharedInstance {
static QiniuManager * manager;
static dispatch_once_t pred;
dispatch_once(&pred, ^{
manager = [[QiniuManager alloc] init];
manager.manager = [[QNUploadManager alloc] init];
});
return manager;
}
- (void)uploadData:(NSData *)data
key:(NSString *)key
success:(void(^)(NSString * key))success
fail:(void (^)(NSError *))fail {
if (self.needRefreshToken) {
[self refreshTokenWithSuccess:^{
[self uploadData:data key:key success:success fail:fail];
} fail:^(NSError *error) {
if (fail) {
fail(error);
}
}];
} else {
[self.manager putData:data key:key token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
if (info.isOK && resp != nil) {
if (success) {
success(key);
}
} else {
if (fail) {
fail(info.error);
}
}
} option:nil];

}
}
- (void)refreshTokenWithSuccess:(void(^)())success fail:(void(^)(NSError * error))fail {
[NetworkManager addGetRequestWithURLString:[NSString stringWithFormat:kGetQiniuTokenURL, IS_DEBUG ? 4 : 2] tag:100 contentType:GBContentType success:^(NetworkManager *network, id responseObject) {
NSString * status = [responseObject objectForKey:@"status"];
if (status.integerValue == 0) {
NSString * qiniuToken = [responseObject objectForKey:@"token"];
self.token = qiniuToken;
self.lastTime = [NSDate date].timeIntervalSince1970;
if (success) {
success();
}
} else {
if (fail) {
fail([NSError errorWithDomain:[responseObject objectForKey:@"err_msg"] code:[[responseObject objectForKey:@"code"] integerValue] userInfo:nil]);
}
}
} fail:^(NetworkManager *network, NSError *error) {
if (fail) {
fail(error);
}
}];
}
//十分钟
- (BOOL)needRefreshToken {
NSTimeInterval nowTime = [NSDate date].timeIntervalSince1970;
return nowTime - self.lastTime > 60 * 10;
}
@end
```
DingSoung
2015-09-29 13:31:19 +08:00
V2EX 不是支持 MarkDown 么
itchina110
2015-09-29 14:13:00 +08:00
然后 2016 年 1 月 10 日 将在北京举办 中国首届 Swift 大会 : http://atswift.io
xfspace
2015-09-29 14:15:36 +08:00
@dingsoung 回复不支持 Markdown 语法。。。
XadillaX
2015-09-29 14:21:22 +08:00
@dingsoung 你可以贴 gist 或者 paste.ubuntu.com
ibcker
2015-09-29 14:24:42 +08:00
打一架
dorentus
2015-09-29 14:28:24 +08:00
下面的那个版本似乎多了对 refresh token 的检查和支持,估计是他想加这功能,然后不懂 Swift 吧……

然后你的版本里面,第 41 行附近, cancel?() 了两次,应该是不对的吧
DingSoung
2015-09-29 14:36:00 +08:00
@dorentus 发现了 谢谢
zapper
2015-09-29 15:12:16 +08:00
可是你为什么不直接去问他呢...
Felldeadbird
2015-09-29 16:54:09 +08:00
@zapper 我也觉得。他这样做肯定有原因,先了解好

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/224486

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX