检索数组函数不工作 [英] Retrieve array function not working

查看:119
本文介绍了检索数组函数不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个函数调用数组保存和检索数组,他们的工作是保存和检索从手​​机的数组。我的问题是,他们没有工作。这些是两种功能。

  FUNC SaveArray(数组:[欠条] FILEID:字符串){
NSKeyedArchiver.archiveRootObject(数组,TOFILE:FILEID)
}
FUNC RetrieveArray(FILEID:字符串,VAR数组:[欠条]) - GT; [IOU] {
    如果让arraytoRetrieve = NSKeyedUnarchiver.unarchiveObjectWithFile(FILEID)作为? [IOU] {
        阵列= arraytoRetrieve
    }
    返回的数组
}

借条是我这样定义一个类:

 类借条:NSObject的,NSCoding {VAR金额:双
VAR付款人:字符串
Var描述:字符串的init(金额:双,付款人:字符串描述:字符串){
    self.Amount =量
    self.Payer =付款人
    self.Description =说明    super.init()}需要方便的init(codeR德codeR:NS codeR){
    让金额=去coder.de codeDoubleForKey(额)
    让纳税人去= coder.de codeObjectForKey(付款人)作为!串
    让我们描述=去coder.de codeObjectForKey(说明)为!串
    self.init(金额:金额,付款人:付款人,说明:说明)
}
FUNC EN codeWith codeR(A codeR:NS codeR){
    一个coder.en codeDouble(self.Amount,forKey:金额)
    一个coder.en codeObject的(self.Payer,forKey:付款人)
    一个coder.en codeObject的(self.Description,forKey:说明)
}
}

和我实现我的viewWillAppear中的方法是这样检索功能:

  =债务RetrieveArray(债务,数组:债务)

但由于某些未知的原因,当我运行类似下面的操场测试,它工作得很好:

  VAR IOUArray:[欠条] = [IOU()
VAR例如=借条(金额:70,付款人:杰克逊介绍:因为)
IOUArray.append(实施例)
FUNC SaveArray(数组:[欠条] FILEID:字符串){
    NSKeyedArchiver.archiveRootObject(数组,TOFILE:FILEID)
}
FUNC RetrieveArray(FILEID:字符串) - GT; [IOU] {
    让IOUA = NSKeyedUnarchiver.unarchiveObjectWithFile(FILEID)作为! [IOU]
    返回IOUA
}SaveArray(IOUArray,FILEID:IOUArray)
RetrieveArray(IOUArray)
打印(RetrieveArray(IOUArray)[0] .Amount)


解决方案

在方法参数 TOFILE 的值 archiveRootObject unarchiveObjectWithFile 必须是一个有效的文件路径。

archiveRootObject 收益真正如果操作成功,否则

I created two functions called save array and retrieve array and their job is to save and retrieve an array from the phone. My problem is that they are not working. These are the two functions.

func SaveArray (array: [IOU],fileID: String){
NSKeyedArchiver.archiveRootObject(array, toFile: fileID)
}
func RetrieveArray (fileID: String, var array: [IOU]) -> [IOU]{
    if let arraytoRetrieve = NSKeyedUnarchiver.unarchiveObjectWithFile(fileID) as? [IOU]{
        array = arraytoRetrieve
    }
    return array
}

IOU is a class I have defined like this:

class IOU : NSObject, NSCoding {

var Amount : Double
var Payer : String
var Description : String



init (amount: Double, payer: String, description: String){
    self.Amount = amount
    self.Payer = payer
    self.Description = description

    super.init()

}

required convenience init(coder decoder: NSCoder){
    let amount = decoder.decodeDoubleForKey("amount")
    let payer = decoder.decodeObjectForKey("payer") as! String
    let description = decoder.decodeObjectForKey("description") as! String
    self.init(amount: amount,payer: payer,description: description)
}
func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeDouble(self.Amount, forKey: "amount")
    aCoder.encodeObject(self.Payer, forKey: "payer")
    aCoder.encodeObject(self.Description, forKey: "description")
}


}

And I implemented my retrieve function in the viewWillAppear method like this:

Debts = RetrieveArray("Debts", array: Debts)

But for some unknown reason, when I run a playground test like the one below, it works just fine:

var IOUArray : [IOU] = [IOU]()


var example = IOU(amount: 70, payer: "Jackson", description: "Because")
IOUArray.append(example)


func SaveArray (array: [IOU],fileID: String){
    NSKeyedArchiver.archiveRootObject(array, toFile: fileID)
}
func RetrieveArray (fileID: String) -> [IOU]{
    let IOUA = NSKeyedUnarchiver.unarchiveObjectWithFile(fileID) as! [IOU]
    return IOUA
}

SaveArray(IOUArray, fileID: "IOUArray")
RetrieveArray("IOUArray")
print(RetrieveArray("IOUArray")[0].Amount)

解决方案

The value of the parameter toFile in the method archiveRootObject and the parameter in unarchiveObjectWithFile must be a valid file path..

archiveRootObject returns true if the operation was successful, otherwise false

这篇关于检索数组函数不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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