内部条件下的快速分配

时间:2015-06-13 16:06:06

标签: swift conditional-statements variable-assignment swift2

我想知道这段代码的作用:

var something: String = "Hi"

if something = "Hello world!" {
    // Will this be executed?
}

它会分配给something变量并执行if正文吗?或者它是否仅为if正文设置该变量的值,在外面它不会改变?或者它与nil有什么关系?

3 个答案:

答案 0 :(得分:2)

分配不是返回布尔值的表达式,因此不能在 DatasetGroup * make_array(){ DatasetGroup *dg_array; // Allocate space for a few groups dg_array = (DatasetGroup *) malloc(sizeof(DatasetGroup) * INCREMENT); return dg_array; } void add_group_to_array(DatasetGroup *dg_array, ...){ // Add a datasetgroup DatasetGroup *dg = new_DatasetGroup(...); // groupCount - 1 as the count is incremented when the group is created, so will always be one ahead of the array index we want to assign to dg_array[groupCount - 1] = dg; if (groupCount % INCREMENT == 0) { //Grow the array dg_array = realloc(dg_array, sizeof(DatasetGroup) * (groupCount + INCREMENT)); } } 这样的内部使用。所以这不会编译。

(虽然你会得到一个误导性的编译器消息)

assignment in if failing to compile

答案 1 :(得分:1)

此模式仅适用于可能失败的分配 - 也就是说,如果您正在分配返回Optional值的表达式的结果。在这种情况下,您使用的是if let,而不只是if

答案 2 :(得分:0)

我们不能对if条件使用赋值运算符,如果让假设你正在使用可选类型

,那么你会吗?

以下是一些可帮助您明确区分赋值运算符的运算符

    salaries= load 'salaries' using PigStorage(',') As (gender, age,salary,zip);

    salaries= load 'salaries' using PigStorage(',') As (gender:chararray,age:int,salary:double,zip:long);

    salaries=load 'salaries' using PigStorage(',') as (gender:chararray,details:bag{b(age:int,salary:double,zip:long)});

    highsal= filter salaries by salary > 75000;

    dump highsal

    salbyage= group salaries by age;

    describe salbyage;

    salbyage= group salaries All;

    salgrp= group salaries by $3;

    A= foreach salaries generate age,salary;

    describe A;

    salaries= load 'salaries.txt' using PigStorage(',') as (gender:chararray,age:int,salary:double,zip:int);



    vivek@ubuntu:~/Applications/Hadoop_program/pip$ pig -x mapreduce p.pig 
15/09/24 03:16:32 INFO pig.ExecTypeProvider: Trying ExecType : LOCAL
15/09/24 03:16:32 INFO pig.ExecTypeProvider: Trying ExecType : MAPREDUCE
15/09/24 03:16:32 INFO pig.ExecTypeProvider: Picked MAPREDUCE as the ExecType
2015-09-24 03:16:32,990 [main] INFO  org.apache.pig.Main - Apache Pig version 0.14.0 (r1640057) compiled Nov 16 2014, 18:02:05
2015-09-24 03:16:32,991 [main] INFO  org.apache.pig.Main - Logging error messages to: /home/vivek/Applications/Hadoop_program/pip/pig_1443089792987.log
2015-09-24 03:16:38,966 [main] WARN  org.apache.hadoop.util.NativeCodeLoader - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
2015-09-24 03:16:41,232 [main] INFO  org.apache.pig.impl.util.Utils - Default bootup file /home/vivek/.pigbootup not found
2015-09-24 03:16:42,869 [main] INFO  org.apache.hadoop.conf.Configuration.deprecation - mapred.job.tracker is deprecated. Instead, use mapreduce.jobtracker.address
2015-09-24 03:16:42,870 [main] INFO  org.apache.hadoop.conf.Configuration.deprecation - fs.default.name is deprecated. Instead, use fs.defaultFS
2015-09-24 03:16:42,870 [main] INFO  org.apache.pig.backend.hadoop.executionengine.HExecutionEngine - Connecting to hadoop file system at: hdfs://localhost:9000
2015-09-24 03:16:45,436 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1000: Error during parsing. Encountered " <PATH> "salaries=load "" at line 7, column 1.
Was expecting one of:
    <EOF> 
    "cat" ...
    "clear" ...
    "fs" ...
    "sh" ...
    "cd" ...
    "cp" ...
    "copyFromLocal" ...
    "copyToLocal" ...
    "dump" ...
    "\\d" ...
    "describe" ...
    "\\de" ...
    "aliases" ...
    "explain" ...
    "\\e" ...
    "help" ...
    "history" ...
    "kill" ...
    "ls" ...
    "mv" ...
    "mkdir" ...
    "pwd" ...
    "quit" ...
    "\\q" ...
    "register" ...
    "rm" ...
    "rmf" ...
    "set" ...
    "illustrate" ...
    "\\i" ...
    "run" ...
    "exec" ...
    "scriptDone" ...
    "" ...
    "" ...
    <EOL> ...
    ";" ...

Details at logfile: /home/vivek/Applications/Hadoop_program/pip/pig_1443089792987.log
2015-09-24 03:16:45,554 [main] INFO  org.apache.pig.Main - Pig script completed in 13 seconds and 48 milliseconds (13048 ms)

vivek@ubuntu:~/Applications/Hadoop_program/pip$