首页 \ 问答 \ NSNumber未从NSUserDefaults准确设置(NSNumber not being accurately set from NSUserDefaults)

NSNumber未从NSUserDefaults准确设置(NSNumber not being accurately set from NSUserDefaults)

在ViewControllerA中,我设置以下内容:

NSNumber *phaseIsKnownNSNum = 1

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:phaseIsKnownNSNum forKey:@"Phase is Known"];
[defaults synchronize];

在ViewControllerB中,只要我不使用相同的“phaseIsKnownNSNum”变量名,我就可以从用户默认值中提取此值。 在ViewControllerB中按下按钮后发生以下情况:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

//using 2 instances of the standardUserDefaults, just to be sure.
NSUserDefaults *defaultsTest = [NSUserDefaults standardUserDefaults];

NSNumber *testNum = [defaultsTest objectForKey:@"Phase is Known"];

NSLog(@"in ViewControllerB, btn pressed:  test %d", [testNum intValue]);

这记录“在ViewControllerB中,btn按下:测试1”。

NSNumber *testNum2 = [defaultsTest objectForKey:@"Phase is Known"];

NSLog(@"in ViewControllerB, btn pressed:  test2  %d", [testNum2 intValue]);

这记录“在ViewControllerB中,按下btn:test2 1”

testNum = [defaults objectForKey:@"Phase is Known"];

NSLog(@"in ViewControllerB, btn pressed:  test %d", [testNum intValue]);

这记录“在ViewControllerB中,btn按下:测试1”

当我在ViewControllerB中尝试使用“phaseIsKnownNSNum”时,我并不是那么幸运。

//I've tried not allocating & initializing.  Gives the same issue.
NSNumber *phaseIsKnownNSNum = [[NSNumber alloc] initWithInt:1];

NSLog(@"phaseIsKnownNSNum from initialized value:  %d", [phaseIsKnownNSNum intValue]);

此日志:“初始值为phaseIsKnownNSNum:1”

phaseIsKnownNSNum = [defaultsTest objectForKey:@"Phase Is Known"];

NSLog(@"phaseIsKnownNSNum from defaultsTest:  %d", [phaseIsKnownNSNum intValue]);

这会记录“defaultsTest:0中的phaseIsKnownNSNum”。

设置断点并检查phaseIsKnownNSNum的值,它是零。

phaseIsKnownNSNum = [defaults objectForKey:@"Phase Is Known"];

NSLog(@"phaseIsKnownNSNum from defaults:  %d", [phaseIsKnownNSNum intValue]);

这会记录“phaseIsKnownNSNum默认值:0”。

设置断点并检查phaseIsKnownNSNum的值,它仍然是零。

重复使用相同名称存储和检索用户默认值是否存在问题? 我知道简单的解决方案是使用不同的变量名称。 但是,在ViewControllers C,D,E中检索此默认值,...

每次NSNumber变量名称必须不同是没有意义的。


In ViewControllerA, I am setting the following:

NSNumber *phaseIsKnownNSNum = 1

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:phaseIsKnownNSNum forKey:@"Phase is Known"];
[defaults synchronize];

In ViewControllerB, I'm able to pull this value from user defaults as long as I don't use the same "phaseIsKnownNSNum" variable name. The following occurs after a button press in ViewControllerB:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

//using 2 instances of the standardUserDefaults, just to be sure.
NSUserDefaults *defaultsTest = [NSUserDefaults standardUserDefaults];

NSNumber *testNum = [defaultsTest objectForKey:@"Phase is Known"];

NSLog(@"in ViewControllerB, btn pressed:  test %d", [testNum intValue]);

this logs "in ViewControllerB, btn pressed: test 1".

NSNumber *testNum2 = [defaultsTest objectForKey:@"Phase is Known"];

NSLog(@"in ViewControllerB, btn pressed:  test2  %d", [testNum2 intValue]);

this logs "in ViewControllerB, btn pressed: test2 1"

testNum = [defaults objectForKey:@"Phase is Known"];

NSLog(@"in ViewControllerB, btn pressed:  test %d", [testNum intValue]);

this logs "in ViewControllerB, btn pressed: test 1"

when trying to use "phaseIsKnownNSNum" in ViewControllerB, I am not so lucky.

//I've tried not allocating & initializing.  Gives the same issue.
NSNumber *phaseIsKnownNSNum = [[NSNumber alloc] initWithInt:1];

NSLog(@"phaseIsKnownNSNum from initialized value:  %d", [phaseIsKnownNSNum intValue]);

this logs: "phaseIsKnownNSNum from initialized value: 1"

phaseIsKnownNSNum = [defaultsTest objectForKey:@"Phase Is Known"];

