草庐IT

variable_scope

全部标签

swift - 已经声明的变量显示 'variable used before declaration'

我“修复”了之前的一个错误,但这样做最终导致它在我明确声明时显示“在声明之前使用了变量“答案””。代码有什么问题?ifoperation.text=="/"{identifyVal()varanswer:Float=0.0//declaredthevalueofansweranswer=round(Float(randomNumber)/Float(randomNumber2))}varanswer:UInt32ifoperation.text=="+"{answer=randomNumber+randomNumber2//nothingwrong}ifoperation.text==

ios - Swift 4 : Variable 'value' inferred to have type 'Void' , 这可能是意想不到的?

我正在尝试将存储在firebase存储中的图像的下载URL保存在数据库中。我看过here,和here,但是我收到以下错误:Cannotconvertvalueoftype'Void'totype'[String:String]'incoercion这发生在下面指示的行上:storageRef.downloadURL{(url,error)inguardletdownloadURL=urlelse{//Uh-oh,anerroroccurred!return}//databaseintegrationletref=Database.database().reference()letuse

swift 3 : Programmatically functioning scope bar

我已经以编程方式为我的TableView设置了一个搜索栏,并向搜索栏添加了一个范围栏。我试图弄清楚当用户点击范围按钮之一时如何显示正确的信息。例如,如果他们点击“甜食”范围按钮,它只会显示数据模型中与甜食相关的所有水果。到目前为止,这是我的代码:classViewController:UIViewController,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating{@IBOutletweakvarmyTable:UITableView!varfruits=[Fru

swift 延迟特殊性 : why is code in case:defer is invoked before the end of the switch scope?

case.foo:defer{baz()}fallthroughcase.bar:baz()在我们进入酒吧场景之前已经到达。这是预期的还是编译器bork?我期待baz()在切换结束时被调用作用域还是函数作用域???!? 最佳答案 case:block的结尾是该block范围的结尾...fallthrough语句不维护范围。考虑以下几点:defer{print("outerdeferred")}lett=1switcht{case0:print("0")case1:print("1")defer{print("deferred")}f

swift - 错误 : Variable with getter/setter cannot have an initial value

如何修复此错误?Variablewithgetter/settercannothaveaninitialvalue这是我的代码:functableView(tableView:UITableView,cellForRowAtIndexPathindexPath:NSIndexPath)->UITableViewCell{varcell:UITableViewCell=tableview.dequeueReusableCellWithIdentifier("cell")asUITableViewCell{//Errorcell.textLabel?.text=self.items[ind

ios - 当我尝试在函数前添加 private 时,Xcode 报错 "attribute private can only be used in a non local scope"

当我尝试在函数前面添加private时,xcode提示“属性private只能在非本地范围内使用”。我认为“私有(private)”应该用于您想要保留本地权利的事情?有人可以告诉我如何处理错误消息吗?我仍然想将函数保密。 最佳答案 我是通过搜索这个错误attributeprivatecanonlybeusedinanonlocalscope到这里的。在我的例子中,这是由switch语句末尾缺少右括号引起的。希望这对某人有帮助。 关于ios-当我尝试在函数前添加private时,Xcode

variables - 通过引用访问变量

我正在尝试找出在Swift中引用简单类型的基本方法。在C中,这不是问题:inta=42;int*refA=&a;*refA=43;//Atthispoint,ais43在Swift中,我似乎不能这样做。vara:Int=42println(a)//varaRef:Int=&a//Nope.//varaRef:Int&=&a//Nah.//inoutvar:IntaRef=&a//Nyet//varinout:IntaRef=&a//Non//varaRef:Int*=&a//Whatareyou,stupid,orstubborn?////aRef=43//Ifanyoftheabov

ios - 删除 NotificationCenter 的观察者 - "Variable used within its own initial value"

我不明白如何使用block删除通知的观察者。varblock=NotificationCenter.default.addObserver(forName:.notifName,object:obj,queue:OperationQueue.current,using:{notificationinNotificationCenter.default.removeObserver(block)//Dostuff})这会出现编译器错误“变量在其自身的初始值内使用”。我怎样才能删除这个观察者? 最佳答案 编译器提示是因为它“不知道”闭包

ios - swift 3 : How to assign variable of different option types without multiple if let statements?

首先,如果标题含糊不清,我深表歉意。请随意更改它以符合问题描述。我会尽力描述我的问题,但首先这里有一段代码:ifletvc=currentVCas?FirstViewController{vc.doSameMethod()}elseifletvc=currentVCas?SecondViewController{vc.doSameMethod()}elseifletvc=currentVCas?ThirdViewController{vc.doSameMethod()}基本上,我使用iflet语句检查可选的nil,然后解包并赋值。我在所有3个ViewController中都有doSam

swift WKWebView : Can't find variable error when calling method

我正在尝试将GPS坐标传递给方法调用setIOSNativeAppLocation。我在下面有以下代码,但出现此错误:AJavaScriptexceptionoccurred"UserInfo={WKJavaScriptExceptionLineNumber=1,WKJavaScriptExceptionMessage=ReferenceError:Can'tfindvariable:setIOSNativeAppLocation,WKJavaScriptExceptionSourceURL=http://mywebsite.com,NSLocalizedDescription=AJa