declared-property

Access Objective-C property dynamically using the name of the property

ε祈祈猫儿з 提交于 2019-11-27 14:57:53
I know the string name of a property of an object. How would I go about getting and setting that property using the string? While @weichsel is correct, there is a better way. Use: [anObject valueForKey: @"propertyName"]; and [anObject setValue:value forKey:@"propertyName"]; Obviously, @"propertyName" can be an NSString that is dynamically composed at runtime. This technique is called Key Value Coding and is fundamental to Cocoa. Why this is better is because -valueForKey will do what is necessary to "box" whatever type the property returns into an object. Thus, if the property is of type int ,

myView.frame.origin.x = value; does not work - But why?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 11:41:00
I know that I can't use this: myView.frame.origin.x = 25.0; and that I have to use this instead: CGRect myFrame = myView.frame; myFrame.origin.x = 25.0; myView.frame = myFrame; And I'm doing it all the time, but I don't know why I must do it that way. I would like to fill that gap in my understanding. Can someone explain ? Nowadays Xcode gives you "Expression not assignable". Some time ago you got a compile error "Lvalue required as left operand of assignment". There are two distinct dot syntaxes being used here. They look the same, but they do different things depending on what they are

Should an NSString property under ARC be strong or copy?

此生再无相见时 提交于 2019-11-27 00:09:42
问题 When not compiling with ARC, it is recommended to use copy properties for data types such as NSString . I could not find proper documentation on the use of copy in ARC mode. Can someone tell me what's applicable for ARC? 回答1: It is still recommended to copy because you want to avoid something passing a mutable string and then changing it without you knowing. A copy guarantees that the string you have will not change. 回答2: Copying and ARC are orthogonal: you make copies of mutable objects to

Get property name as a string

﹥>﹥吖頭↗ 提交于 2019-11-26 22:08:43
I need a way to pass a property and get the name assigned to it. Any suggestions? @property (nonatomic, retain) MyObject *crazyObject; NSString *str = SOME_WAY_TO_GET_PROPERTY_NAME(crazyObject); // Above method should return @"crazyObject" You can try this: unsigned int propertyCount = 0; objc_property_t * properties = class_copyPropertyList([self class], &propertyCount); NSMutableArray * propertyNames = [NSMutableArray array]; for (unsigned int i = 0; i < propertyCount; ++i) { objc_property_t property = properties[i]; const char * name = property_getName(property); [propertyNames addObject:

Expose a private Objective-C method or property to subclasses

谁说胖子不能爱 提交于 2019-11-26 20:07:58
问题 According to some official talk, a class in Objective-C should only expose public methods and properties in its header: @interface MyClass : NSObject @property (nonatomic, strong) MyPublicObject *publicObject; - (void)publicMethod; @end and private methods/properties should be kept in class extension in .m file: @interface MyClass() @property (nonatomic, strong) MyPrivateObject *privateObject; - (void) privateMethod; @end and I don't think there is a protected type for things that are private

Difference between self.var and simply var

随声附和 提交于 2019-11-26 19:08:52
What is the difference between using self.var vs. just var in an Objective-C class? Are there benefits or dangers to one or the other? foo = self.var; self.var = foo; is conceptually identical to foo = [self var]; [self setVar: foo]; So using dot notation, you are really sending messages to self. foo = var; var = foo; is conceptually the same as foo = self->var; self->var = foo; So not using dot notation to access an instance variable is the same as treating self as a pointer to a C struct and accessing the struct fields directly. In almost all cases, it is preferable to use the property

Access Objective-C property dynamically using the name of the property

血红的双手。 提交于 2019-11-26 18:28:21
问题 I know the string name of a property of an object. How would I go about getting and setting that property using the string? 回答1: While @weichsel is correct, there is a better way. Use: [anObject valueForKey: @"propertyName"]; and [anObject setValue:value forKey:@"propertyName"]; Obviously, @"propertyName" can be an NSString that is dynamically composed at runtime. This technique is called Key Value Coding and is fundamental to Cocoa. Why this is better is because -valueForKey will do what is

self.variable and variable difference [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-26 13:47:46
This question already has an answer here: Difference between self.var and simply var 3 answers What is the difference between self.myVariable = obj; and myVariable = obj; , when I use @propery / @synthesize to create `myVariable? It's important to note that dot-syntax is converted to a simple objc_msgSend call by the compiler: that is to say that underneath it acts exactly like a message send to the accessor for that variable. As such, all three of the following are equivalent: self.myVariable = obj; [self setMyVariable:obj]; objc_msgSend(self, @selector(setMyVariable:), obj); Of course, this

self.variable and variable difference [duplicate]

馋奶兔 提交于 2019-11-26 08:51:19
问题 This question already has answers here : Difference between self.var and simply var (3 answers) Closed 5 years ago . What is the difference between self.myVariable = obj; and myVariable = obj; , when I use @propery / @synthesize to create `myVariable? 回答1: It's important to note that dot-syntax is converted to a simple objc_msgSend call by the compiler: that is to say that underneath it acts exactly like a message send to the accessor for that variable. As such, all three of the following are

Get property name as a string

随声附和 提交于 2019-11-26 08:10:53
问题 I need a way to pass a property and get the name assigned to it. Any suggestions? @property (nonatomic, retain) MyObject *crazyObject; NSString *str = SOME_WAY_TO_GET_PROPERTY_NAME(crazyObject); // Above method should return @\"crazyObject\" 回答1: You can try this: unsigned int propertyCount = 0; objc_property_t * properties = class_copyPropertyList([self class], &propertyCount); NSMutableArray * propertyNames = [NSMutableArray array]; for (unsigned int i = 0; i < propertyCount; ++i) { objc