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

swift不允许在函数参数中进行初始化吗?,swift,sprite-kit,Swift,Sprite Kit,第一个块生成一个通用的Swift编译错误(甚至不指向行) 第二个街区很好用 let screenDivision = size.width / 5; var a = CGSize(width: 2 * screenDivision, height: size.height) var game1 = SKSpriteNode(color: .redColor(), size: a); 为什么??我不想再次遇到这个错误,也不记得我在哪里写了这样的代码 (我还需要给它传递一个变量,而不是常数?Wtf

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

第二个街区很好用

let screenDivision = size.width / 5;
var a = CGSize(width: 2 * screenDivision, height: size.height)
var game1 = SKSpriteNode(color: .redColor(), size: a);
为什么??我不想再次遇到这个错误,也不记得我在哪里写了这样的代码


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

如果在调用
redColor
时包含
UIColor
,错误就会消失:

let screenDivision = size.width / 5;
var game1 = SKSpriteNode(
    color: UIColor.redColor(),
    size: CGSize(width: 2 * screenDivision,
    height: size.height)
)
我不知道为类方法省略类型名的能力。我只知道在列举案例时才可能做到这一点。Swift编程指南明确指出,这在枚举中是可能的,但在本节中没有说明


事实上,我很惊讶第二个版本能起作用。现在,除非您引用的是枚举案例,否则我将始终包含类型名。

这似乎是一个编译器错误

在这种情况下,应执行以下操作

隐式成员表达式是在类型推断可以确定隐式类型的上下文中访问类型成员(例如枚举案例或类方法)的一种缩写方式。其形式如下:

成员名称

我复制了与此代码完全相同的问题:

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 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)
*/
enum MyEnum{
案例1
}
func f(arg:MyEnum!,arg2:Int){}
f(.Case1,1)
/*
0 swift 0x00000001062d8a68 llvm::sys::PrintStackTrace(u sFILE*)+40
1 swift 0x00000001062d8f54信号处理器(内部)+452
2 libsystem_platform.dylib 0x00007fff8a314f1a_sigtramp+26
3 libsystem_platform.dylib 0x00007fd8085362e8_sigtramp+2116162536
4 swift 0x00000001056e8e98 emitApplyArgument((匿名名称空间)::IRGenSILFunction&,swift::SILValue,swift::SILParameterInfo,llvm::ArrayRef,swift::irgen::Explosion&+456
5 swift 0x00000001056e5f1d swift::SILVisitor::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性能生成(swift::IRGenOptions&,swift::Module*,swift::SILModule*,llvm::StringRef,llvm::LLVMContext&,swift::SourceFile*,unsigned int)+2038
9 swift 0x00000001056ca2e3 swift::性能生成(swift::IRGenOptions&,swift::SourceFile&,swift::SILModule*,llvm::StringRef,llvm::LLVMContext&,unsigned int)+51
10 swift 0x000000010561f5f4前端_main(llvm::ArrayRef,char const*,void*)+5444
11 swift 0x000000010561c96d干管+1677
12 libdyld.dylib 0x00007fff90b525c9启动+1
13 libdyld.dylib 0x0000000000000010开始+1867176520
堆栈转储:
0程序参数:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend-c-主文件test.swift-目标x86_64-apple-darwin14.0.0-目标cpu核心2-sdk/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk-颜色诊断-模块名称测试-o/var/folders/kb/xgglxb597sv6h8b744d5vft0000gn/T/test-4cf5cc.o
1.在发出IR SIL功能@top_level_代码:0:错误:无法执行命令:分段错误:11
:0:错误:swift前端命令因信号而失败(使用-v查看调用)
*/
两种情况下,删除
解决了问题


比如说:“不要对隐式展开的可选上下文使用隐式成员表达式”

仅供参考,“隐式成员表达式”在语法意义上是不同的。
.redColor()
应该在这个上下文中编译。@rintaro,我不知道语法中有这一点,谢谢你指出。这在什么时候可能还很模糊。例如,这是否只有在类方法位于要返回的类上时才可能,或者它是否适用于任何返回所需类型的类方法?Drewag您的答案直接解决了我的问题。非常感谢!这可能是swift的一个错误,但你的解决方案确实解决了这个问题,所以干杯,伙计。@drewag看到rintaro的答案了。捕捉得好,回答得好。一定要!谢谢,这让我感觉更清醒。不幸的是,我无法控制SKSpriteNode初始值设定项来更改它的原型参数,所以我必须使用drewag的解决方案。不过,你发现这是一件很酷的事情。我会提交bug,尽管我确信其他人已经提交了。当然。我只是想了解“是什么导致了这个错误”。应该注意的是,这个答案已经过时了:至少在Swift 3.2中,崩溃不再发生。我还没有做过广泛的研究来发现这个问题何时得到解决,但至少在Swift 3.2中,这不再是一个问题
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)
*/