Xcode generates some remove methods for my property, what are they? - ios

When I define a property like:
#property (nonatomic, strong) NSObject *myTestObject;
Xcode would automatically generate 4 methods, all begins with "remove":
So what is this mechanism, and how to use it?

Related

Different methods for IBOutlet creation

There are at least 3 methods of creating an IBOutlet in Objective-C, for making iOS 10 App, in Xcode 8.
Method 1: in ViewController.h
#interface ViewController : UIViewController
#property (nonatomic, strong) UILabel *textLabel;
#end
Method 2: in the interface of ViewController.m
#interface ViewController () {
IBOutlet UILabel *textLabel;
}
#end
Method 3: in the interface of ViewController.m, using #property
#interface ViewController ()
#property (nonatomic, strong) UILabel *textLabel;
#end
Given that the textLabel has to be accessed & its text is needed to be updated frequently, which method is the correct way to do so?
That all depends on whether you need your outlet to be accessible to classes outside of the containing one; generally I would discourage this because it is good practice to keep your view controllers responsible for updating your UI and not pass this task around to other classes. With this being said, Method 3 would be the best option, however, if you do have to access your object from another class, then simply use Method 1 so it is exposed in your class header.
Method 2 utilises iVars rather than object properties and is not the proper way to declare outlets, it may even cause unexpected behaviour so it is best to avoid this method.
Your code contains no proper IBOutlet. Outlets are connections to Storyboard.
Method 1
This is a property. As it is in .h file, it can be reached from outside. The Objective-C pattern for public.
Method 2
This is an iVar. Do not use iVars if you do not have to.
Method 3
This is a property. As it is in .m file, it can not be reached from outside. The Objective-C pattern for private.
Method 4
A proper IBOutlet looks like this:
#interface ViewController ()
#property (nonatomic, weak) IBOutlet UILabel *label;
#end
It is a simple property. You have to decide if you put it in .h or .m file depending on whether or not you want to publish it.
The IBOutlet simply makes the property connect-able to Storyboard. It's an annotation for Xcode and does not alter the semantic of your code.
Edit 1:
As Sulthan correctly mentions in the comments:
In most situations the correct design pattern is to hide outlets because it's an implementation detail. External classes should not set data directly using views.
Edit 2:
Why "not to use iVars if you do not have to" (2)
Opinion based:
I consider it as good OOP practice to use getters & setters (and thus not to access the variables directly). Also code is easier to read as you know while reading what x = self.variable (property) and x = variable (local variable) are.
If you have to use iVars for some reason, it is common to (and I would recommend to) prefix the name with _. x = _variable (iVar).

What is the default property of a #property in objective-c? [duplicate]

This question already has answers here:
What are the default attributes for Objective-C properties?
(3 answers)
Closed 7 years ago.
A #property can be set as strong, weak , assign , copy ... like
#property (copy, nonatomic) NSString *string;
#property (strong ,nonatomic) CustomClass *object;
#property (weak,nonatomic) id <CustomDelegate>delegate;
However, if
#property id <CustomDelegate>delegate; weak?strong?
#property (copy, nonatomic) NSString *string; strong?
If (weak,nonatomic) is abbreviated. What is the default value of an id? And other?
Properties Are Atomic by Default
This means that the synthesized accessors ensure that a value is always fully retrieved by the getter method or fully set via the setter method, even if the accessors are called simultaneously from different threads.
Because the internal implementation and synchronization of atomic accessor methods is private, it’s not possible to combine a synthesized accessor with an accessor method that you implement yourself. You’ll get a compiler warning if you try, for example, to provide a custom setter for an atomic, readwrite property but leave the compiler to synthesize the getter.
For more detail you can read this article https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html
Thanks
id is a reference to some random Objective-C object of unknown class, thus it's default attributes are:
#property (atomic, readwrite, strong) id value;
Note: delegates 99.999% of the time should be weak.

using #property or #interface in header

What is the right method of declaration if I want to use single object in my viewcontroller?
to use a #property i my .m file
#property (nonatomic, strong) UITextView *resolutionText;
#property (nonatomic, strong) AWLResolutionView *myView;
or to declare them in my .h file
#interface
{
#private
UITextView *_resolutionText;
AWLResolutionView *myView;
}
For the sake of clean coding I would prefer creating properties in the anonymous category inside the .m file.
However, using #property creates automatically an instance variable for you that has the same name as your property preceded by an underscore (_), that can be accessed from within the .m file. This is called synthesising.
Y
ou can also manually synthesize a property to a custom instance variable using #synthesize.
Apple provided some clear instructions how to write clean code in their developer library.
The best way to declare the private variable should be declared as #property in the extension of .m file . If you see in your .m file there is an extension class called as #interface by default, so declared the same in the extension class. Also no need of writing extra code in.h file for declaring the private variable.
So your first approach is best.

iOS when should #property be in .h and when in .m

I understand that methods available for other classes to call should be in the header file..but I'm a little confused when the #property should be in the header file and when it should be in the implementation file.
How do you make that decision, and what difference does it make?
Any property that you want publicly exposed to the other classes goes in the .h file. The 'private' properties (pun intended) go in the implementation file in a anonymous category or class extension. You might also make the .h version of the property readonly for example, and the .m version readwrite.
An implementation file with properties defined in a Private Category
#interface CPClassFileName ()
#property (nonatomic, retain) NSString *string;
#end
#implementation
...
#end
You put the #property on the header if you want other classes (or developers) to know that there are accessors to your ivars.
To rephrase :
If you want "public" getter / setter you put the #property on the header. If you want them private you put the #property on the .m file.

Should I continue to use iVar and #property (nonatomic, retain) plus #synthesize under Automatic Reference Counting (ARC)?

Like a doop I'd been declaring Instant Variables (iVar) and then #property in the interface .h file for a while now.
#interface MainGameViewController : UIViewController {
UserFactorsViewController *userFactorsViewController;
UITableView *myTableView;
}
#property (nonatomic, retain) UserFactorsViewController *userFactorsViewController;
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
Under Automatic Reference Counting, should I just dispense with iVar and go all #property? Should I even have the word "retain" in property? What if I'm deploying for iOS 4.3, should I still use ARC?
Don't feel like a doop, even though the compiler will add ivars for you if you don't include them, many people still declare them (many book authors as well) to make the code a little bit easier to read (easier to distinguish between ivar and property).
When creating a property now, Apple wants you to think in terms of Object Graphs, so do some research on "strong" and "weak" property attributes instead of retain and releases.
Also, iOS 4 is setup as a target for ARC so you should be ok. But I believe if you wanted to support iOS 3.0 you would have to manually manage retain and releases as before.

Resources