草庐IT

self-assignment

全部标签

ios - Swift 函数范围 - 引用 self (意味着 "self as the enveloping function")

采用这个基本函数:funcsampleFunction(){print(self)}这里self指的是包含它的class实例,而不是-可能更有意义-包含它的function.如何获取对封装函数而不是其封装类实例的引用?理想情况下,我希望做这样的事情:funcsampleFunction(value:Int){print(selfAsEnvelopingSampleFunction,value)} 最佳答案 如果要打印函数名,可以使用__FUNCTION__print(__FUNCTION__)在您的print语句中,否则只需使用sa

ios - 自定义 UIView 初始化程序的便利初始化程序必须使用 self.init 错误进行委托(delegate)

我正在尝试为UIView创建自定义初始化方法,如下所示:convenienceinit(frame:CGRect,tutorProfileImageURL:String?){self.tutorProfileImageURL=tutorProfileImageURLsuper.init(frame:frame)}overrideinit(frame:CGRect){super.init(frame:frame)setup()}requiredinit?(coderaDecoder:NSCoder){super.init(coder:aDecoder)setup()}funcsetup(

ios - 错误 : Assigning non-escaping parameter 'publicationQuery' to an @escaping closure

我有一个像这样的ViewController:classPublicationListViewController:UIViewController{varpublicationQuery:(()->[Publication])!funcinitWith(title:String,publicationQuery:()->[Publication]){self.title=titleself.publicationQuery=publicationQuery}}为什么我会收到“将非转义参数‘publicationQuery’分配给@escaping闭包”错误?

swift - 如何实现返回协变 Selfs 的协议(protocol)方法?

错误:非最终类(“Class”)无法满足协议(protocol)“Protocol”要求“instance”,因为它在非参数、非结果类型位置使用“Self”protocolProtocol{varinstance:Self{get}}classClass:Protocol{varinstance:Class{returnSubclass()}}classSubclass:Class{}以下是我在C#中表达我想要的内容的方式。(据我所知,C#没有办法强制通用参数“Self”实际上是我们从Swift中知道的Self,但它作为文档足以让我做正确的事情。)interfaceProtocolwh

swift 4 : Cannot assign value of type '(_) -> Void' to type '(() -> ())?'

XCode9,Beta3。Swift4。letbutton=JumpingButton(x:0,y:50,w:150,h:300)//JumpingButton:UIButton//InsideJumpingButton://varclickAction:(()->())?button.clickAction={(sender)->Voidin//Errorlineaction()Sound.playSound(Sounds.Button)}出现错误:无法将类型“(_)->Void”的值分配给类型“(()->())?” 最佳答案 因

Swift - didSet 中的弱 self

我很少看到有人在didSet中使用[weakself]。这是有原因的吗?我尝试在变量的didSet中使用[weakself]:vardata:Dictionary!{//[1]didSet{[2]self?.layoutSubviews()}}无论我将[weakself]in放在[1]还是[2],我仍然得到错误:Useofunresolvedidentifierweak这是为什么呢?将[weakself]用于didSet是否违法?问候, 最佳答案 didSet不是闭包,您不能对它使用闭包语法。没有理由在那里使用weakself。di

Swift - 在关闭时强烈捕获 self 后期待泄漏

谁能解释一下为什么这不会泄漏?我在closure中捕获self所以我会有两个指向彼此的强指针,因此deinit消息应该'永远不会为Person对象调用。首先,这是我的类(class)Person:classPerson{varname:Stringinit(name:String){self.name=name}deinit{print("\(name)isbeingdeinitialized")}}这是我的ViewController的实现:classViewController:UIViewController{varjohn:Person?funccallClosureFunct

ios - 如何使用其中的 UIStackView 进行 self 调整 UITableViewCell

我无法通过其中的UIStackView实现self调整大小的UITableViewCell。对于UITableViewCell我正在做这样的事情:在代码上我这样做:overridefuncviewDidLoad(){super.viewDidLoad()self.tableView.estimatedRowHeight=80}但是当我运行应用程序时,UITableViewCell不会调整大小:在UITableViewCell中使用UIStackView进行self调整时,我错过了什么? 最佳答案 单元格大小调整工作正常。您的问题是您

ios - self.variable 和 _variable 的区别,关于 KVO

这个问题在这里已经有了答案:What'sthedifferencebetween_variable&self.variableinObjective-C?[duplicate](1个回答)关闭7年前。第一张图是用self.name改的,第二张图是用_name改的,应该是一样的结果,但是第二张什么都不输出,为什么?这是代码#import"ViewController.h"@interfacekvo:NSObject@property(nonatomic,strong)NSString*name;@end@implementationkvo-(void)change{_name=@"b";

swift - SpriteKit : why node in collision has category bit mask of 4294967295 when this category was never assigned to a node

在下面的didBegin函数中,其中一个节点的类别位掩码为4294967295。但是,此类别从未分配给任何节点。这里是所有使用的位掩码:structPhysicsCategory{staticletNone:UInt32=0staticletAll:UInt32=UInt32.maxstaticletPlayer:UInt32=0b1//1staticletWorldBorder:UInt32=0b10//2staticletTopWorldBorder:UInt32=0b100//4staticletRightWorldBorder:UInt32=0b1000//8staticlet