NSLog(@"phaseIsKnownNSNum from defaultsTest:  %d", [phaseIsKnownNSNum intValue]);

this logs "phaseIsKnownNSNum from defaultsTest: 0".

setting a breakpoint and inspecting the value of phaseIsKnownNSNum, it is nil.

phaseIsKnownNSNum = [defaults objectForKey:@"Phase Is Known"];

NSLog(@"phaseIsKnownNSNum from defaults:  %d", [phaseIsKnownNSNum intValue]);

this logs "phaseIsKnownNSNum from defaults: 0".

setting a breakpoint and inspecting the value of phaseIsKnownNSNum, it is still nil.

Is there some issue with reusing the same name for storing and retrieving user defaults? I know the simple solution is to use a different variable name. However, this default is retrieved in ViewControllers C, D, E, ...

It wouldn't make sense that the NSNumber variable name would have to be different each time.


原文:https://stackoverflow.com/questions/35385734
更新时间:2024-01-18 12:01

最满意答案

udf函数不应该是选择,如果你有spark功能做同样的工作, udf函数会序列化和反序列化列数据。

给定dataframe

+---+----+------+-----+---+---+-----+---+---+--------------+
|id |age |rbc   |bgr  |dm |cad|appet|pe |ane|classification|
+---+----+------+-----+---+---+-----+---+---+--------------+
|3  |48.0|normal|117.0|no |no |poor |yes|yes|ckd           |
+---+----+------+-----+---+---+-----+---+---+--------------+

您可以when功能为as when达到您的要求

import org.apache.spark.sql.functions._
def applyFunction(column : Column) = when(column === "yes" || column === "present" || column === "normal", lit(1))
  .otherwise(when(column === "no" || column === "notpresent" || column === "abnormal", lit(0)).otherwise(column))

df.withColumn("dm", applyFunction(col("dm")))
  .withColumn("cad", applyFunction(col("cad")))
  .withColumn("rbc", applyFunction(col("rbc")))
  .withColumn("pe", applyFunction(col("pe")))
  .withColumn("ane", applyFunction(col("ane")))
  .show(false)

结果是

+---+----+---+-----+---+---+-----+---+---+--------------+
|id |age |rbc|bgr  |dm |cad|appet|pe |ane|classification|
+---+----+---+-----+---+---+-----+---+---+--------------+
|3  |48.0|1  |117.0|0  |0  |poor |1  |1  |ckd           |
+---+----+---+-----+---+---+-----+---+---+--------------+

现在问题清楚地表明,您不希望重复所有列的过程,因为您可以执行以下操作

val columnsTomap = df.select("rbc", "cad", "rbc", "pe", "ane").columns

var tempdf = df
columnsTomap.map(column => {
  tempdf = tempdf.withColumn(column, applyFunction(col(column)))
})

tempdf.show(false)

udf functions should not be the choice if you have spark functions to do the same job as udf functions would serialize and deserialize the column data.

Given a dataframe as

+---+----+------+-----+---+---+-----+---+---+--------------+
|id |age |rbc   |bgr  |dm |cad|appet|pe |ane|classification|
+---+----+------+-----+---+---+-----+---+---+--------------+
|3  |48.0|normal|117.0|no |no |poor |yes|yes|ckd           |
+---+----+------+-----+---+---+-----+---+---+--------------+

You can achieve your requirement with when function as

import org.apache.spark.sql.functions._
def applyFunction(column : Column) = when(column === "yes" || column === "present" || column === "normal", lit(1))
  .otherwise(when(column === "no" || column === "notpresent" || column === "abnormal", lit(0)).otherwise(column))

df.withColumn("dm", applyFunction(col("dm")))
  .withColumn("cad", applyFunction(col("cad")))
  .withColumn("rbc", applyFunction(col("rbc")))
  .withColumn("pe", applyFunction(col("pe")))
  .withColumn("ane", applyFunction(col("ane")))
  .show(false)

The result is

+---+----+---+-----+---+---+-----+---+---+--------------+
|id |age |rbc|bgr  |dm |cad|appet|pe |ane|classification|
+---+----+---+-----+---+---+-----+---+---+--------------+
|3  |48.0|1  |117.0|0  |0  |poor |1  |1  |ckd           |
+---+----+---+-----+---+---+-----+---+---+--------------+

Now the question clearly says that you don't want to repeat the procedure for all the columns for that you can do the following

val columnsTomap = df.select("rbc", "cad", "rbc", "pe", "ane").columns

var tempdf = df
columnsTomap.map(column => {
  tempdf = tempdf.withColumn(column, applyFunction(col(column)))
})

tempdf.show(false)

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取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的基本操作命令。。。