While creating a custom iOS table view cell, I created a new .xib file, dragged/dropped some UI elements in interface builder and my .h file looked like this...
#import <UIKit/UIKit.h>
#interface MasterTableViewCell : UITableViewCell
{
IBOutlet UILabel *cellLabel;
IBOutlet UIImage *cellImage;
}
#property (nonatomic, retain) IBOutlet UILabel *cellLabel;
#property (nonatomic, retain) IBOutlet UIImage *cellImage;
#end
On some blogs I saw that the instance variables were missing. When do I need to declare instance variables? Are both instance variables and #property declarations not needed for a particular UI object.
Also I am creating the app using automatic reference counting, so garbage collection needs aren't there as well. What difference does that make in usage of instance variables & properties?
There is no garbage collection in iOS. iOS uses reference counting to track ownership of objects. Using ARC does not do away with reference counting, but the compiler takes care of releasing and retaining objects. When using ARC you are not allowed to send a retain, release, or autorelease message to an object, nor are you allowed to call [super dealloc] in a dealloc method. In your code above, since you are using ARC, the 'retain' attributes should be replaced by the 'strong' attribute.
When you use #property, and the corresponding #synthesize in your implementation, you do not need to create a backing instance variable - the compiler does that for you. #property along with #synthesize create your accessor methods (your getters and setters), and also enable you to use dot notation to refer to your objects' properties. You may still write your own accessor methods if you choose.
The above code could be replaced by the following:
#import <UIKit/UIKit.h>
#interface MasterTableViewCell : UITableViewCell
#property (nonatomic, strong) IBOutlet UILabel *cellLabel;
#property (nonatomic, strong) IBOutlet UIImage *cellImage;
#end
In your implementation file you would have:
#import "MasterTableViewCell.h"
#implementation MasterTableViewCell
#synthesize cellLabel;
#synthesize cellImage;
or
#synthesize cellLabel, cellImage;
... remainder of your code
In your code, to ensure that you are using your accessor methods, use 'self' to refer to your properties:
self.cellLabel.text = #"some text";
or
[[self cellLabel] setText:#"some text"];
I hope this helps clarify things a little.
If you don't create the instance variables (iVar's) then they will automatically be created for you if you are using the #synthesize directive (see below) so they really aren't required. If you are using #dynamic or writing your own methods and want to access the iVar directly then you need to declare it yourself.
In the documentation for Declared Properties under the Property Implementation Directives section, it states:
#synthesize You use the #synthesize directive to tell the compiler
that it should synthesize the setter and/or getter methods for a
property if you do not supply them within the #implementation block.
The #synthesize directive also synthesizes an appropriate instance
variable if it is not otherwise declared.
Note that this behavior is for "modern" runtimes (2.0 and newer). Before this, the iVar was required to be declared or the #synthesize would generate an error.
Related
I am following the tutorial here: http://blog.soff.es/archiving-objective-c-objects-with-nscoding
to create an NSObject that can save my match data in a turn based game.
However I get this warning in my .m file:
Autosynthesized property 'title' will use synthesized instance variable '_title', not existing instance variable 'title'
So my Qustion is if (in the code below) I delete the code in between the brackets will I be losing something important?
#interface Note : NSObject <NSCoding> {
NSString *title;
NSString *author;
BOOL published;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *author;
#property (nonatomic) BOOL published;
#end
You shouldn't explicitly declare ivars since the properties will auto-synthesize their own ivars with slightly different names. The explicit ivars are pointless and won't be used by the properties. Having them is just going to lead to bugs when you use your ivars by mistake when you meant to set a property.
The warning is pointing this out by letting you know there will be two similar ivars.
Your code should simply be:
#interface Note : NSObject <NSCoding>
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *author;
#property (nonatomic) BOOL published;
#end
This avoid bugs such as:
title = #"Some Title"; // sets your ivar, not the property
as opposed to:
_title = #"Some Title"; // directly sets the property's ivar
Of course you should use the property:
self.title = #"Some Title"; // uses the property methods
You would be losing instance variables, but you don't really want them as the properties will create them for you (with slightly different names) and it's safer to access these (kind of hidden) auto-generated instance variables via the property accessor methods.
Yes it is and likely you get something you do not want.
If you do not have an instance variable neither with the identifier identifier nor with the identifier _identifier, manual synthesize and automatic synthesize will create one with the name _identifier.
If you already have an instance variable with the identifier _identifier, manual synthesize and automatic synthesize will use it. (Therefore in most cases it is meaningless to declare such an instance variable.)
But if you have an instance variable with the identifier identifier (without underscore) manual synthesize will use it, while automatic synthesize will not use it and instead create a new one with the identifier _identifier. Therefore you have two instance variables identifier and _identifier. Typically you do not want this.
I'm developing for iOS 7 but I still have to manually write getters otherwise my properties just don't get initialized. I tried to manually synthesize those properties, even though that shouldn't be needed anymore, but that doesn't do it.
In my view controller below, I use the property motionTracker, which never gets initialized. I have the same issue with all my projects, so I know it's a misunderstanding on my part.
#import "ViewController.h"
#import "TracksMotion.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIButton *startRecording;
#property (weak, nonatomic) IBOutlet UIButton *stopRecording;
#property (strong, nonatomic) TracksMotion *motionTracker;
#end
#implementation ViewController
#synthesize motionTracker = _motionTracker;
- (void)startMyMotionDetect
{
[self.motionTracker startsTrackingMotion];
}
#end
The motionTracker has a public API for the method startsTrackingMotion so I don't know why this doesn't work.
#import <Foundation/Foundation.h>
#import <CoreMotion/CoreMotion.h>
#interface TracksMotion : NSObject
- (void)startsTrackingMotion;
- (void)stopTrackingMotion;
#property (strong, nonatomic) CMMotionManager *motionManager;
#end
Properties / instance variables are not magically initialized for you. When you say:
#property (strong, nonatomic) TracksMotion *motionTracker;
... you are just reserving memory space for an instance variable (and generating a getter and a setter method through #synthesize or autosynthesis). There is no actual TracksMotion object there until you put one there. You must write code to do that. You must create or obtain a TracksMotion instance and assign it to self.motionTracker at some point, presumably early in the life of self (in this case, that's a ViewController instance). Until you run code that does that, self.motionTracker is nil.
(It is possible that you are being confused because it looks like outlets are automatically initialized. For example, you've got #property (weak, nonatomic) IBOutlet UIButton *startRecording; And sure enough, self.startRecording is a button. But that's because the nib-loading process does for you the very thing I'm saying you must do: it makes a button from the storyboard or .xib file, and assigns it to this instance variable.)
I'm going through a tutorial on core-data in Objective-C and can't understand the following syntax:
#interface RootViewController : UITableViewController <CLLocationManagerDelegate> {
NSMutableArray *eventsArray;
NSManagedObjectContext *managedObjectContext;
CLLocationManager *locationManager;
UIBarButtonItem *addButton;
}
#property (nonatomic, retain) NSMutableArray *eventsArray;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain) CLLocationManager *locationManager;
#property (nonatomic, retain) UIBarButtonItem *addButton;
#end
We have four properties here that are declared in the implementation file, which to my understanding means they are private. What exactly is happening within the curly brackets? Why are these variables put there? And also, is it a class extension? I see () are missing here so probably it is not. What is this kind of syntax called then?
Its not a category.Its just a class named RootViewController which extends UITableViewController and implements protocol CLLocationManagerDelegate.
Coming to your braces ->
generally if you don't create iVars in curly braces, by default they are created with underscore as prefix to them. This is done by compiler.
But here, you explicitly said, that the ivar should be without underscore(_).
Any you should synthesize them as below or else it will give a warning.
#synthesize eventsArray= eventsArray;
It's just a regular definition of a RootViewController class, the #interface doesn't necessarily have to be in a header file, private classes (that shouldn't/don't need to be accessible elsewhere) can also be defined directly in the .m file.
The definitions in the curly braces are just regular instance variables of the RootViewController class.
What you have is called the class interface. It is just the .h file of your program files. .If you want a class category, just do
#interface RootViewController (CategoryName)
and for an extension, inside the .m type
#interface RootViewController ()
#end
#implementation
Variables between curly braces:
{
NSMutableArray *eventsArray;
NSManagedObjectContext *managedObjectContext;
CLLocationManager *locationManager;
UIBarButtonItem *addButton;
}
are just usual variables.
For variable, defined with #property base word:
#property (nonatomic, retain) NSMutableArray *eventsArray;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain) CLLocationManager *locationManager;
#property (nonatomic, retain) UIBarButtonItem *addButton;
created accessor and mutator methods. And also you can define options for those variables in bracers. Plus you can get local synonym for them, using base word #synthesize in .m file, like
#synthesize addButton = myLovelyButton;
then you can use myLovelyButton in .m file instead addButton
Both of these definition do not belong to the category.
For define category just type code like:
#interface <#className#> (<#categoryName#>)
{
//optional variables here
int i;
NSString *s;
}
//optional variables here
#property NSString *str;
//optional methods here
-(void)doSomething;
#end
Then you can implement your methods and use these variables:
#implementation <#className#> (<#categoryName#>)
-(void)doSomething
{
int i = 0;
str = #"blah";
s = #"wow";
NSLog(#"%i - %# - %#",i,str,s);
}
#end
Use that for add your methods to existing classes.
Variables inside brackets directly after the #interface or #implementation are instance variables. These are variables associated with each instance of your class, and thus accessible anywhere in your instance methods.
If you don't put the brackets, you declare global variables. Any variable declared outside of any bracket block will be a global variable, wether these variables are before or after the #implementation directive. And global variables are evil and needs to be avoided at all costs (you can declare global constants, but avoid global variables), especially because they are not thread-safe (and may thus generate bugs that are a mess to debug).
#interface YourClass : ParentClass
{
// Declare instance variables here
int ivar1;
}
// declare instance and class methods here, as well as properties (which are nothing more than getter/setter instance methods)
-(void)printIVar;
#end
// .m
int someGlobalVariable; // Global variable (bad idea!!)
#implementation YourClass
int someOtherGlobalVariable; // Still a bad idea
-(void)printIVar
{
NSLog(#"ivar = %d", ivar1); // you can access ivar1 because it is an instance variable
// Each instance of YourClass (created using [[YourClass alloc] init] will have its own value for ivar1
}
Only modern compilers let you declare instance variables (still in brackets) also inside either your class extension (#interface YourClass () in your .m implementation file) or in your #implementation, in addition to the possibility to declare them after the #interface in your .h. The benefits being to hide those instance variables from external users of your classes, by declaring them in the .m file and not in the .h file anymore, because users of your class don't need to be aware of the internal coding details of your class, but only needs to know the public API.
One final advice: instead of using instance variables, Apple more and more recommends to use #property directly, and let the compiler (explicitely using the #synthesize directive, or implicity with modern LLVM compilers) generate the internal backing variable. So that at the end you generally won't need to declare instance variables at all, and thus omit the empty { } after the #interface directive:
// .h
#interface YourClass : ParentClass
// Declare methods and properties here
#property(nonatomic, assign) int prop1;
-(void)printProp;
#end
// .m
#implementation YourClass
// #synthesize prop1; // That's even not needed with modern LLVM compiler
-(void)printProp
{
NSLog(#"ivar = %d", self.prop1);
}
Super class Resource
#interface Resource : CoderObject
#property (strong, nonatomic) NSString *resourceID;
#property (assign, nonatomic) ResourceType resourceType;
#property (assign, nonatomic) DataType dataType;
#end
Subclass ViewResource
#interface ViewResource : Resource
#property (strong, nonatomic) CustomView *view;
#property (strong, nonatomic) UIViewController *viewController;
#end
In subclass ViewResource's init method how to access Resource's variable dataType? Now I'm trying to just use super.dataType = ...
Is there other ways?
You just need to use self.dataType. Your subclass has full visibility of all of the superclass properties defined in the .h file. Using self.xxx also gives you the ability to override the accessor methods if required in the future without coming back to edit all of your usage code.
Looking at your link below, fair enough. Those are all valid points. Accessors shouldn't have side effects but you can't guarantee they won't. If the property is defined the superclass then you have a couple of options:
Use self.xxx to set the property and endeavour to ensure no side effects
Call an init method on super, passing the required parameters, and set them there
Like Wain stated in his answer you have direct access to your super's class members (if they are not private).
And there is no problem calling self.property in the init method as long as your init looks like this
-(id)initAndTheNameYoWantAndMaybeSomeParameters:(NSString *)paramExample {
self = [super initNameOfAnInitMethodFromSuperClass];
//check if the init was with success
if(self != nil) {
self.myStringProp = paramExample;
//or
self.propertyFromSuper = paramExample;
}
}
Yes, you can also do stupid stuff in the initMethods (I did it before :)) ) like calling the same initMethod from inside it which was generating a recursive calling that was crashing my app. (Easy to spot this issue)
I am having a label code for "decisionText" inside dot-m file as follows :
#synthesize decisionText ; //<<<This generates the error
inside dot-h file, the code is written as follows:
IBOutlet UILabel *decisionText
The error i get is :
No declaration of property 'decisionText found in the interface.
ps: In the interface builder when i click the label, i can find the name "decisionText" under Referencing Outlets mapped with File's Owner
Stuck on this. :(
As suggested I removed line #synthsize decisionText and used :
#property (nonatomic,weak) IBOutlet UILabel *decisionText ;
Now i get the error :
Expected a property attribute before 'weak'
Dot M file :
#import "ClickButtonViewController.h"
#implementation ClickButtonViewController;
//#synthesize decisionText ;
#property (weak,nonatomic) IBOutlet UILabel *decisionText ;
-(IBAction)buttonPressed:(id)sender
{
decisionText.text = #"Go for it!" ;
}
-(void)dealloc{
[decisionText release];
[super dealloc] ;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#end
in the .h file add:
#interface ViewController : UIViewController
{
//....
IBOutlet UILabel *decisionText ;
//...
}
#property (nonatomic, retain) IBOutlet UILabel *decisionText ;
//...
#end
then in the .m file add:
#synthesize decisionText ;
You use #synthesize statements with declared properties. Thus, your code should probably look like:
#interface ViewController : UIViewController
{
// your ivars go here
// but this is not needed:
//
// IBOutlet UILabel *decisionText;
}
// your properties go here
#property (nonatomic, retain) IBOutlet UILabel *decisionText;
#end
If you're using ARC, replace retain with weak.
And in your .m file, you would have:
#implementation ViewController
#synthesize decisionText = _decisionText;
// and your implementation goes here
Note:
While you can explicitly declare your instance variable, if you omit it, the #synthesize statement will create one for you. Thus, you don't need to explicitly declare any instance variable. In fact, I might argue that you should not explicitly declare your instance variable because if you have a typo, it only presents an opportunity to accidentally end up with two instance variables, the one you explicitly declared and the one the compiler will generate. I've seen that problem here on Stack Overflow more than once. So, in my example, I've omitted the explicit instance variable declaration and I'll let the compiler take care of it for me and it minimizes the chance for error.
While not required, it is often advised that #synthesize statements specify a different name for your property's instance variable (e.g., in this case, I'm suggesting that the property decisionText would have an instance variable of _decisionText). This helps discourage the accidentally reference to instance variables when you meant to invoke the property's getter or setter. (In fact, in Xcode 4.4 and later, if you omit the #synthesize statement, the compiler will automatically synthesize the instance variable for you with the leading underscore.) Thus, in your code, you would then refer to the property self.decisionText or to the instance variable _decisionText. It's generally not so critical for IBOutlet objects, but as you start to use your own custom properties, this convention becomes useful.
Alternatively, if you are using Xcode 4.4 you can use autosynthesis.
In which case you don't need to declare the iVar you can just write:
#property (weak, nonatomic) IBOutlet UILabel *decisionText;
And you don't need to write the #sythesize line at all.
If you do this - be aware that the generared iVar will have a leading underscore appended by default, although you should just stick to using the property accessor in this case so it makes little difference.
You can see what you can do in the Objective-C Features Availability Index
Change
IBOutlet UILabel *decisionText
to
#property (nonatomic, weak) IBOutlet UILabel *decisionText
You can only synthesize properties you defined like that with the #property keyword
You declared only the instance variable which will store the content of your property but you didn't declare the property itself. I think that the easiest way to solve that is to add in your public interface (.h file) or in your private interface (#interface ClassName () ... #end in ClassName.m file) the declaration of the property.
ClassName.h
#interface ClassName : ParentClass
#property (nonatomic, weak) IBOutlet UILabel decisionText; //This is the declaration of the property than you can ctrl-drag to wire it up to your label
#end
ClassName.m
#implementation ClassName
#synthesize decisionText = _decisionText //the _decisionText stuff is the name of the instance variable that will store the content of your property
... //your methods
#end