草庐IT

self-referencing

全部标签

Swift CFRunLoopTimerCreate - 如何在定时器回调中获取 "self"

在给定以下代码的情况下,如何访问类“self”实例以调用类实例方法。如果我尝试self.callSomeClassIntance(),如图所示,我会从编译器收到“无法从捕获上下文的闭包形成C函数指针”错误。我尝试使用info.callSomeClassInstance(),但这会给出“无成员callSomeClassInstance”错误。如果一行代码xxxx.callSomeClassIntance()被删除,代码将正确触发时间。importFoundationclassFoo{funccallSomeClassIntance(){}funcstart(){letrunLoop:CF

ios - self.navigationController?.pushViewController 不工作 Swift

我有UICollectionViewController,当我尝试单击单元格并导航到相应的ViewController时,它不起作用。我该如何解决这个问题。funccollectionView(_collectionView:UICollectionView,didSelectItemAtindexPath:IndexPath){letcell:AddOptionCollectionViewCell=collectionView.cellForItem(at:indexPath)as!AddOptionCollectionViewCellif(cell.name.text=="CONT

Swift 为嵌套回调保护弱 self

我的问题更像是对答案的更好练习。假设我们有多个嵌套的回调层,每一层我们都必须使self成为weak我知道我们可以为每一层编写guard(参见代码片段1),但这是否必要?如果我们只在第一层守卫就足够了吗(见代码片段2)?如果从引用计数的角度考虑,第一个strongself是否足够好?片段1:letcallBack1={[weakself]xxinguardletstrongSelf=self{return}//strongSelf.func(param)letcallBack2={[weakself]yyin{guardletstrongSelf=self{return}//strong

ios - Swift – self.navigationController 在过渡后变为 nil

我在我的应用程序中遇到了一个非常奇怪的错误,在两个View之间,self.navigationController正在变为nil我有几个ViewController:MainViewController、SecondViewControllerPastSessionsViewController、JournalViewController。我使用JournalViewController有两个目的,将新条目保存到CoreData或编辑旧条目。详细信息与此错误并不相关。当我尝试从堆栈中弹出JournalViewController并返回到MainViewController时发生错误,但

swift - 检查 UITableViewDelegate 是否等于 self

我怎样才能编译它?assert(self.tableView.delegate==self)其中self是一个包含tableview的UIViewController我明白了WjateverViewController.swift:56:44:Binaryoperator'=='cannotbeappliedtooperandsoftype'UITableViewDelegate?'and'WjateverViewController'你可能会认为断言(myTableView.delegate==selfasUITableViewDelegate?)会起作用。但它不会:OffersVi

ios - Swift:当我们对 Int 进行扩展时,属性 getter 中的 "self"应该是什么?

这似乎是一个错误并在Xcode7中用Swift(2b3)解决了为了方便观察,我们把代码贴到playground上,结果会在评论中指出。extensionInt{varsq:Int{mutatingget{self//5self=self*self//25returnself//25Here25ismade}}}varn:Int=5//5n.sq//25n//5Whyisn't25?我的问题是,如果属性sq的getter中的self指的是整数本身,为什么n在之后保留5>n.sq做了?让我们看看如果我们只添加一个setter会发生什么:extensionInt{varsq:Int{muta

ios - 返回一个 Self 类型的数组

我无法找到一种在运行时在Swift中返回特定动态类类型实例数组的方法。我成功地编译并测试了这个返回类的单个实例的版本:classGeneric{classfuncall()->Self{returnself.init()}requiredinit(){}}classA:Generic{}leta=A.all()//isoftypeA这里的挑战是编译允许all函数原型(prototype)如下:classfuncall()->[Self](即返回一个实例数组,使用子类,没有任何转换)。classGeneric{classfuncall()->[Self]{return[self.init

ios - `Protocol can only be used as a generic constraint because it has Self or associated type requirements` 问题

我正在尝试生成一个符合协议(protocol)Protocoling的ViewModel,该协议(protocol)是通用的,并且具有关联的类型。有几个ViewModel符合协议(protocol),所以我正在尝试为viewModel创建一个工厂。我在Swift中遇到了以下错误:Protocol只能用作泛型约束,因为它有Self或关联类型要求示例代码:protocolProtocoling{associatedtypemodulingTypevardata:modulingType{get}}enumMyTypes{casemyNamecasemyAddress}classNameVi

ios - 关闭键盘后如何恢复 UIInputViewController self.view?

我正在开发一个具有键盘扩展的应用程序,在键盘扩展的某些用途中(例如:当我使用切换键盘按钮时)扩展正在经历viewDidDisappear并在在这种情况下,我知道我可以清理键盘,它的所有View都将被解除分配,内存也将被释放。在其他用途​​中(例如:当使用Notes应用程序并按下Done/Back按钮时)扩展将通过viewWillDisappear而不是通过viewDidDisappear,所以在这个如果我没有清洁键盘,因为它没有达到我的清洁方法,因此它没有被释放。在这一点上,我希望当我通过回到笔记返回到键盘时,例如我会收到之前的UIInputViewController以及我已经构建的

swift - 闭包中的无主 self

如果我在另一个闭包中有一个闭包,是否足以在外部闭包中使用unowned/weakonce来避免保留循环?例子:foo.aClosure({[unownedself](allowed:Bool)inifallowed{self.doStuff()self.something.anotherClosure({(s:String)->(Void)inself.doSomethingElse(s)})}}) 最佳答案 如果您没有在外部闭包中创建对self的强引用(例如通过这样做:guardletstrongSelf=selfelse{返回}