草庐IT

ios - 检查 `if let` 是否为零

我有一个应用程序,我目前正在使用SwiftKeychainWrapper.下面是我检查retrievedString是否为nil的代码。但是,我仍然在控制台中收到retrievedString:nil。if-let语句中的代码不应该运行,还是我对if-let的使用/理解不正确?对于给定的示例,使用if-let解包我的可选值的正确方法是什么?ifletretrievedString:String?=KeychainWrapper.stringForKey("username"){print("retrievedString:\(retrievedString)")//valuenotni

arrays - Array.reduce 无法通过下标赋值 : 'x' is a 'let' constant

我正在尝试对数组使用reduce函数:varfields=["gender","name","nickname"]varstats=fields.reduce([String:String]()){(res,field)->[String:String]inres[field]=json[field]??""returnres}它只是遍历fields数组并将值从json对象分配给res。对我来说似乎很简单,但编译器一直在说:error:cannotassignthroughsubscript:'res'isa'let'constant在线:res[field]=json[field]?

swift - let 如何评估 Swift 中的 bool 值?

我正在阅读SwiftProgrammingLanguage书籍,让您像许多其他人一样熟悉Swift语言。上面写着Inanifstatement,theconditionalmustbeaBooleanexpression—thismeansthatcodesuchasifscore{...}isanerror,notanimplicitcomparisontozero.据我所知,条件必须评估bool值。即使它是一个整数值,它也不起作用。但是我不明白的是ifletconvertedRank=Rank.fromRaw(3){letthreeDescription=convertedRank

swift - Swift 可选链接总是通过 if let 构造完成,还是只是使用带可选的问号完成?

根据Apple文档,可选链接如下:Youspecifyoptionalchainingbyplacingaquestionmark(?)aftertheoptionalvalueonwhichyouwishtocallaproperty,methodorsubscriptiftheoptionalisnon-nil....optionalchainingfailsgracefullywhentheoptionalisnil...我对此的解释是,如下所示的构造是可选链接:someMasterObject.possiblyNilHandler?.handleTheSituation()..

Swift:不能在 if let 语句条件中使用数组过滤器

假设我有一个用户名数组letusers=["Hello","1212","12","Bob","Rob"]我想得到名字长度为2的第一个用户,所以我过滤了数组,得到了第一个用户ifletselected=users.filter{$0.characters.count==2}.first{print(selected)}此代码在swift2.2下抛出编译错误一行中的连续语句必须用';'分隔但是,这虽然工作正常letselected=users.filter{$0.characters.count==2}.firstifletselected=selected{print(selected

swift - Swift 中的 var 与 let

这个问题在这里已经有了答案:Whatisthedifferencebetween`let`and`var`inSwift?(32个答案)关闭8年前。我是Swift编程的新手,我遇到过var和let类型。我知道let是一个常量,我知道那是什么意思,但我从来没有使用常量,主要是因为我不需要。那么为什么要用var而不是let,应该在什么情况下使用呢?

ios - guard let in swift 2.0 playground 获取关于可选绑定(bind)的错误...为什么?

我在看thisvideo.在9:40左右,屏幕上出现了一些示例代码,如下所示://SieveofEratosthenes,asseeninWWDC2015funcprimes(n:Int)->[Int]{varnumbers=[Int](2..0else{continue}formultipleinstride(from:2*prime-2,to:n-2,by:prime){numbers[multiple]=0print("\"numbers[i]")}}returnnumbers.filter{$0>0}}当我将其输入XcodePlayground时,出现以下错误:Initiali

swift - Realm :Swift `let` 属性不能标记为动态

我正在使用Xcode7.2、Swift2.1.1。我下面有一个Realm模型对象classB:Object{dynamicletlists=List()}但是Swift编译器给我一个错误提示:PropertycannotbemarkedasdynamicbecauseitstypecannotberepresentedinObjective-C我看到Realm的文档说:Realmmodelpropertiesneedthedynamicvarattributeinorderforthesepropertiestobecomeaccessorsfortheunderlyingdataba

swift - 为什么我要同时使用 if 和 let,而不是仅仅检查原始变量是否为 nil? ( swift )

在“TheSwiftProgrammingLanguage.”在书中,Apple提到在访问可选变量时一起使用if和let。书上给出了如下代码示例:varoptionalString:String?="Hello"optionalString==nilvaroptionalName:String?="JohnAppleseed"vargreeting="Hello!"ifletname=optionalName{greeting="Hello,\(name)"}使用ifletname=optionalName而不是ifoptionalName!=nil有什么好处(并且总是将其称为opti

xcode - 有没有更好的方法来处理 Swift 的嵌套 "if let" "pyramid of doom?"

有没有比嵌套的iflet语句更好的处理可选属性链的方法?我被建议在检查可选属性时使用iflets,这是有道理的,因为它在编译时而不是运行时处理它们,但它看起来完全疯狂!有没有更好的办法?这是我最终得到的当前“厄运金字塔”,例如:(users:[JSONValue]?)inifletjsonValue:JSONValue=users?[0]{ifletjson:Dictionary=jsonValue.object{ifletuserIDValue:JSONValue=json["id"]{letuserID:String=String(Int(userIDValue.double!))