self

Why does assigning to self not work, and how to work around the issue?

感情迁移 提交于 2019-11-28 13:57:11
I have a class (list of dict s) and I want it to sort itself: class Table(list): … def sort (self, in_col_name): self = Table(sorted(self, key=lambda x: x[in_col_name])) but it doesn't work at all. Why? How to avoid it? Except for sorting it externally, like: new_table = Table(sorted(old_table, key=lambda x: x['col_name']) Isn't it possible to manipulate the object itself? It's more meaningful to have: class Table(list): pass than: class Table(object): l = [] … def sort (self, in_col_name): self.l = sorted(self.l, key=lambda x: x[in_col_name]) which, I think, works. And in general, isn't there

Is “self” weak within a method in ARC?

 ̄綄美尐妖づ 提交于 2019-11-28 10:20:48
I have a method that occasionally crashes. -(void)foo{ [self doSomething]; [self.delegate didFinish]; [self doSomethingElse]; } -doSomething works correctly, then I call to a delegate -didFinish. Within -didFinish, the reference to this object might be set to nil, releasing it under ARC. When the method crashes, it does so on -doSomethingElse. My assumption was that self would be strong within a method, allowing the function to complete. Is self weak or strong? Is there documentation on this? What would the reasoning be for it being strong or weak? Edit Upon being inspired by some of the

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

那年仲夏 提交于 2019-11-28 09:28:40
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? 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 example: myObject = anotherObject; self.myObject = anotherObject; The second choice will ensure that you're

Swift Update Label (with HTML content) takes 1min

馋奶兔 提交于 2019-11-28 08:41:46
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() println("test4") LBoutput.text = "test5" } This codes give me a output in the console of test0 test3

Assigning to self in Objective-C

戏子无情 提交于 2019-11-28 04:09:10
I'm from the C++ world so the notion of assigning this makes me shudder: this = new Object; // Gah! But in Objective-C there is a similar keyword, self , for which this is perfectly acceptable: self = [super init]; // wait, what? A lot of sample Objective-C code uses the above line in init routines. My questions: 1) Why does assignment to self make sense (answers like "because the language allows it" don't count) 2) What happens if I don't assign self in my init routine? Am I putting my instance in some kind of jeopardy? 3) When the following if statement fails, what does it mean and what

“Invalid use of 'this' in non-member function” in objective-c context?

拜拜、爱过 提交于 2019-11-28 02:01:17
问题 Using Xcode. In this code (func is declared in interface), tells subj error, standing on string with 'self'. + (void) run: (Action) action after: (int) seconds { [self run:action after:seconds repeat:NO]; } What the... ? 回答1: self is an instance variable used to refer to an instance of the current object. You are attempting to use it in a class level method +(void)... where self has no meaning. Try using a shared instance, or passing an instance of the class in question to the method. + (void

Why do I get the error “Protocol … can only be used as a generic constraint because it has Self or associated type requirements”?

荒凉一梦 提交于 2019-11-28 01:36:26
问题 I wrote an extension onto Int as below. extension Int { func squared () -> Int { return self * self } } print(10.squared()) // works The above code works. Now I want to extend the IntegerType protocol so that Int, UInt, Int64, etc would all conform. My code is as below. extension IntegerType { func squared () -> IntegerType { // this line creates error return self * self } } I get error: Protocol 'IntegerType' can only be used as a generic constraint because it has Self or associated type

When should I use the “self” keyword?

早过忘川 提交于 2019-11-27 22:26:53
When should I be using the self expression in my iphone development applications? say i have 2 fields: UITextField *text1; and NSString *str1; retained and synthesized. when i am accessing either of these 2 fields, when should i and when should i not use self.text1 and self.str1 ? There are certain circumstances where it's generally discouraged to use the self. -expression to access a property. Normally you always use self for any access of a property. It's the most secure and uncomplicated way. Especially if you used retain, then memory management will be done for you. The two exceptions from

What does “self” mean in javascript?

戏子无情 提交于 2019-11-27 19:23:40
I read here that " self Refers to the current window or form ". Self does not seem to refer to the current form in this case: <form><input type="text" onkeyup="alert(self.foo.value)" name="foo"></form> However in this case it works (referring to the window): <form><input type="text" onkeyup="alert(self.document.forms[0].foo.value)" name="foo"></form> So when would you use the self DOM property over just window ? Swift For all windows, the self and window properties of a window object are synonyms for the current window, and you can optionally use them to refer to the current window. For

Calling [self methodName] from inside a block?

妖精的绣舞 提交于 2019-11-27 17:07:45
I've just run into blocks and I think they are just what I'm looking for, except for one thing: is it possible to call a method [self methodName] from within a block? This is what I'm trying to do: -(void)someFunction{ Fader* fader = [[Fader alloc]init]; void (^tempFunction)(void) = ^ { [self changeWindow:game]; //changeWindow function is located in superclass }; [fader setFunction:tempFunction]; } I've been searching for a couple of days and I can't find any evidence that this is possible. Is this at all possible, or am I trying to use blocks for something they aren't meant for? The reason I