Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios /p>_Ios_Swift_Nsuserdefaults - Fatal编程技术网

Ios /p>

Ios /p>,ios,swift,nsuserdefaults,Ios,Swift,Nsuserdefaults,每次查看下一个有趣的事实时,只写入用户默认值的操作并不昂贵。因此,为了简单起见,每次他们查看新的有趣事实时,您可以将新索引写入NSUserDefaults @IBAction func showFunFact() { if (UIApplication.sharedApplication().applicationIconBadgeNumber != 0){ UIApplication.sharedApplication().applicationIconBadgeNum

每次查看下一个有趣的事实时,只写入用户默认值的操作并不昂贵。因此,为了简单起见,每次他们查看新的有趣事实时,您可以将新索引写入
NSUserDefaults

@IBAction func showFunFact() {
    if (UIApplication.sharedApplication().applicationIconBadgeNumber != 0){
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }

    TechfactIndex = NSUserDefaults.standardUserDefaults().integerForKey("ByteLocation")

    if (TechfactIndex >= TechnologyfactBook.TechfactsArray.count) {  
        self.TechfactIndex = 0   
    }

    TechByteLabel.text = TechnologyfactBook.TechfactsArray[TechfactIndex]
    TechfactIndex++
    NSUserDefaults.standardUserDefaults().setInteger(TechfactIndex, forKey: "ByteLocation")
    NSUserDefaults.standardUserDefaults().synchronize()
}
但是,用户默认值通常保留为首选项。。。因此,最好在这里实现plist(或类似的简单保存选项)来存储当前索引

编辑:下面是一个简单的示例,说明如何使用plist实现这一点

首先,在目录中创建一个
属性列表
文件,称之为
Data.plist
。将根对象设置为字典,并使用键
FunFactIndex
添加NSNumber对象。这将是第一次保存时plist的模板

func showFunFact() {
    if (UIApplication.sharedApplication().applicationIconBadgeNumber != 0){
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }

    // Load the next index
    var factIndex = getCurrentFunFactIndex() as Int

    if factIndex < 0 {
        println("error")
        return
    }

    if (factIndex >= TechnologyfactBook.TechfactsArray.count) {
        factIndex = 0
    }

    TechByteLabel.text = TechnologyfactBook.TechfactsArray[factIndex]
    factIndex++

    // Save the index
    let saveSuccess = saveFunFactIndex(factIndex);

    let successString = (saveSuccess) ? "success" : "failure"

    println("Save was a \(successString)")
}

func getCurrentFunFactIndex() -> Int {

    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
    let documentsDirectory = paths.objectAtIndex(0) as? NSString
    let path = documentsDirectory!.stringByAppendingPathComponent("Data.plist")

    let fileManager = NSFileManager.defaultManager()

    // Check if file exists, copy it over from the bundle if it doesn't
    if !fileManager.fileExistsAtPath(path) {
        let bundle = NSBundle.mainBundle().pathForResource("Data", ofType: "plist")
        fileManager.copyItemAtPath(bundle!, toPath: path, error:nil)
    }

    if let dataDict = NSDictionary(contentsOfFile: path) {
        if let indexNum: AnyObject = dataDict.objectForKey("FunFactIndex") {
            return indexNum.integerValue
        }
    }

    return -1   // Something went wrong
}

func saveFunFactIndex(index: Int) -> Bool {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
    let documentsDirectory = paths.objectAtIndex(0) as! NSString
    let path = documentsDirectory.stringByAppendingPathComponent("Data.plist")

    let fileManager = NSFileManager.defaultManager()

    // Check if file exists, copy it over from the bundle if it doesn't
    if !fileManager.fileExistsAtPath(path) {
        let bundle = NSBundle.mainBundle().pathForResource("Data", ofType: "plist")
        fileManager.copyItemAtPath(bundle!, toPath: path, error:nil)
    }

    if let dataDict = NSMutableDictionary(contentsOfFile: path) {
        let indexNum: NSNumber = index
        dataDict.setObject(indexNum, forKey: "FunFactIndex")
        return dataDict.writeToFile(path, atomically: true)
    }

    return false
}
func showFunFact(){
if(UIApplication.sharedApplication().ApplicationOnBadgeNumber!=0){
UIApplication.sharedApplication()
}
//加载下一个索引
var factIndex=getCurrentFunFactIndex()作为Int
如果factIndex<0{
println(“错误”)
返回
}
if(factIndex>=TechnologyfactBook.TechfactsArray.count){
factIndex=0
}
TechByteLabel.text=TechnologyfactBook.TechfactsArray[factIndex]
事实索引++
//保存索引
让saveSuccess=saveFunFactIndex(factIndex);
让successString=(saveSuccess)?“success”:“failure”
println(“保存为\(成功字符串)”)
}
func getCurrentFunFactIndex()->Int{
将path=NSSearchPathForDirectoriesInDomains(.DocumentDirectory、.UserDomainMask,true)设为NSArray
将documentsDirectory=path.objectAtIndex(0)设为?NSString
让path=documentsDirectory!.stringByAppendingPathComponent(“Data.plist”)
让fileManager=NSFileManager.defaultManager()
//检查文件是否存在,如果不存在,则从捆绑包中复制它
if!fileManager.fileExistsAtPath(路径){
让bundle=NSBundle.mainBundle().pathForResource(“数据”,类型为“plist”)
fileManager.copyItemAtPath(bundle!,toPath:path,错误:nil)
}
如果让dataDict=NSDictionary(contentsOfFile:path){
如果let indexNum:AnyObject=dataDict.objectForKey(“FunFactIndex”){
返回indexNum.integerValue
}
}
return-1//出了问题
}
func saveFunFactIndex(索引:Int)->Bool{
将path=NSSearchPathForDirectoriesInDomains(.DocumentDirectory、.UserDomainMask,true)设为NSArray
将documentsDirectory=path.objectAtIndex(0)设为!NSString
让path=documentsDirectory.stringByAppendingPathComponent(“Data.plist”)
让fileManager=NSFileManager.defaultManager()
//检查文件是否存在,如果不存在,则从捆绑包中复制它
if!fileManager.fileExistsAtPath(路径){
让bundle=NSBundle.mainBundle().pathForResource(“数据”,类型为“plist”)
fileManager.copyItemAtPath(bundle!,toPath:path,错误:nil)
}
如果let dataDict=NSMutableDictionary(contentsOfFile:path){
let indexNum:NSNumber=索引
dataDict.setObject(indexNum,forKey:“FunFactIndex”)
返回dataDict.writeToFile(路径,原子性:true)
}
返回错误
}

我为您添加了两种方法
getCurrentFunFactIndex
saveFunFactIndex:。两者都将首先在沙箱的文档目录中检查
Data.plist
。如果该文件不存在,它将复制到我们在捆绑包中创建的模板plist上。这些方法的所有未来使用都将使用Documents目录中的
Data.plist`文件。这将允许该值在随后的应用程序启动时保持不变(从后台删除应用程序或关闭设备)。

每次查看下一个有趣的事实时,只写入用户默认值的操作并不昂贵。因此,为了简单起见,每次他们查看新的有趣事实时,您可以将新索引写入
NSUserDefaults

@IBAction func showFunFact() {
    if (UIApplication.sharedApplication().applicationIconBadgeNumber != 0){
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }

    TechfactIndex = NSUserDefaults.standardUserDefaults().integerForKey("ByteLocation")

    if (TechfactIndex >= TechnologyfactBook.TechfactsArray.count) {  
        self.TechfactIndex = 0   
    }

    TechByteLabel.text = TechnologyfactBook.TechfactsArray[TechfactIndex]
    TechfactIndex++
    NSUserDefaults.standardUserDefaults().setInteger(TechfactIndex, forKey: "ByteLocation")
    NSUserDefaults.standardUserDefaults().synchronize()
}
但是,用户默认值通常保留为首选项。。。因此,最好在这里实现plist(或类似的简单保存选项)来存储当前索引

编辑:下面是一个简单的示例,说明如何使用plist实现这一点

首先,在目录中创建一个
属性列表
文件,称之为
Data.plist
。将根对象设置为字典,并使用键
FunFactIndex
添加NSNumber对象。这将是第一次保存时plist的模板

func showFunFact() {
    if (UIApplication.sharedApplication().applicationIconBadgeNumber != 0){
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
    }

    // Load the next index
    var factIndex = getCurrentFunFactIndex() as Int

    if factIndex < 0 {
        println("error")
        return
    }

    if (factIndex >= TechnologyfactBook.TechfactsArray.count) {
        factIndex = 0
    }

    TechByteLabel.text = TechnologyfactBook.TechfactsArray[factIndex]
    factIndex++

    // Save the index
    let saveSuccess = saveFunFactIndex(factIndex);

    let successString = (saveSuccess) ? "success" : "failure"

    println("Save was a \(successString)")
}

func getCurrentFunFactIndex() -> Int {

    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
    let documentsDirectory = paths.objectAtIndex(0) as? NSString
    let path = documentsDirectory!.stringByAppendingPathComponent("Data.plist")

    let fileManager = NSFileManager.defaultManager()

    // Check if file exists, copy it over from the bundle if it doesn't
    if !fileManager.fileExistsAtPath(path) {
        let bundle = NSBundle.mainBundle().pathForResource("Data", ofType: "plist")
        fileManager.copyItemAtPath(bundle!, toPath: path, error:nil)
    }

    if let dataDict = NSDictionary(contentsOfFile: path) {
        if let indexNum: AnyObject = dataDict.objectForKey("FunFactIndex") {
            return indexNum.integerValue
        }
    }

    return -1   // Something went wrong
}

func saveFunFactIndex(index: Int) -> Bool {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
    let documentsDirectory = paths.objectAtIndex(0) as! NSString
    let path = documentsDirectory.stringByAppendingPathComponent("Data.plist")

    let fileManager = NSFileManager.defaultManager()

    // Check if file exists, copy it over from the bundle if it doesn't
    if !fileManager.fileExistsAtPath(path) {
        let bundle = NSBundle.mainBundle().pathForResource("Data", ofType: "plist")
        fileManager.copyItemAtPath(bundle!, toPath: path, error:nil)
    }

    if let dataDict = NSMutableDictionary(contentsOfFile: path) {
        let indexNum: NSNumber = index
        dataDict.setObject(indexNum, forKey: "FunFactIndex")
        return dataDict.writeToFile(path, atomically: true)
    }

    return false
}
func showFunFact(){
if(UIApplication.sharedApplication().ApplicationOnBadgeNumber!=0){
UIApplication.sharedApplication()
}
//加载下一个索引
var factIndex=getCurrentFunFactIndex()作为Int
如果factIndex<0{
println(“错误”)
返回
}
if(factIndex>=TechnologyfactBook.TechfactsArray.count){
factIndex=0
}
TechByteLabel.text=TechnologyfactBook.TechfactsArray[factIndex]
事实索引++
//保存索引
让saveSuccess=saveFunFactIndex(factIndex);
让successString=(saveSuccess)?“success”:“failure”
println(“保存为\(成功字符串)”)
}
func getCurrentFunFactIndex()->Int{
将path=NSSearchPathForDirectoriesInDomains(.DocumentDirectory、.UserDomainMask,true)设为NSArray
将documentsDirectory=path.objectAtIndex(0)设为?NSString
让path=documentsDirectory!.stringByAppendingPathComponent(“Data.plist”)
让fileManager=NSFileManager.defaultManager()
//检查文件是否存在,如果不存在,则从捆绑包中复制它
if!fileManager.fileExistsAtPath(路径){
让bundle=NSBundle.mainBundle().pathForResource(“数据”,类型为“plist”)
fileManager.copyItemAtPath(bundle!,toPath:path,错误:nil)
}
如果让dataDict=NSDictionary(contentsOfF