在Swift中使用NSCoding归档可选结构的数组? [英] archive array of optional structs with NSCoding in Swift?

查看:611
本文介绍了在Swift中使用NSCoding归档可选结构的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Obj-C中完成了很多NSCoding归档,但是我不确定它如何处理Swift中的结构,也不能处理带有可选值的数组.这是我的代码:

I've done a lot of NSCoding archiving in Obj-C, but I'm not sure how it handles structs in Swift, nor arrays with optional values. Here is my code:

public struct SquareCoords {
    var x: Int, y: Int
}

这是我需要存储的类:

public class Player: NSCoding {
    var playerNum: Int
    var name = ""
    private var moveHistory: [SquareCoords?] = []

    init (playerNum: Int, name: String) {
        self.playerNum = playerNum
        self.name = name
    }

    public required init(coder aDecoder: NSCoder!) {
        playerNum = aDecoder.decodeIntegerForKey("playerNumKey")
        name = aDecoder.decodeObjectForKey("nameKey") as String
        moveHistory = aDecoder.decodeObjectForKey("moveHistoryKey") as [SquareCoords?]
    }

    public func encodeWithCoder(aCoder: NSCoder!) {
        aCoder.encodeInteger(playerNum, forKey: "playerNumKey")
        aCoder.encodeObject(name, forKey: "nameKey")
        aCoder.encodeObject(moveHistory, forKey: "moveHistoryKey")
    }
...

在编码器初始化的最后一行,我在XCode中收到以下错误消息:

On the last line of the coder init, I get the following error message in XCode:

'AnyObject' is not convertible to [SquareCoords?]'

,然后在encodeWithEncoder的最后一行:

and on the last line of encodeWithEncoder:

Extra argument 'forKey' in call

有人可以让我朝着正确的方向前进吗?

Can anyone get me moving in the right direction?

推荐答案

我不确定确切的问题是什么,但是如果使用NSMutableArray而不是Swift数组,则问题可以解决:

I'm not sure what the exact problem is, but if you use NSMutableArray rather than a Swift array the problem resolves:

public struct SquareCoords {
    var x: Int, y: Int
}



public class Player: NSCoding {
    var playerNum: Int
    var name = ""
    var moveHistory: NSMutableArray = NSMutableArray()

    init (playerNum: Int, name: String) {
        self.playerNum = playerNum
        self.name = name
    }

    public required init(coder aDecoder: NSCoder!) {
        playerNum = aDecoder.decodeIntegerForKey("playerNumKey")
        name = aDecoder.decodeObjectForKey("nameKey") as String
        moveHistory = aDecoder.decodeObjectForKey("moveHistoryKey") as NSMutableArray
    }

    public func encodeWithCoder(aCoder: NSCoder!) {
        aCoder.encodeInteger(playerNum, forKey: "playerNumKey")
        aCoder.encodeObject(name, forKey: "nameKey")
        aCoder.encodeObject(moveHistory, forKey: "moveHistoryKey")
    }
}

当aDecoder.decodeObjectForKey返回一个隐式展开的AnyObject时,似乎不会转换为SquareCoords数组.

It seems to be the case that when aDecoder.decodeObjectForKey returns an implicitly unwrapped AnyObject this won't cast to a SquareCoords array.

开始更深入地研究这一点,我注意到它可能与使用结构有关. (您正在创建值类型的结构数组.)这有点猜测,但是我注意到,如果SquareType使用了类类型,就不会有问题,例如

Been playing with this a little further and I noticed it may have something to do with the use of a struct. (you're creating an array of structs which are value types.) This is a bit of a guess but I noticed if a class type is used for SquareCoords there is no issue, e.g.

public class SquareCoords {
    var x: Int = 0, y: Int = 0
}



public class Player: NSCoding {
    var playerNum: Int
    var name = ""
    private var moveHistory: [SquareCoords] = [SquareCoords]()

init (playerNum: Int, name: String) {
    self.playerNum = playerNum
    self.name = name
}

public required init(coder aDecoder: NSCoder!) {
    playerNum = aDecoder.decodeIntegerForKey("playerNumKey")
    name = aDecoder.decodeObjectForKey("nameKey") as String
    moveHistory = aDecoder.decodeObjectForKey("moveHistoryKey") as [SquareCoords]
}

public func encodeWithCoder(aCoder: NSCoder!) {
    aCoder.encodeInteger(playerNum, forKey: "playerNumKey")
    aCoder.encodeObject(name, forKey: "nameKey")
    aCoder.encodeObject(moveHistory, forKey: "moveHistoryKey")
    }
}

也许由于某种原因,从AnyObject强制转换为struct数组. -我相信其他人可以提供更多见解,希望这能有所帮助!斯威夫特可能是狂暴的:D

Maybe the cast from AnyObject fails to a struct array for some reason. - I'm sure someone else can provide more insight, hope this helps somewhat! Swift can be tempestuous :D

这篇关于在Swift中使用NSCoding归档可选结构的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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