declared-property

Why does Xcode automatically create variables with underscores?

拟墨画扇 提交于 2019-12-04 10:56:42
Why in the newest version of Xcode (dp-4) are variables declared with retain,nonatomic made to use the underscore before the variable name? Does this create some sort of type safety? For example, I create a property: @property (retain, nonatomic) IBOutlet UILabel *name; Unless I change the variable inside the dealloc to not have the _, I have to do: @synthesize name = _name; Why is this? Mark Dalrymple, who's way smarter than I am, wrote a blog post at Big Nerd Ranch about this very subject. Bottom line: the underscore is a good idea. I will summarize his post here just in case the link stops

Does an Objective-C readonly property need to specify strong or copy?

孤街浪徒 提交于 2019-12-04 01:01:46
问题 If I have a read-only string property, is it necessary to specify strong (or retain ) or copy in the declaration? If I don't specify, is one of them assumed? It seems to me the ownership attribute is only useful when you have a setter. @property (nonatomic, readonly) NSString *name; 回答1: That is mostly correct. For a readonly property, strong , retain , weak , and assign have no effect. But if you also declare the property elsewhere as readwrite (most frequently in an anonymous category in

Why does a declared property use both retain and readonly?

亡梦爱人 提交于 2019-12-03 03:47:48
问题 I've noticed that some of Apple's examples include both a retain and readonly modifier on properties. What's the point of including retain if no setter gets generated when we're using the readonly modifier? Example: @property (retain, readonly) NSString *title; from the AnimatedTableView sample. 回答1: You can include a second, private readwrite declaration in a class extension. The memory management scheme for all references needs to match IIRC, so you get silliness like "readonly, retain".

Does an Objective-C readonly property need to specify strong or copy?

不羁岁月 提交于 2019-12-01 03:45:45
If I have a read-only string property, is it necessary to specify strong (or retain ) or copy in the declaration? If I don't specify, is one of them assumed? It seems to me the ownership attribute is only useful when you have a setter. @property (nonatomic, readonly) NSString *name; That is mostly correct. For a readonly property, strong , retain , weak , and assign have no effect. But if you also declare the property elsewhere as readwrite (most frequently in an anonymous category in the .m ), then the other modifiers need to match. 来源: https://stackoverflow.com/questions/9405211/does-an

Assignment to ivar in a Block via weak pointer

≯℡__Kan透↙ 提交于 2019-11-30 09:52:06
I have a read-only property isFinished in my interface file: typedef void (^MyFinishedBlock)(BOOL success, NSError *e); @interface TMSyncBase : NSObject { BOOL isFinished_; } @property (nonatomic, readonly) BOOL isFinished; and I want to set it to YES in a block at some point later, without creating a retain cycle to self : - (void)doSomethingWithFinishedBlock:(MyFinishedBlock)theFinishedBlock { __weak MyClass *weakSelf = self; MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) { [weakSelf willChangeValueForKey:@"isFinished"]; weakSelf -> isFinished_ = YES; [weakSelf

Declare properties in .h interface or in an extension in .m file?

白昼怎懂夜的黑 提交于 2019-11-30 05:00:19
In Objective-C, is it best practice to: Declare objects such as buttons in the .h and then synthesize in the .m .h @interface SomeViewController : UIViewController @property (strong, nonatomic) UIButton *someButton; @end .m @implementation SomeViewController @synthesize someButton = _someButton; @end or declare them as ivars in the .m @interface SomeViewController () @property (strong, nonatomic) UIButton *someButton; @end I notice that in a lot of Apple code, specifically their Breadcrumbs sample code, many of their properties are declared in the interface. Is there a difference between the

Is there a difference between setting a property with the dot or the bracket syntax?

寵の児 提交于 2019-11-29 11:12:04
Given the property declaration below, does method (A) work in exactly the same way as method (B)? I just want to check that self.yellowViewController = yellcon_New; is going via my setter, so that the old objects gets released and the new one retained. // INTERFACE @property(nonatomic, retain) YellowViewController *yellowViewController; // IMPLEMENTATION (A) self.yellowViewController = yellcon_New; // IMPLEMENTATION (B) [self setYellowViewController:yellcon_New]; All of this is correct : self.yellowViewController = yellcon_New; And [self setYellowViewController:yellcon_New]; Work the same. I

Declare properties in .h interface or in an extension in .m file?

馋奶兔 提交于 2019-11-29 03:18:15
问题 In Objective-C, is it best practice to: Declare objects such as buttons in the .h and then synthesize in the .m .h @interface SomeViewController : UIViewController @property (strong, nonatomic) UIButton *someButton; @end .m @implementation SomeViewController @synthesize someButton = _someButton; @end or declare them as ivars in the .m @interface SomeViewController () @property (strong, nonatomic) UIButton *someButton; @end I notice that in a lot of Apple code, specifically their Breadcrumbs

Should an NSString property under ARC be strong or copy?

China☆狼群 提交于 2019-11-28 03:46:29
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? 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. Copying and ARC are orthogonal: you make copies of mutable objects to "freeze" their state; ARC keeps track of object's reference count. NSString objects may or may not be mutable.

Expose a private Objective-C method or property to subclasses

橙三吉。 提交于 2019-11-27 20:03:23
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 but accessible from subclasses. I wonder, is there anyway to achieve this, apart from declaring