如何使用NSKeyedUnarchiver的代理? [英] How to use the delegates with NSKeyedUnarchiver?

查看:177
本文介绍了如何使用NSKeyedUnarchiver的代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSKeyedUnarchiver取消存档一个对象,并希望使用代理(NSKeyedUnarchiverDelegate),但是我的委托不被调用。存档和取消归档正常工作,但代理(unarchiver& unarchiverDidFinish)不被调用。有人可以帮助吗?

I am using NSKeyedUnarchiver to unarchive an object and would like to use the delegates (NSKeyedUnarchiverDelegate), but my delegates are not called. Archiving and Unarchiving is working fine, but the Delegates (unarchiver & unarchiverDidFinish) are not called. Can someone help?

我有以下实现:

        class BlobHandler: NSObject , NSKeyedUnarchiverDelegate{

           func load() -> MYOBJECTCLASS{          
              let data:NSData? = getBlob();      
              var mykeyedunarchiver:NSKeyedUnarchiver=NSKeyedUnarchiver(forReadingWithData: data!);
              mykeyedunarchiver.delegate = self;
              let temp=mykeyedunarchiver.decodeObjectForKey("rootobject")
// No delegates are called
                            if temp==nil {
                                blobsexists=false;
                            }else{
                                objectreturn = temp! as! MYOBJECTCLASS;
                                return objectreturn;
                            }
        }

    func save1(myobject:MYOBJECTCLASS){
            let data = NSMutableData()
            var keyedarchiver:NSKeyedArchiver=NSKeyedArchiver(forWritingWithMutableData: data);
            keyedarchiver.encodeObject(maptheme, forKey: "rootobject");

            let bytes = data.bytes;
            let len=data.length; 
            saveblob(bytes);
    }

以下代理,也在我的Blobhandler中实现,从不被调用:

The following delegates, which are also implemented in my Blobhandler, are never called:

func unarchiver(unarchiver: NSKeyedUnarchiver, cannotDecodeObjectOfClassName name: String, originalClasses classNames: [String]) -> AnyClass? {
    print("I am in unarchiver !");
    return nil;
}

func unarchiverDidFinish(_ unarchiver: NSKeyedUnarchiver){
    print("I am in unarchiverDidFinish ! ");
}


推荐答案

要使 unarchiverWillFinish: unarchiverDidFinish:被正确调用,我们必须调用 finishDecoding

To make either unarchiverWillFinish: and unarchiverDidFinish: be called properly, we have to invoke finishDecoding when finished decoding.


一旦配置了解码器对象,要对对象或数据项进行解码,请使用 decodeObjectForKey:方法。解码密码存档后,您应该在发布unarchiver之前调用 finishDecoding

Once you have the configured decoder object, to decode an object or data item, use the decodeObjectForKey: method. When finished decoding a keyed archive, you should invoke finishDecoding before releasing the unarchiver.

我们通知NSKeyedUnarchiver实例的代理,并通过调用此方法对归档执行任何最终操作。一旦这个方法被调用,根据苹果公司的官方文档,我们的unarchiver不能解码任何进一步的值。如果我们在调用 finishDecoding 之后继续执行任何解码操作,我们将收到以下消息:

We notify the delegate of the instance of NSKeyedUnarchiver and perform any final operations on the archive through invoking this method. And once this method is invoked, according to Apple's official documentation, our unarchiver cannot decode any further values. We would get following message if we continue to perform any decoding operation after invoked finishDecoding:


*** - [NSKeyedUnarchiver decodeObjectForKey:]:unarchive已经完成,无法解码更多

*** -[NSKeyedUnarchiver decodeObjectForKey:]: unarchive already finished, cannot decode anything more

它也是有意义的>编码对应。

It also makes sense for encoding counterparts.

这篇关于如何使用NSKeyedUnarchiver的代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