swift不允许在函数参数中初始化吗?

时间:2014-11-14 00:46:53

标签: swift sprite-kit

第一个块生成一个通用的Swift编译错误(甚至没有指向该行)

let screenDivision = size.width / 5;
var game1 = SKSpriteNode(color: .redColor(), size: CGSize(width: 2 * screenDivision, height: size.height));

第二块可以正常工作。

let screenDivision = size.width / 5;
var a = CGSize(width: 2 * screenDivision, height: size.height)
var game1 = SKSpriteNode(color: .redColor(), size: a);

为什么呢?我不想再次遇到这个错误而不记得我在哪里写了这样的代码。

(我还需要传递一个变量,而不是常量?Wtf ... [如果我改变var a,让a,我得到编译错误])

2 个答案:

答案 0 :(得分:4)

如果您在UIColor的电话中加入redColor,则错误就会消失:

let screenDivision = size.width / 5;
var game1 = SKSpriteNode(
    color: UIColor.redColor(),
    size: CGSize(width: 2 * screenDivision,
    height: size.height)
)

我不知道能否为类方法省略类型名称。我只知道Enumeration案例可以实现。 Swift编程指南mentions特别指出这可以通过枚举实现,但在type method部分中没有说明。

我真的很惊讶第二个版本有效。现在,除非您指的是枚举案例,否则我将始终包含类型名称。

答案 1 :(得分:4)

看来,这是一个编译器错误。

在这种情况下,应该执行"Implicit Member Expression"

  

隐式成员表达式是在类型推断可以确定隐含类型的上下文中访问类型成员的缩写方式,例如枚举大小写或类方法。它具有以下形式:

     
    

. 会员姓名

  

我使用此代码重现了完全相同的问题:

class MyClass {
    class func create() -> MyClass { return MyClass() }
}

func f(arg:MyClass!, arg2:Int) { } 

f(.create(), 1)

/*
Bitcast requires both operands to be pointer or neither
  %.asUnsubstituted = bitcast %C4test7MyClass* %6 to i64
LLVM ERROR: Broken function found, compilation aborted!
*/

甚至是enum

enum MyEnum {
    case Case1
}

func f(arg:MyEnum!, arg2:Int) { } 

f(.Case1, 1)

/*
0  swift                    0x00000001062d8a68 llvm::sys::PrintStackTrace(__sFILE*) + 40
1  swift                    0x00000001062d8f54 SignalHandler(int) + 452
2  libsystem_platform.dylib 0x00007fff8a314f1a _sigtramp + 26
3  libsystem_platform.dylib 0x00007fd8085362e8 _sigtramp + 2116162536
4  swift                    0x00000001056e8e98 emitApplyArgument((anonymous namespace)::IRGenSILFunction&, swift::SILValue, swift::SILParameterInfo, llvm::ArrayRef<swift::Substitution>, swift::irgen::Explosion&) + 456
5  swift                    0x00000001056e5f1d swift::SILVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::ValueBase*) + 34605
6  swift                    0x00000001056dcbab swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 9179
7  swift                    0x0000000105656347 swift::irgen::IRGenModule::emitLazyDefinitions() + 199
8  swift                    0x00000001056c9966 performIRGeneration(swift::IRGenOptions&, swift::Module*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 2038
9  swift                    0x00000001056ca2e3 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, unsigned int) + 51
10 swift                    0x000000010561f5f4 frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 5444
11 swift                    0x000000010561c96d main + 1677
12 libdyld.dylib            0x00007fff90b525c9 start + 1
13 libdyld.dylib            0x0000000000000010 start + 1867176520
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file test.swift -target x86_64-apple-darwin14.0.0 -target-cpu core2 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -color-diagnostics -module-name test -o /var/folders/kb/xgglxb597sv6h8b744d5vft00000gn/T/test-4cf5cc.o 
1.  While emitting IR SIL function @top_level_code<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
*/

这两种情况,从函数参数中删除!都可以解决问题。

让我们说:“不要使用隐式成员表达式隐式展开的可选上下文”