self

What does new self(); mean in PHP?

孤人 提交于 2019-11-27 05:55:12
I've never seen code like this: public static function getInstance() { if ( ! isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; } Is it the same as new className() ? EDIT If the class is inheritant,which class does it point to? Pascal MARTIN self points to the class in which it is written. So, if your getInstance method is in a class name MyClass , the following line : self::$_instance = new self(); Will do the same as : self::$_instance = new MyClass(); Edit : a couple more informations, after the comments. If you have two classes that extend each other, you

self.delegate = self; what's wrong in doing that?

风流意气都作罢 提交于 2019-11-27 04:53:29
self.delegate = self; what's wrong in doing that? and what is the correct way of doing it? Thanks, Nir. Code: (UITextField*)initWith:(id)sender:(float)X:(float)Y:(float)width:(float)hieght:(int)textFieldTag { if (self = [super initWithFrame:CGRectMake(X, Y,width, hieght)]) { finalText = [[NSMutableString alloc] initWithString:@""]; senderObject = sender; self.textColor = [UIColor blackColor]; self.font = [UIFont systemFontOfSize:17.0]; self.backgroundColor = [UIColor whiteColor]; self.autocorrectionType = UITextAutocorrectionTypeNo; self.keyboardType = UIKeyboardTypeDefault; self.returnKeyType

Is “self” weak within a method in ARC?

吃可爱长大的小学妹 提交于 2019-11-27 03:37:52
问题 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?

Attribute assignment to built-in object [duplicate]

大憨熊 提交于 2019-11-27 03:25:30
问题 This question already has answers here : Can't set attributes of object class (6 answers) Closed 4 years ago . This works: class MyClass(object): pass someinstance = MyClass() someinstance.myattribute = 42 print someinstance.myattribute >>> 42 But this doesn't: someinstance = object() someinstance.myattribute = 42 >>> AttributeError: 'object' object has no attribute 'myattribute' Why? I've got a feeling, that this is related to object being a built-in class, but I find this unsatisfactory,

How to get self into a Python method without explicitly accepting it

南笙酒味 提交于 2019-11-27 02:22:40
问题 I'm developing a documentation testing framework -- basically unit tests for PDFs. Tests are (decorated) methods of instances of classes defined by the framework, and these are located and instantiated at runtime and the methods are invoked to execute the tests. My goal is to cut down on the amount of quirky Python syntax that the people who will write tests need to be concerned about, as these people may or may not be Python programmers, or even very much programmers at all. So I would like

Assigning to self in Objective-C

余生颓废 提交于 2019-11-27 00:18:36
问题 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

When should I use the “self” keyword?

醉酒当歌 提交于 2019-11-26 23:09:50
问题 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 ? 回答1: 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

How to call an Objective-C Method from a C Method?

谁说我不能喝 提交于 2019-11-26 22:04:02
I have an Obj-C object with a bunch of methods inside of it. Sometimes a method needs to call another method inside the same object. I can't seem to figure out how to get a C method to call a Obj-C method... WORKS: Obj-C method calling an Obj-C method: [self objCMethod]; WORKS: Obj-C method calling a C method: cMethod(); DOESN'T WORK: C method calling an Obj-C method: [self objCMethod]; // <--- this does not work The last example causes the compiler spits out this error: error: 'self' undeclared (first use in this function) Two questions. Why can't the C function see the "self" variable even

iOS: Usage of self and underscore(_) with variable [duplicate]

风格不统一 提交于 2019-11-26 20:15:21
问题 Possible Duplicate: How does an underscore in front of a variable in a cocoa objective-c class work? I have been very confused with using self or underscore with variable name after synthesizing it like below: In .h file: @property(nonatomic, strong) NSMutableArray *users; In .m file: @synthesize users = _users; Based on my understandings when I use self.users , OS will make sure to release previously allocated memory in set method so we don't need to take care explicitly. _users is an

Calling [self methodName] from inside a block?

牧云@^-^@ 提交于 2019-11-26 18:52:40
问题 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.