草庐IT

self-referencing

全部标签

swift - “self”在所有成员初始化之前被闭包捕获

好吧,下面是我的代码:importUIKitimportForecastIOclassWeather{vartemp:Floatvarcondition:Stringvarwind:Floatvarprecip:Floatinit(){DarkSkyClient(apiKey:"").getForecast(latitude:Utils().getLat(),longitude:Utils().getLong()){resultinswitchresult{case.success(letcurrentForecast,_):self.temp=(currentForecast.cur

swift - 内部闭包的捕获列表是否需要将 `self` 重新声明为 `weak` 或 `unowned`?

如果我有一个闭包传递给这样的函数:someFunctionWithTrailingClosure{[weakself]inanotherFunctionWithTrailingClosure{[weakself]inself?.doSomething()}}如果我在someFunctionWithTrailingClosure的捕获列表中将self声明为[weakself]而没有在捕获列表中再次将其重新声明为weakanotherFunctionWithTrailingClosureself已经变成了Optional类型,但它是否也变成了weak引用?谢谢!

ios - 在 Swift 1.2 中将 self 作为参数传递给 init 方法

以下类有一个“let”属性声明为隐式解包变量。这以前适用于Xcode6.2:classSubView:UIView{letpandGestureRecognizer:UIPanGestureRecognizer!requiredinit(coderaDecoder:NSCoder){super.init(coder:aDecoder)self.pandGestureRecognizer=UIPanGestureRecognizer(target:self,action:"panAction:")}funcpanAction(gesture:UIPanGestureRecognizer)

swift - 在 Swift 的类扩展函数中使用 'self'

我希望能够从Nib中提取UIView子类的实例。我希望能够调用MyCustomView.instantiateFromNib()并拥有MyCustomView的实例。我几乎准备好通过桥接头移植我拥有的工作Objective-C代码,但我想我会先尝试惯用的方法。那是两个小时前。extensionUIView{classfuncinstantiateFromNib()->Self?{lettopLevelObjects=NSBundle.mainBundle().loadNibNamed("CustomViews",owner:nil,options:nil)fortopLevelObje

ios - 如何在一个方法中重新加载viewDidLoad,而不是再次调用如[self viewDidLoad];

我想在方法中重新加载ViewdidLoad但现在想再次调用[selfviewDidLoad];这可能吗? 最佳答案 不是调用viewDidLoad:而是创建另一个方法(newMethod)并将其中需要调用的所有代码从-(void)viewDidLoad{[superviewDidLoad];[selfnewMethod];}然后从您要调用viewDidLoad:的代码中调用[selfnewMethod];快速版本funcviewDidLoad(){super.viewDidLoad()self.newMethod()}然后从您要调用

Swift: Overriding Self-requirement 是允许的,但会导致运行时错误。为什么?

我刚开始学习Swift(v.2.x),因为我很好奇新功能是如何发挥作用的,尤其是带有self要求的协议(protocol)。下面的示例将编译得很好,但会导致任意运行时影响的发生://TheprotocolwithSelfrequirementprotocolNarcissistic{funcgetFriend()->Self}//BaseclassthatadoptstheprotocolclassMario:Narcissistic{funcgetFriend()->Self{print("Mario.getFriend()")returnself;}}//Intermediatec

swift - 当我在 ubuntu 16.04 中使用 self swift2 时,无法将类型 'String' 的值转换为强制类型 'NSString'

我对抛出下一条消息的代码有疑问:error:cannotconvertvalueoftype'String'totype'NSString'incoercionreturn(selfasNSString).substringWithRange(range)我之前可以解决它,但不能通过self调用,所以这里是代码:letrange=expression.rangeOfFirstMatchInString(self,options:[],range:NSMakeRange(0,self.utf16.count))ifrange.location!=NSNotFound{return(sel

swift - Xcode lldb 错误 : can't print out Swift variable - get "$__lldb_injected_self.$__lldb_wrapped_expr_x" instead

有时,当试图在调试器中打印出一个变量时,会显示以下错误消息:error:warning::12:9:warning:initializationofvariable'$__lldb_error_result'wasneverused;considerreplacingwithassignmentto'_'orremovingitvar$__lldb_error_result=__lldb_tmp_error~~~~^~~~~~~~~~~~~~~~~~~~_error::18:5:error:useofunresolvedidentifier'$__lldb_injected_self'

Swift set delegate to self 给出 EXC_BAD_ACCESS

我正在通过移植现有应用程序来学习Swift。我坚持设置委托(delegate),无法解决问题所在。我有一个扩展UITableViewCell的类importUIKitprotocolSwitchCellDelegate{funcswitchChanged(switchCell:SwitchCell,state:Bool)}classSwitchCell:UITableViewCell{@IBOutletvarswtSelector:UISwitch@IBOutletvarlblTitle:UILabelvardelegate:SwitchCellDelegate?init(style:

ios - 在 Swift 中返回 self

目标:制作一个通用的ViewController和TableViewController,它们能够从现有的Storyboard中返回自己,并且可以被其他ViewController子类化并允许它们使用此功能。classGenericTableViewController:UITableViewController{//MARK:StoryboardclassfuncstoryboardName()->String{return""}classfuncstoryboardIdentifier()->String{return""}classfuncexistingStoryboardCo