我有一个控制器,它作为两个滚动视图的委托,放置在由前面提到的视图控制器管理的视图中.
为了区分两个滚动视图,我试图使用switch语句(而不是简单的指针与if语句比较).我已将这两个滚动视图标记为0和1
NSUInteger const kFirstScrollView = 0;
NSUInteger const kSecondScrollView = 1;
Run Code Online (Sandbox Code Playgroud)
当我尝试在switch语句中使用这些常量时,编译器说case语句不是常量.
switch (scrollView.tag) {
case kFirstScrollView: {
// do stuff
}
case kSecondScrollView: {
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
cocoa-touch switch-statement uiscrollview uiscrollviewdelegate
我正在使用Eclipse进行Python和R开发.如何使用键盘快捷键/热键在编辑器选项卡和控制台窗口之间跳转?
根据The Java Tutorials,在Java SE 7及更高版本中,您可以在switch语句的表达式中使用String对象.
String s = ...
switch(s){
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
但这是真的吗?我已经安装了JRE并将其添加到我的Eclipse项目的构建路径中,但是我收到以下编译时错误:
无法打开String类型的值.只允许使用可转换的int值或枚举常量
此外,我认为我已经正确配置,因为我能够使用它的java.nio.file.Files类,以及JLayer.
有任何想法吗?
例如:我将数字1到7映射到星期几.我可以使用七项case语句查找它们,或使用七项常量数组.哪个更快?
案例:
function GetDayNameBr(Num: Integer): String;
begin
case Num of
1: Result := 'Domingo';
2: Result := 'Segunda';
3: Result := 'Terça';
4: Result := 'Quarta';
5: Result := 'Quinta';
6: Result := 'Sexta';
7: Result := 'Sábado';
end;
end;
Run Code Online (Sandbox Code Playgroud)
常数组示例:
function GetDayNameBr(Num: Integer): String;
const
DayNames: array [1..7] of String = (
'Domingo',
'Segunda',
'Terça',
'Quarta',
'Quinta',
'Sexta',
'Sábado');
begin
Result := DayNames[Num];
end;
Run Code Online (Sandbox Code Playgroud) 我是否可以switch发表声明,决定返回什么?例如,我想根据我的想法返回不同的东西return.Eclipse给了我一个错误,希望我把switch声明放在外面switch.不确定要搜索什么,如果这是重复,请对不起.这是我的代码:
public String wordBank() { //Error here saying: "This method must return a type of string"
String[] wordsShapes = new String[10];
wordsShapes[1] = "square";
wordsShapes[2] = "circle";
wordsShapes[3] = "cone";
wordsShapes[4] = "prisim";
wordsShapes[5] = "cube";
wordsShapes[6] = "cylinder";
wordsShapes[7] = "triangle";
wordsShapes[8] = "star";
wordsShapes[9] = "moon";
wordsShapes[10] = "paralellogram";
Random rand = new Random();
int i = rand.nextInt(11);
if (i == 0) {
i = rand.nextInt(11);
}
switch (i) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的一个应用程序从Obj-C迁移到Swift,我遇到了电子邮件管理方面的问题.
我按小时搜索,但我没有找到如何解决这个问题.
基本上,我正在尝试迁移该func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!)功能.
问题是交换机内没有选项有效.
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!)
{
switch result.value
{
case CUnsignedInt(MFMailComposeResultCancelled):
var alert = UIAlertController(
title: NSLocalizedString("sendingStatus", tableName: "LocalizationFile", comment:"sendingStatus"),
message: NSLocalizedString("emailCancelledByUser", tableName: "LocalizationFile", comment:"emailCancelledByUser"),
preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
case MFMailComposeResult(MFMailComposeResultFailed):
var alert = UIAlertController(
title: NSLocalizedString("sendingStatus", tableName: "LocalizationFile", comment:"sendingStatus"),
message: NSLocalizedString("emailSentFailed", tableName: "LocalizationFile", comment:"emailSentFailed"),
preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
case MFMailComposeResultSaved:
var alert = UIAlertController(
title: NSLocalizedString("sendingStatus", tableName: …Run Code Online (Sandbox Code Playgroud) 我从VB.NET背景来到Objective-C,其中switch语句是,Select Case并且break不需要(或可能)语句.
我知道一般规则是break在每个case语句的末尾放置一个语句,以便执行不会"落到"下一个case语句.
在编写iOS应用程序时,我经常switch在我的-tableView: heightForRowAtIndexPath:方法中使用语句.基本上,我经常让我的单元格报告所需的高度,所以我最终得到这样的switch陈述:
switch (indexPath.row) {
case 0:
return ...
break;
case 1:
return ...
break;
...
default:
return ...
break;
}
Run Code Online (Sandbox Code Playgroud)
我看到了这个答案,这对我来说是有道理的,也是我期望答案的答案,但这个问题是关于Java的,我想看看Objective-C是否也有同样的答案.
我也找到了这个与C相关的答案,我认为这也是Objective-C的正确答案.
那么,return声明是一种专门的break声明吗?
我遇到了这个代码,它是Swift算法俱乐部中链接列表的Swift实现的一部分.在整个实现过程中,作者case let在打开一个可选项之前使用了一个while语句.我从来没有看到在switch语句的上下文之外使用case关键字,我想知道它到底是做什么的?它是否以某种方式将该let next? = node.next部分转换为真或假,可能取决于是否next?变为零?
public var last: Node? {
if var node = head {
while case let next? = node.next {
node = next
}
return node
} else {
return nil
}
}
Run Code Online (Sandbox Code Playgroud) 我需要对两个解决方案进行性能测试 - 一个使用多态来执行开启类型,另一个使用switch case来选择执行哪些函数.我真的需要优化这段代码.我编写了以下测试用例(您可以简单地复制粘贴代码,编译它g++ -std=c++14 -O3并运行它echo 1 | ./a.out!)如果您阅读它,代码非常简单!
#include <iostream>
#include <chrono>
#include <functional>
#include <array>
#include <cassert>
#include <vector>
#include <memory>
using namespace std;
struct profiler
{
std::string name;
std::chrono::high_resolution_clock::time_point p;
profiler(std::string const &n) :
name(n), p(std::chrono::high_resolution_clock::now()) { }
~profiler()
{
using dura = std::chrono::duration<double>;
auto d = std::chrono::high_resolution_clock::now() - p;
std::cout << name << ": "
<< std::chrono::duration_cast<dura>(d).count()
<< std::endl;
}
};
#define PROFILE_BLOCK(pbn) profiler _pfinstance(pbn)
class Base {
public:
virtual …Run Code Online (Sandbox Code Playgroud) switch-statement ×10
eclipse ×2
java ×2
return ×2
swift ×2
arrays ×1
c++ ×1
c++11 ×1
c++14 ×1
cocoa-touch ×1
delphi ×1
hotkeys ×1
java-7 ×1
methods ×1
objective-c ×1
performance ×1
polymorphism ×1
python ×1
string ×1
tabs ×1
uiscrollview ×1