V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
DingSoung
V2EX  ›  Swift

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

  •  
  •   DingSoung ·
    DingSoung · 2015-09-29 13:12:28 +08:00 · 4456 次点击
    这是一个创建于 2256 天前的主题,其中的信息可能已经有所发展或是发生改变。
    13 条回复    2015-09-29 16:54:09 +08:00
    gaitana
        1
    gaitana  
       2015-09-29 13:24:58 +08:00
    然后呢
    DingSoung
        2
    DingSoung  
    OP
       2015-09-29 13:26:33 +08:00
    @gaitana 搞不懂是他不习惯 Swift 还是嫌弃,不让就重复劳动了啊。囧
    gaitana
        3
    gaitana  
       2015-09-29 13:29:36 +08:00
    @dingsoung 可能他不认同 swift ,也可能是他 OC 学的太精通了,不忍心看到被后来者 Swift 替换掉。其实我也怀疑 Swift 的执行效率,虽然苹果声称某些方面比 OC 还快,但商业公司的话不能听信,谷歌每次新版本也是每次比老版本快 N 倍。
    DingSoung
        4
    DingSoung  
    OP
       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
        5
    DingSoung  
    OP
       2015-09-29 13:31:19 +08:00
    V2EX 不是支持 MarkDown 么
    itchina110
        6
    itchina110  
       2015-09-29 14:13:00 +08:00
    然后 2016 年 1 月 10 日 将在北京举办 中国首届 Swift 大会 : http://atswift.io
    xfspace
        7
    xfspace  
       2015-09-29 14:15:36 +08:00 via Android
    @dingsoung 回复不支持 Markdown 语法。。。
    XadillaX
        8
    XadillaX  
       2015-09-29 14:21:22 +08:00
    @dingsoung 你可以贴 gist 或者 paste.ubuntu.com
    ibcker
        9
    ibcker  
       2015-09-29 14:24:42 +08:00
    打一架
    dorentus
        10
    dorentus  
       2015-09-29 14:28:24 +08:00
    下面的那个版本似乎多了对 refresh token 的检查和支持,估计是他想加这功能,然后不懂 Swift 吧……

    然后你的版本里面,第 41 行附近, cancel?() 了两次,应该是不对的吧
    DingSoung
        11
    DingSoung  
    OP
       2015-09-29 14:36:00 +08:00
    @dorentus 发现了 谢谢
    zapper
        12
    zapper  
       2015-09-29 15:12:16 +08:00   ❤️ 2
    可是你为什么不直接去问他呢...
    Felldeadbird
        13
    Felldeadbird  
       2015-09-29 16:54:09 +08:00
    @zapper 我也觉得。他这样做肯定有原因,先了解好
    关于   ·   帮助文档   ·   API   ·   FAQ   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   3843 人在线   最高记录 5497   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 07:49 · PVG 15:49 · LAX 23:49 · JFK 02:49
    ♥ Do have faith in what you're doing.