草庐IT

self-referencing

全部标签

ios - self.view.subviews 在 iOS 8 中为空

我用xib创建了自定义UIViewController,现在我想从中获取第一个subview...它适用于iOS9,但在iOS8view.subviews上有0个ViewletcustomVC=CustomViewController()varcustomTestView=customVC.view.subviews.firstself.view应该有1个subview 最佳答案 如果你用xib创建ViewController,你应该使用它来初始化ViewControllerletvc=CustomViewController(ni

iphone - 过滤不是来 self 的应用程序的 HKSample 数据

在我当前的项目中,我需要将HealthKit样本与我的应用程序同步。我正在从HealthKit获取示例数据并将一些应用程序生成的示例写回HealthKit。为了获取,我使用了以下函数:-privatefuncreadHealthKitSample(sampleType:HKSampleType,limit:Int,startDate:NSDate,endDate:NSDate,completion:(([HKSample]?,NSError!)->Void)!){letmostRecentPredicate=HKQuery.predicateForSamplesWithStartDat

Swift 如何使用 'self' 作为泛型

我有课ManagedObject:NSManagedObject和一些子类MOProject:ManagedObjectMOOrganization:ManagedObject等等在ManagedObject中request(Router.Create(object:userInfo)).responseJSON(completionHandler:{(response)->Voidinswitchresponse.result{case.Success(letresult):Mapper().map(result)completion(result:Result(success:"d

swift - 在 Swift 中,是否有像 self 这样的关键字,但用于类?

classmySuperClass{staticvarsharedInstance=mySuperClass()vartest="hello"}在这个小片段中,我将静态变量设置为mySuperClass()以创建一个简单的单例。有没有办法不使用类名mySuperClass来做到这一点?我问,因为我想子类化这样的东西,并让子类创建一个它自己的sharedInstance,而不是它所做的父类(superclass)的。完整代码在这里(Playground可用):importCocoaclassmySuperClass{staticvarsharedInstance=mySuperClass

协议(protocol) : operator '===' cannot be applied to operands of type '_' and 'Self.T' 中的 Swift 泛型

我正在尝试使用Swift2构建一个简单的观察者混合。这里只是相关部分。protocolObservable{typealiasTvarobservers:[T]{getset}mutatingfuncremoveObserver(observer:T)}为了创建混入,我使用了一个扩展:extensionObservable{mutatingfuncremoveObserver(observer:T){letindex=self.observers.indexOf{$0===observer}iflet_=index{self.observers.removeAtIndex(index)

ios - 在我的 ViewController 中,如何将函数作为参数传递并保持 [weak self]?

我最初的方法。一切正常。:funcfetchInitialItems(num:Int){firebaseRef?.observeSingleEventOfType(.Value,withBlock:{[weakself]snapshotinifself!=nil{//performcalculations}})}我想将闭包移动到它自己的另一个方法中:funcfetchBlock(snapshot:FDataSnapshot!){//performcalculations}funcfetchInitialItems(num:Int){firebaseRef?.observeSingleE

ios - 在 Swift 中捕获 [weak self] 的 Realm 通知

在Swift的Realm文档中,关于notifications的部分有这个示例代码:classViewController:UITableViewController{varnotificationToken:NotificationToken?=niloverridefuncviewDidLoad(){super.viewDidLoad()letrealm=try!Realm()letresults=realm.objects(Person.self).filter("age>5")//ObserveResultsNotificationsnotificationToken=resu

swift - 在通用函数 where 子句中使用 Self 时出错

当我尝试将Self用作作为协议(protocol)一部分的通用函数中的where子句的一部分时,我遇到了一个问题。例如,假设我定义了这个协议(protocol)和这个通用函数:protocolAnimal{associatedtypeFoodSourcefunceat(_food:FoodSource)}//ThewhereclausespecifiesthatT2mustconformto//whatevertypeisT1'sFoodSourceassociatedtypefuncfeed(animal:T1,food:T2)whereT2==T1.FoodSource{anima

ios - 如果 "self"是 ViewController,你应该几乎总是使用 [weak self] 吗?

我一直在每个回调中使用[weakself],以防Controller展开并且“self”变为nil。这是正确的方法吗?当我应该使用[unownedself]或两者都不使用时,有哪些示例...假设“self”是一个会经常展开的ViewController? 最佳答案 如果当self被取消初始化时回调仍然会被调用,那么使用[weakself]是正确的。使用[unownedself]的原因是您知道在self消失后永远不会调用回调,但强烈捕获self会导致保留周期。[unownedself]的一个例子是闭包的存在依赖于self的存在,那么很

swift - 协议(protocol)中的 associated(typealias) 类型和 Self 如何工作?

对不起,我是编程新手,我尽量表达我想问的问题。请原谅我。我在协议(protocol)中看到过类似的东西。protocolPro1{typealiasElement//...}protocolPro2:Pro1{typealiasElement=Self//...}Element在协议(protocol)中,这个Element是否相互关联?我不明白下面的表达式是什么意思:typealiasElement=Self非常感谢。 最佳答案 Pro1写这个protocolPro1{typealiasElement}您只是告诉我们将有一个名为E