首页 \ 问答 \ NSUserDefaults - 保存和检索“高分”(NSUserDefaults - Saving and retrieving “High Scores”)

NSUserDefaults - 保存和检索“高分”(NSUserDefaults - Saving and retrieving “High Scores”)

我只是学习编程和做一些SWIFT教程。 我做了尽可能多的研究,但似乎缺少一些关键方面。 我有一个简单的游戏,当玩家死亡时,我想检查他们的分数与之前保存的高点。 如果它更高,则保存新分数。

在标题屏幕上显示最高分数。

这是我正在使用的当前代码;

NSUserDefaults.standardUserDefaults().integerForKey("highscore")

    if score > NSUserDefaults.standardUserDefaults().integerForKey("highscore") {
        NSUserDefaults.standardUserDefaults().setInteger(score, forKey: "highscore")
        NSUserDefaults.standardUserDefaults().synchronize()
    }

    NSUserDefaults.standardUserDefaults().integerForKey("highscore")

我无法解决的问题只是如何打印高分! 我试过用;

hiScoreText.text = "Hi Score :  \(highscore)"

但正如我猜测那不正确。 我使用什么变量或如何“读取”已保存的高分?

对于一个新人的任何建议将不胜感激。


I am just learning to program and doing some SWIFT tutorials. I have done as much research as I could but seem to be missing some critical aspect. I have a simple game and when a player dies I want to check their score against the previously saved high. If it's higher it's to save the new score.

On a title screen the highest score is displayed.

Here is the current code I am using;

NSUserDefaults.standardUserDefaults().integerForKey("highscore")

    if score > NSUserDefaults.standardUserDefaults().integerForKey("highscore") {
        NSUserDefaults.standardUserDefaults().setInteger(score, forKey: "highscore")
        NSUserDefaults.standardUserDefaults().synchronize()
    }

    NSUserDefaults.standardUserDefaults().integerForKey("highscore")

The problem I cannot solve is simply how to print the highscore! I tried using;

hiScoreText.text = "Hi Score :  \(highscore)"

but as I guessed that isn't correct. What variable do I use or how do I "read" the highscore that has been saved?

Any advice for a new guy would be greatly appreciated.


原文:https://stackoverflow.com/questions/26293391
更新时间:2022-10-18 14:10

最满意答案

Ruby中保证了哈希顺序。 我认为因为它离开Ruby并进入数据库,你不再得到保证了。 我不认为postgres会保留订单来源

[hstore]对的顺序不重要(可能不会在输出中重现)

如果你真的想要保留订单我认为你运气不好。 如果您希望订单相同,那么您应该以已知的方式对Ruby哈希进行排序,然后以相同的方式对SQL进行排序。 我相信有一种方法可以按键对hstore列进行排序: 按hstore中任意属性的值排序


Hash order is guaranteed in Ruby. I assume that because it's leaving Ruby and going to the database, you are not getting the guarantee anymore. I don't think postgres preserves the order source:

The order of the [hstore] pairs is not significant (and may not be reproduced on output)

If you really want the order to be preserved I think you're out of luck. If you want the order to be the same then you should sort the Ruby hash in a known way and then sort the SQL in the same way. I believe there's a way to sort an hstore column by keys: Order by a value of an arbitrary attribute in hstore

相关问答

更多
  • exist()函数测试是否存在密钥。 确定密钥“42”是否存在于hstore中的任何位置。 。 。 select * from (select test_id, exist(test_hs, '42') key_exists from test) x where key_exists = true; test_id key_exists -- 2 t svals()函数将值作为集合返回。 您可以查询结果以确定是否存在特定值。 select * from (select tes ...
  • 看起来你只是传递哈希。 我确实发现我必须将哈希值分配给一个对象,但我可能已经使用了parens来使其工作。 FactoryGirl.define do factory :thingy do hash = { foo: "foo", bar: "bar" } unstructured_data hash end end Looks you just pass a Hash. I did find thad I had to assign the ha ...
  • PostgreSQL hstore旨在存储键值对的文本字符串。 看看hstore文档 。 我已经使用JSON类型将数组存储为散列值。 有两种JSON数据类型:JSON和JSONB。 JSONB支持索引,但速度稍慢,而JSON不支持索引,但速度更快。 你可以在这里阅读它们: JSON类型 。 我会做以下事情: class Preference < ActiveRecord::Migration def change create_table :preferences do |t| t. ...
  • 好吧,如果有人遇到这样的麻烦,我设法解决了这个问题。 这就像Ruby上的童话故事之一,父母告诉孩子这个故事的寓意:'孩子们不要忘记尽可能使用括号,只是为了确保你的代码是正确的'。 问题是你已经注意到括号,我想它以一些其他方式解释了这个方法。它的工作原理如下: album.roles.merge!(Hash[users.pluck(:id).zip(params[:album][:roles])]) Well I managed to solve that question, if somebody gets ...
  • 我刚刚发现了这个问题。 该例外清楚地表明,存在不能自动投射的默认值。 我尝试投射的列有一个默认值。 更改为以下迁移工作。 change_column_default :table_name, :field_name, nil change_column :table_name, :field_name, 'jsonb USING CAST(field_name AS jsonb)' I just figured out the issue. The exception clearly states tha ...
  • 我认为你误解了PostgreSQL Array 。 Array实际上只是一个字符串。 您不能索引数组中的对象(在本例中为HSTORE ),因为它不是TABLE 。 相反,创建一个额外的表: CREATE TABLE customer ( pk bigserial PRIMARY KEY, customer hstore ); CREATE TABLE purchases ( pk bigserial PRIMARY KEY, customer_pk bigint not n ...
  • Ruby中保证了哈希顺序。 我认为因为它离开Ruby并进入数据库,你不再得到保证了。 我不认为postgres会保留订单来源 : [hstore]对的顺序不重要(可能不会在输出中重现) 如果你真的想要保留订单我认为你运气不好。 如果您希望订单相同,那么您应该以已知的方式对Ruby哈希进行排序,然后以相同的方式对SQL进行排序。 我相信有一种方法可以按键对hstore列进行排序: 按hstore中任意属性的值排序 Hash order is guaranteed in Ruby. I assume that ...
  • 这是我提出的一个解决方案很好的解决方案: scope :with_features, lambda { |features| #store the composed scope in local variable to build on composed_scope = self.scoped # if array, then loop through each if features.instance_of?(Array) features.each do |feature| ...
  • Craig告诉你在自己的架构中安装hstore。 这意味着沿着这些方向做点什么。 create schema hs; create extension hstore with schema hs; 这与存储hstore 数据的方式和位置无关。 如果我有一个名为“mike”的个人模式,我会使用像这样的hstore数据类型在“mike”模式中构建表。 create schema mike; create table mike.test ( some_column_name hs.hstore ); 您可 ...
  • 想出了问题。 HSTORE扩展安装在一个单独的模式中; 我能够通过识别它(sys.hstore)来调用该模式,但它仍然不喜欢运算符||。 修复实际上非常简单:我将sys添加到search_path。 Figured out the issue. The HSTORE extension was installed into a separate schema; I was able to call that schema by identifying it (sys.hstore), but it was ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。