草庐IT

self-updating

全部标签

swift - 类名后的 .self 是做什么用的? swift 3

我最近注意到有些代码会初始化实例,例如ClassName.self():letrealm=try!Realm.self()从输出结果和之后的使用来看,就像没有它一样-Realm()使用.self进行初始化是否有任何特定原因或用法? 最佳答案 这有点猜测,但我相信确实在Realm的上下文中使用Type.self()的地方遵循了一个被误解的约定(一些无关紧要的)Swift团队解决bug的效果SR-899:.selfcanbeomittedifafunctionhasonlyoneparameterRealm团队的以下提交是为上述错误的预

swift - “Self”不能用于非平凡闭包

我想要一个带有静态初始化方法的类:classA{requiredinit(){}//thisoneworksclassfuncf0()->Self{returnself.init()}//thisoneworksaswellclassfuncf1()->Self{letcreate={self.init()}//noerror,inferredclosuretypeis'()->Self'returncreate()}}不幸的是,Swift3编译器无法为任何比{self.init()}更复杂的闭包推断类型。例如:classfuncf2()->Self{letcreate={//erro

swift - 使用从另一个协议(protocol)继承或使用 where Self 声明 swift 协议(protocol)时的区别

我仍然不明白使用继承声明Swift协议(protocol)时有什么区别:protocolSubProtocol:SuperProtocol{...}或使用whereSelfprotocolSubProtocolwhereSelf:SuperProtocol{...}通过以这两种方式执行此操作,结果完全相同,两个选项都可以正常编译,并且可以正常工作,SubProtocol将具有与SuperProtocol相同的内容。那有什么区别呢?我能看到的唯一区别是语义,一个比另一个更清楚(见下面的例子)。但这是我的观点,我想知道其他人是否也这么认为,或者我可能误解了整件事。示例:protocolPr

ios - 在 Swift 闭包中使用 [weak self] 和赋值

我一直在玩弄Swift并将AFNetworking集成到一个项目中,并且有一些我不是特别满意的代码。拥有alreadyposted在Apple开发论坛上没有得到回复,我想我会把它带到SO...我的类具有对AFHTTPSessionManager实例的强引用,以下代码段是调用GET(_,parameters,success,failure)时使用的闭包方法。letfailure={[weakself](task:NSURLSessionDataTask!,error:NSError!)->(Void)iniferror?.userInfo[AFNetworkingOperationFai

ios - #selector(Aclass.method) 和#selector(self.method) 的区别

这个问题在这里已经有了答案:HowdoIresolve"ambiguoususeof"compileerrorwithSwift#selectorsyntax?(3个答案)关闭6年前。有什么区别:#selector(Aclass.someMethod)和#selector(self.someMethod)someMethod是一个实例函数,我在AClass内部调用是这样的:NSNotificationCenter.defaultCenter().addObserver(self,selector:#selector(self.someMethod),//#selector(Aclass

swift - 非最终类无法满足协议(protocol)要求,因为它在非参数、非结果类型中使用 'Self'

这个问题在这里已经有了答案:ASwiftprotocolrequirementthatcanonlybesatisfiedbyusingafinalclass(4个答案)关闭6年前。我想用一个类方法实现一个协议(protocol),该类方法将实现类的数组作为参数。例如这样的事情:protocolMyProtocol{staticfuncfoo(verticies:[Self])->Int}classMyClass:MyProtocol{classfuncfoo(verticies:[MyClass])->Int{return42}}当我尝试这样做时,出现以下错误:Protocol'My

ios - 当我已经更新数据时出现错误 "Invalid update: invalid number of rows"

我的代码是这样的:functableView(_tableView:UITableView,commiteditingStyle:UITableViewCellEditingStyle,forRowAtindexPath:IndexPath){letIndexPaths=NSArray(array:[indexPath])letplistPath=NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0]asStringletpath=plistPath.appending("/Clas

ios - swift ios9 : Trying to start MapKit location updates without prompting for location authorization

我为mapViewsuingswift写了一个简单的例子,但我得到打印TryingtostartMapKitlocationupdateswithoutpromptingforlocationauthorization.必须先调用-[CLLocationManagerrequestWhenInUseAuthorization]或-[CLLocationManagerrequestAlwaysAuthorization]。我将mapView添加到viewController并开始定位。我还在startUpdatingLocation()之前调用了requestWhenInUseAutho

ios - .self 在 Swift 中的结构类型之后

我对Metal示例中的一行代码感到困惑,其中内存指针绑定(bind)到一个类型。uniforms=UnsafeMutableRawPointer(uniformBuffer.contents()).bindMemory(to:Uniforms.self,capacity:1)我的困惑是Uniforms类型之后的.self。Uniforms是在Objective-C文件中定义的结构,如果调用中没有.self,代码将无法运行。为什么有必要? 最佳答案 .self返回相应类型的元类型实例。将其视为类型安全的类型标识符(例如,比使用字符串更

ios - swift 3 : Realm creates additional object instead of updating the exisiting one

在我的AppDelegate中letrealm=try!Realm()print("numberofusers")print(realm.objects(User.self).count)if!realm.objects(User.self).isEmpty{ifrealm.objects(User.self).first!.isLogged{User.current.setFromRealm(user:realm.objects(User.self).first!)letstoryboard=UIStoryboard(name:"Main",bundle:nil)letviewCon