self

Declaring self-referencing pointers in Swift [duplicate]

╄→гoц情女王★ 提交于 2019-12-19 09:18:21
问题 This question already has answers here : Adding observer for KVO without pointers using Swift (5 answers) Closed 4 years ago . What would be the equivalent in swift of this Obj-C code? I'm guessing something with CMutableVoidPointer static void *CapturingStillImageContext = &CapturingStillImageContext; 回答1: Probably something like this: var CapturingStillImageContext: COpaquePointer = nil withUnsafePointer(&CapturingStillImageContext) { CapturingStillImageContext = COpaquePointer($0) } 回答2:

Delphi self keyword

我只是一个虾纸丫 提交于 2019-12-18 15:35:37
问题 I am learning Delphi reading Marco Cantu's book and it's super complete. It's very clear but I have a doubt about the keyword self . I already have experience with OOP and I have the basics of it. My question is very simple. Can I compare the keyword self (Delphi) to the keyword this (Java)? When I read on the book about the self used inside record, I got in my mind something like self : Delphi = this : Java . Look at the code I created to make a test: type TMarioKart = packed record

Python assignment to self in constructor does not make object the same

天大地大妈咪最大 提交于 2019-12-18 05:12:41
问题 I am making a constructor in Python. When called with an existing object as its input, it should set the "new" object to that same object. Here is a 10 line demonstration: class A: def __init__(self, value): if isinstance(value, A): self = value else: self.attribute = value a = A(1) b = A(a)#a and b should be references to the same object print("b is a", b is a)#this should be true: the identities should be the same print("b == a", b == a)#this should be true: the values should be the same I

Python assigning two variables on one line

倖福魔咒の 提交于 2019-12-18 04:48:18
问题 class Domin(): def __init__(self , a, b) : self.a=a , self.b=b def where(self): print 'face : ' , self.a , "face : " ,self.b def value(self): print self.a + self.b d1=Domin(1 , 5) d1=Domin(20 , 15) I get this error: Traceback (most recent call last): File "test2.py", line 13, in <module> d1=Domin(1 , 5) File "test2.py", line 5, in __init__ self.a=a , self.b=b TypeError: 'int' object is not iterable 回答1: You cannot put two statements on one line like that. Your code is being evaluated like

Objective-C: When to call self.myObject vs just calling myObject

瘦欲@ 提交于 2019-12-17 19:00:29
问题 This little bit of syntax has been a bit of a confusion for me in Objective-C. When should I call self.myObject vs just calling myObject? It seems redundant however they are not interchangeable. Would someone please enlighten me? 回答1: If you're just accessing them, there's not much reason to use self.member . If you're doing assignment, it's good if you're doing more than the simple @property (assign) parameter--for instance, retain, copy, etc, so it can save on code you're writing. An

Swift Update Label (with HTML content) takes 1min

这一生的挚爱 提交于 2019-12-17 18:48:52
问题 I got a small problem, let me start with the code class ViewController: UIViewController { @IBOutlet weak var LBoutput: UILabel! @IBAction func BTclick(sender: AnyObject) { var url = NSURL(string: "http://google.com") println("test0") let getdata = NSURLSession.sharedSession().dataTaskWithURL(url){(data ,response , error) in var htmlContent = NSString(data: data, encoding: NSUTF8StringEncoding) println("test1") println("test2") self.LBoutput.text = "test6" } println("test3") getdata.resume()

What is the 'cls' variable used for in Python classes?

只谈情不闲聊 提交于 2019-12-17 05:16:19
问题 Why is cls sometimes used instead of self as an argument in Python classes? For example: class Person: def __init__(self, firstname, lastname): self.firstname = firstname self.lastname = lastname @classmethod def from_fullname(cls, fullname): cls.firstname, cls.lastname = fullname.split(' ', 1) 回答1: The distinction between "self" and "cls" is defined in PEP 8 . As Adrien said, this is not a mandatory. It's a coding style. PEP 8 says: Function and method arguments : Always use self for the

Instance variable: self vs @

喜你入骨 提交于 2019-12-17 04:14:19
问题 Here is some code: class Person def initialize(age) @age = age end def age @age end def age_difference_with(other_person) (self.age - other_person.age).abs end protected :age end What I want to know is the difference between using @age and self.age in age_difference_with method. 回答1: Writing @age directly accesses the instance variable @age . Writing self.age tells the object to send itself the message age , which will usually return the instance variable @age — but could do any number of

When to access properties with 'self'

这一生的挚爱 提交于 2019-12-17 03:43:29
问题 I have read a number of questions on this site about this issue, I understand the following: self.property accesses the getter/setter method created manually or by @synthesize. Depending upon whether the property is declared as retain, copy etc. the retain count is modified correctly, for example a retained property, releases the previous value assigned the new value with 'retain' and increments the retain count by 1. Properties are usually declared with instance variables of the same name

How to keep track of class instances?

心不动则不痛 提交于 2019-12-17 02:22:40
问题 Toward the end of a program I'm looking to load a specific variable from all the instances of a class into a dictionary. For example: class Foo(): __init__(self): x = {} foo1 = Foo() foo2 = Foo() foo...etc. Let's say the number of instances will vary and I want the x dict from each instance of Foo() loaded into a new dict. How would I do that? The examples I've seen in SO assume one already has the list of instances. 回答1: One way to keep track of instances is with a class variable: class A