数组 – 在Swift中使用NSCoding的可选结构的归档数组?

数组 – 在Swift中使用NSCoding的可选结构的归档数组?,第1张

概述我在Obj-C中做了很多NSCoding归档,但是我不知道如何处理Swift中的结构体,也不知道如何处理Swift中的结构体,也不知道如何处理Swift中的结构体。这是我的代码: public struct SquareCoords { var x: Int, y: Int} 这里是我需要存储的课程: public class Player: NSCoding { var pla 我在Obj-C中做了很多NSCoding归档,但是我不知道如何处理Swift中的结构体,也不知道如何处理Swift中的结构体,也不知道如何处理Swift中的结构体。这是我的代码:
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")    }...

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

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

并在编码的最后一行:

Extra argument 'forKey' in call

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

我不知道确切的问题是什么,但如果您使用NSMutableArray而不是Swift数组,问题解决:
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: "moveHistoryKey")    }}

似乎是这样的情况,当aDecoder.decodeObjectForKey返回一个隐式解开的AnyObject时,它不会转换为一个SquareCoords数组。

玩了一点,我注意到它可能与使用结构有关。 (你正在创建一个值类型的结构体数组。)这是一个猜测,但是我注意到,如果一个类类型用于SquareCoords,那就没有问题,例如

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

总结

以上是内存溢出为你收集整理的数组 – 在Swift中使用NSCoding的可选结构的归档数组?全部内容,希望文章能够帮你解决数组 – 在Swift中使用NSCoding的可选结构的归档数组?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1053553.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存