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
最满意答案
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
功能为aswhen
达到您的要求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 havespark
functions to do the same job asudf
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 asimport 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)
相关问答
更多-
你的数据集不干净。 985 split('\t')为只有一个值: >>> from operator import add >>> lines = sc.textFile("classified_tweets.txt") >>> parts = lines.map(lambda l: l.split("\t")) >>> parts.map(lambda l: (len(l), 1)).reduceByKey(add).collect() [(2, 149195), (1, 985)] >>> parts. ...
-
您实际上使用此数据类型手动将矢量类型嵌套到结构中: new StructField("bla", new VectorUDT(), false, Metadata.empty()), 如果删除外部StructField,您将获得所需的内容。 当然,在这种情况下,您需要修改函数定义的签名。 也就是说,您需要使用Vector类型返回。 请参阅下面我的简单JUnit测试形式的具体例子。 package sample.spark.test; import org.apache.spark.api.java.Ja ...
-
在DataFrame中的ArrayType列上操作时,传递给UDF的实际类型是mutable.WrappedArray 。 您看到的失败是尝试将此WrappedArray转换为您的函数所期望的Array[Int]的结果。 修复相当简单 - 定义函数以期望mutable.WrappedArray[Int] : def indexer (values: Array[String]): UserDefinedFunction = { udf((indices: mutable.WrappedArray[Int ...
-
udf函数不应该是选择,如果你有spark功能做同样的工作, udf函数会序列化和反序列化列数据。 给定dataframe为 +---+----+------+-----+---+---+-----+---+---+--------------+ |id |age |rbc |bgr |dm |cad|appet|pe |ane|classification| +---+----+------+-----+---+---+-----+---+---+--------------+ |3 |48.0| ...
-
实际上这种方法在Python中运行得很好: from pyspark.sql.functions import array, udf df = sc.parallelize([("a", "b", "c", "d")]).toDF() f = udf(lambda xs: "+".join(xs)) df.select(f("_1")).show() ## +------------+ ## |
(_1)| ## +------------+ ## | a| ## + ... -
当我尝试提取不存在的元素时,我可以重现错误,即给出一个大于序列长度的索引: val myDF = Seq(Seq(1.0, 2.0 ,3, 4.0), Seq(4.0,3,2,1)).toDF("vector") myDF: org.apache.spark.sql.DataFrame = [vector: array
] def extract (index : Integer) = udf((v: Seq[Double]) => v.grouped(2).toSeq(index)) // ... -
你可以创建一个带有两列的udf并执行你的逻辑: val isMatchedCREDITDEBIT = udf((credit_account_id: String, debit_account_id: String) => { credit_account_id == debit_account_id }) 可以在when子句中调用 when(isMatchedCREDITDEBIT(df("credit_account_id"), df("debit_account_id")), "MATCHING ...
-
我可以从apache spark UDF(在java中)返回Tuple2吗?(Can I return a Tuple2 from an apache spark UDF (in java)?)[2023-05-13]
有一个schema可以定义如下: import org.apache.spark.sql.types.DataType; import org.apache.spark.sql.types.DataTypes; import org.apache.spark.mllib.linalg.VectorUDT; Listfields = new ArrayList<>(); fields.add(DataTypes.createStructField("id", DataTypes. ... -
如果已创建函数,则可以使用以下命令注册创建的UDF: sparksession.sqlContext.udf.register(yourFunction) 我希望这有帮助。 If you have created a function, you can register the created UDF using: sparksession.sqlContext.udf.register(yourFunction) I hope this helps.
-
你的entities类包含一个SparkContext成员 - 所以它不能被序列化(SparkContexts在国际上不可序列化,你不应该序列化它们)。 由于entities不是可序列化的,它的任何非静态方法/成员/匿名内部类都不可序列化(因为它们会尝试序列化持有它们的entities实例)。 在这种情况下,最好的解决方法是将匿名UDF提取到类的静态成员中: private final static UDF2 get_hashed_id = new UDF2
( ...