gpt4 book ai didi

objective-c - 在 switch block 中实例化新对象 - 为什么会失败?

转载 作者:太空狗 更新时间:2023-10-30 03:59:19 27 4
gpt4 key购买 nike

为什么

switch ([document currentTool]) {
case DrawLine:
NSBezierPath * testPath = [[NSBezierPath alloc]init];
//...rest of code that uses testPath....

结果

错误:“*”标记前的语法错误

测试路径?

最佳答案

你不能在 case 语句中实例化一个对象,除非你把它放在一个新的范围内。这是因为否则你可以做这样的事情:

switch( ... ) {
case A:
MyClass obj( constructor stuff );
// more stuff
// fall through to next case
case B:
// what is the value of obj here? The constructor was never called
...
}

如果您希望对象在案例期间持续存在,您可以这样做:

switch( ... ) {
case A: {
MyClass obj( constructor stuff );
// more stuff
// fall through to next case
}
case B:
// obj does not exist here
...
}

这在 Objective C 以及 C 和 C++ 中是相同的。

关于objective-c - 在 switch block 中实例化新对象 - 为什么会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/366073/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com