草庐IT

Switch-case

全部标签

ios - Swift : 'case .success(let dict):' 是什么意思

在SwiftPromiseKit库中有一个使用有点奇怪语法的Alamofire示例:funclogin(completionHandler:(NSDictionary?,ErrorProtocol?)->Void{Alamofire.request(.GET,url,parameters:["foo":"bar"]).validate().responseJSON{responseinswitchresponse.result{case.success(letdict):completionHandler(dict,nil)case.failure(leterror):completi

swift - 在 Swift switch 语句中使用 `let` 编译错误

在Swift中,您可以使用范围运算符获取数组的一部分,如下所示:letlist:String[]=["first","middle","last"]letcdr=list[1..list.endIndex]assert(cdr==["middle","last"])我正在尝试在采用String[]参数的递归函数中做同样的事情,但没有任何运气:funclast(xs:String[])->String?{switchxs{caselet(singleItemList)wheresingleItemList.endIndex==1:returnsingleItemList[0]casele

swift - Swift语言中的switch语句,其中有where的case子句中的执行顺序是什么?

假设我们有以下伪代码片段:switchsome_variable{caseletvwhere:do_something...}据我了解,当代码执行进入switch时,它首先执行第一个case语句(我们只有一个)。然后它检查condition_checking部分,如果它是真的,那么let部分将被执行并且do_something将有机会运行。对吗?我问这个问题是因为我在Apple文档中看到了以下代码片段https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Langu

xcode - 为什么 Xcode 告诉我最后一个 case 永远不会被执行?

关于StackOverflow上的另一个问题,我在Swift中输入了一些测试代码,令我惊讶的是它告诉我最后一个案例永远不会被执行(带有.B,.C,.D),有人有什么想法吗?functest(someEnum:EnumType){switchsomeEnum{case.A:someMethodSpecificToA()fallthroughcase.B,.C,.D:someMethodSpecificToTheseThreeLetters()fallthroughcase.E:someMethodSpecificToE()fallthroughcase.A,.E:aMethodIShou

ios - 枚举 : Count and list all cases (from outside! )

我在自己的文件(“MyEnum.swift”)中有这个枚举(Xcode10/Swift5):enumMyEnum:Int,CaseIterable{caseA=0caseB=1caseC=2caseD=3caseE=4//Eachcaseneedsitsownnumberanddescription!vardescription:String{switchself{case.A:return"abc"case.B:return"bcd"case.C:return"cde"case.D:return"def"case.E:return"efg"}}}...并希望将描述添加到PickerV

Swift:Switch 语句失败行为

目前我有这个:letsomePoint=(1,0)switchsomePoint{case(0,0):print("origin")//doesnotprintfallthroughcase(_,0):print("y-axis")//printsy-axis.thismakessensefallthroughcase(0,_):print("x-axis")//printsx-axis(becauseoffallthrough?thisshouldnotprint)fallthroughcase(-2...2,-2...2):print("in5x5boxabouttheorigin

swift - 替代 SwiftUI ViewBuilder block 中的 switch 语句?

⚠️2020年6月23日编辑:从Xcode12开始,ViewBuilder将支持switch和iflet语句!我一直在尝试使用SwiftUI复制我的一个应用程序。它有一个RootViewController,它根据枚举值显示不同的subviewController。与在SwiftUI中一样,我们使用View而不是ViewController,我的代码如下所示:structRootView:View{@StatevarcontainedView:ContainedView=.homevarbody:someView{//customheadergoeshereswitchcontaine

ios - Xcode 6.1 if/else/for/switch 语句不自动完成(仅限 Swift)

我没有遇到其他帖子中描述的自动完成问题。正常功能自动完成工作得很好。我的问题是我正在运行的Xcode版本(6.1,Swift)不会自动完成/自动建议任何基本程序语句(if、for、switch、do等)。例如,如果我输入“if”,几秒钟后会弹出建议菜单;但是,窗口中没有通常描述语句功能的文档(同时显示自动布局示例以供填写)。如果我在Objective-C的相同版本的Xcode中尝试一个项目,这些基本的程序语句就可以很好地完成。我已经:1)确保我的设置设置为自动建议完成2)确保我已清除DerivedData以及之前帖子中建议的所有其他内容3)无数次重新安装Xcode并重启Mac4)从“X

swift - 枚举 : Switch case for variables of type

当我尝试打开具有枚举friendstate的optional时,当我尝试在下面的switch语句中应用它时,为什么会出现错误?提供的错误是在类型“friendState!”中找不到枚举案例“已添加”(所有情况下都会出现错误)有没有办法解决这个错误?varusernameText:String!varUID:Int!varuserDetails:UserState?varbuttonText:String{switch(userDetails!.state){case.added:return"remove"//erroroccurscase.incoming:return"accept

swift - "if not case"用于 Swift 中的枚举

这个问题在这里已经有了答案:complementeryofanifcase(1个回答)关闭6年前。有没有办法反转case.A=enumValue并使其类似于enumValue!=.A?enumMyEnum{caseAcaseBcaseCcaseD(something:OtherEnum)}letenumValue:MyEnum=...letotherValue:String=...ifcase.A=enumValue{//Donothing}else{ifotherValue=="something"{//Dostuff...}else{//Dootherstuff...}}//Mor