Is it necessory to define property for an IBOutlet field? - iphone

I have seen many iPhone examples to use IBOutlet as a field linked to a UI control in Interface Builder, and the field is also defined as property in the interface class. For example, the following codes are similar ones from Apple example codes:
// in .h file
#interface MyViewController : UIViewController {
IBOutlet UILabel* _firstName;
IBOutlet UILabel* _lastName;
...
}
#property (nonatomic, retain) UILabel* firstName;
#property (nonatomic, retain) UILabel* lastName;
...
#end
// in .m file
#implementation MyViewController {
#synthetic firstName = _firstName;
#synthetic lastName = _lastName;
...
#end
I tried to link labels in Interface Builder to my controller class IBOutlets and I can see _firstName and _lastName. Since the linkage is directly from Interface Builder to my interface class members (even private ones if I place #private directive). Do I need to define properties for those fields?
Actually, I tried to remove these properties and it seems my codes work fine. By defining properties, my class expose them as public. I don't have any usage or reason for them as properties within or out side my codes. My question is that if this practice, defining fields as properties, necessary? Do I miss any thing such as memory management which might be called from Objective-C concept or framework?

As Jeremie Wekdin mentioned that, my question is somehow duplicated. The similar question and answers do reveal that in case of nib/xib file being used, there is memory issue to be considered.
In short, Cocoa will look for setOutletName first, and use the property method to set UI control object; otherwise, Cocoa will direct set to the class member variable and retain it. That means the filed object should be released in dealloc method.
That's fine. However, in case of my question, my field variable has different name from its corresponding property name, like _firstName and firstName. In this case, I think that Cocoa is not smart enough to figure out the property method and object retrieved from nib/xib is directly set to the class member.
To verify it, I overwrite the setter:
// in .m file
#implementation MyViewController {
#synthetic firstName = _firstName;
- (void) setFirstName:(UILabel*) value {
NSLog("_firstname: %#", _firstName);
[_firstname autorelease];
_firstName = [value retain];
}
Then I load my view, the log message does not show in the XCode's output console. However, if I keep the variable name and property name the same. I do see the setter is called:
// in .h
#interface MyViewController : UIViewController {
IBOutlet UILabel* firstName;
...
}
#property (nonatomic, retain) UILabel* firstName;
...
#end
// in .m file
#implementation MyViewController {
#synthetic firstName;
- (void) setFirstName:(UILabel*) value {
NSLog("firstName: %#", firstName);
[firstName autorelease];
firstName = [value retain];
}
...
#end
In the output console, when the view shows, I see:
firstName: (null)
As the duplicated QA suggested, I read the Appl's Resource Programming Guide. Find the document in the section The Nib Object Life Cycle, The Object Loading Process, and #3 Outlet connection. You should see that Mac OS X and iPhone OS have different ways to connect outlet to objects. "In iPhone OS, the nib-loading code uses the setValue:forKey: method to reconnect each outlet"
So I tried the following codes:
#implementation MyViewController {
#synthetic firstName = _firstName;
- (void) setValue:(id) value forKey:(NSString*) key {
NSLog("forKey: %#; value: %#", key, value);
if ([key isEqualToString:#"_firstName"])
// It should then call the accessor or property
// self._firstName = value;
// to set value, like the follow codes in the setter:
[_firstName autorelease];
_firstName = [value retain];
}
...
}
...
#end
I recompiled my code again and I did see all the property setter calls, including the key _firstName. Continued from Apple's doc:
"That method (setValue:forKey:) similarly looks for an appropriate accessor method and falls back on other means when that fails."
This explains why in my case(property name is different from outlet variable name) the property is called by Cocoa.
In conclusion, there is a memory issue when IBOutlet and nib/xib are used (as a way to load views) for a field control. It would be nice to let Cocoa to find out a defined accessor or property to set a field variable, which handles retaining objects. If you define a property for an IBOutlet field variable, both should have the same. As a result, the codes will work in both Mac OS X and iPhone OS.

Related

Can I use a custom initializer for a core data model object?

I use Core Data and have an object ExerciseForRoutine. I'm currently manually creating it and then settings it's attributes, which seems to waste code. Is there any way I can create a custom init method to handle this in one line (I know how to do around alloc/init, but core data has a different init method..)
Current Code:
ExerciseForRoutine *exerciseForRoutine = (ExerciseForRoutine *)[NSEntityDescription insertNewObjectForEntityForName:#"ExerciseForRoutine" inManagedObjectContext:managedObjectContext];
exerciseForRoutine.name = self.selectedExercise;
exerciseForRoutine.timeStamp = date;
exerciseForRoutine.muscleGroup = self.muscleName;
exerciseForRoutine.musclePicture = self.muscleURL;
ExerciseForRoutine Class
#class Routine;
#interface ExerciseForRoutine : NSManagedObject {
#private
}
#property (nonatomic, strong) NSDate * timeStamp;
#property (nonatomic, strong) NSString * name;
#property (nonatomic, strong) NSString * muscleGroup;
#property (nonatomic, strong) NSString * musclePicture;
#property (nonatomic, strong) Routine * exerciseToRoutine;
#end
#implementation ExerciseForRoutine
#dynamic timeStamp;
#dynamic name;
#dynamic muscleGroup;
#dynamic musclePicture;
#dynamic exerciseToRoutine;
I did this using awakeFromInsert and awakeFromFetch.
From Apple's documentation:
In a typical Cocoa class, you usually override the designated initializer (often the init method). In a subclass of NSManagedObject, there are three different ways you can customize initialization —by overriding initWithEntity:insertIntoManagedObjectContext:, awakeFromInsert, or awakeFromFetch. You should not override init. You are discouraged from overriding initWithEntity:insertIntoManagedObjectContext: as state changes made in this method may not be properly integrated with undo and redo.
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdManagedObjects.html
The classes which Xcode creates for handling core data objects should not be overridden, instead what you could do is create your own custom class which inherits from NSObject and write your methods to handle the managed object their.
Sol: You can do this with the help of the parameterized init method
Then it would look something like this
CoreDataHelperClass *someobj = [[CoreDataHelperClass alloc]initWithname:#"name" andTimeStamp:#"Time" andMuscleGroup:#"musclegroup" andPicture:UIImagePNGRepresentation(someimageObj)];
To do the above you need to add your own init method in the CoreDataHelperClass class like this
.h part of CoreDataHelperClass
- (id)initWithName:(NSString*)name andTimeStamp:(NSString*)timeStamp andMuscleGroup:(NSString*)group andPicture:(NSData*)imageData;
.m part of CoreDataHelperClass
- (id)initWithName:(NSString*)name andTimeStamp:(NSString*)timeStamp andMuscleGroup:(NSString*)group andPicture:(NSData*)imageData
{
//you assignment code to the core data attributes goes here
ExerciseForRoutine *obj = [[ExerciseForRoutine alloc]init];
obj.name = name;
obj.timestamp = timeStamp;
//and so on
return self;
}
Anyways what you could also do is pass a dictionary with the keyvalue pair get the values in your custom class or you may also pass an NSMutableArray like what ever suits your business model both will work.
You can get the values of Dictionary or Array inside your CoreDataHelperClass and assign those values to your attribute.
Hope i have got your query right if not then kindly mention the error part via comments
To add to #Radix's answer, you should consider using mogenerator because it'll do much of that subclassing business for you.
http://rentzsch.github.io/mogenerator/
See here for a guide to set it up and have it running on XCode 5.
There's a small caveat to watch out for though: if you get an assertion failure that reads:
-[MOGeneratorApp setModel:] blah blah blah
Then you should point mogenerator to the .xcdatamodel file inside of the .xcdatamodeld package in your Run Script Phase, like so:
mogenerator -m Model.xcdatamodeld/Model.xcdatamodel -O Project/Model --template-var arc=true
Where Project is the name of your project and Model is the name of your model.
See https://github.com/rentzsch/mogenerator/issues/169.

clarifying on properties in objective C

Sorry for the simple question.
When I see a definition of a property inside the h file, but outside of the class #interface scope, what does it mean ?
#property (nonatomic, readonly) RMMapContents *mapContents;
Here is the code:
#class RootViewController;
#class RMMapContents;
#interface MapTestbedAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
//MAIN VIEW
//==============
RootViewController *rootViewController;
// NETWORK DATA
// =============
NSMutableArray *photoTitles; // Titles of images
NSMutableArray *photoSmallImageData; // Image data (thumbnail)
NSMutableArray *photoURLsLargeImage; // URL to larger image
NSMutableData *receivedData;
NSURLConnection *theConnection;
NSURLRequest *request;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
#property (nonatomic, readonly) RMMapContents *mapContents;
#end
Inside a function I see this line:
- (void)foo:(xyz *)abc{
..
RMMapContents *mapContents = [self mapContents];
..
}
So, taking it from C++, the mapContents seem like it is not a global scope var (after all, that's why they call them properties, right?), but isn't defining the same name again inside the function weird a bit?
I hope someone can clarify a little here.
Thanks!
The scope of the #interface block extends upto the #end keyword and is not restricted to the braces {}.
So the #property declaration lies very much inside the scope of the #interface and like cli_hlt rightly answered, it acts like a substitute to setter and getter methods for the mapContents property.
so a property named mapContents, would have setters and getters which look like this :
- (void)setMapContents; //setter
- (RMMapContents *)mapContents; //getter
and would can be accessed from within the class using these methods:
[self setMapContents:newContents];
AND
RMMapContents *contents = [self mapContents];
Well, a property is not just a variable. A property is a variable plus its setter and getter methods. A property is usually said to be backed by a variable, which usually(but not always) has the same name as the property itself.
So there are basically three scenarios:
The developer has redefined the backing variable, look for something like:#synthesize mapContents=mapContents_, at the beginning of the implementation -> no problem here.
The compiler defined the variable to be something you don't now but not equal to mapContents - > no problem.
The property backing variable is indeed called "mapContents", so then the local definition hides the global definition (look for a compiler warning here). But by calling [self mapContents] you will not access the global variable but call the getter, which in turn will access the class variable (because then the local mapContents is out of scope)
Hope this helps.
global var mapContents is readonly,in foo function , create a new pointer,then you can change the value of inner var.
Look for a method in your class with a name mapContents that will return a initialization to your RMMapContents class.
Basically this line RMMapContents *mapContents = [self mapContents]; says that initializing an instance of RMMapContents called mapContens using the method mapContents.

Objective c: i want the automatic memory management of properties, but no access from other classes

I've kind of been confused about properties. Some people say to always use setters and getters for ivars, even within the ivar's class. So if "name" is an ivar, when referring to it, one should always use "self.name". Always. Even if you're in the same class that "name" is declared in.
First, is that correct advice?
Second, what if I wish to reap the automatic memory management that comes with declaring "name" as a property and synthesizing it, but I don't want to give other classes access to change "name"? I guess it would be sort of a private property?
Thanks!
Yes, you should always try to use the property accessors when possible. Using ARC alleviates these concerns somewhat, but it's still good style. As for your second question, you can declare the property as readonly in the public header file and redefine it in a class extension:
In MyClass.h:
#interface MyClass : NSObject
#property (strong, readonly, nonatomic) id foo;
#end
In MyClass.m:
#interface MyClass()
#property (strong, readwrite, nonatomic) id foo;
#end
#implementation MyClass
#synthesize foo = _foo;
// The rest of your code goes here.
#end
This will allow you to call [self setFoo:foo] all day inside of MyClass’s implementation, but not other classes.
For ivars which are accessed externally, I generally use properties to access the ivar from within the class, for ivars which are only used internally (usually BOOL, NSUInteger, NSInteger, etc), I use the ivar directly. I do however access an consistently within the class (i.e. if I'm using a property to access it, I always use a property).
For the second part of your question. You can create a readonly property in the class's interface definition and within the same file as the implementation create a category with the read-write property. For example:
MyClass.h
#interface MyClass : NSObject
{
NSString * name;
}
#property (nonatomic, readonly) NSString * name;
#end
MyClass.m
#interface MyClass()
#property (nonatomic, retain) NSString * name;
#end
#implementation MyClass
#synthesize name;
-(void)dealloc
{
[name release];
[super dealloc];
return;
}
#end
Keep in mind, that although another class accessing the method -setName: may cause compile warnings or errors, another class may still call -(id)performSelector:withObject: with without an error.
For instance:
MyClass * test = [[MyClass alloc] init];
test.name = #"David";
is functionally the same as:
MyClass * test = [[MyClass alloc] init];
[test performSelector:#selector(setName:) withObject:#"David"];

Property with no iVar and getter method?

I spotted this today and just want to verify my understanding of what is going on. "prefixName" is a readonly property that uses a getter method to directly pass a value back, there is no iVar storing the result on the PR_ViewController object. Also if the property was not readonly adding a setter still would not work as there is no iVar to set.
Created: [Meth] prefixName
By contrast "characterName" works the usual way for a property, adding a getter, a setter and an iVar.
Created: [Meth] characterName
Created: [Meth] setCharacterName
Created: [iVar] characterName
.
#interface PR_ViewController : UIViewController
#property (nonatomic, readonly) NSString *prefixName;
#property (nonatomic, retain) NSString *characterName;
.
#implementation PR_ViewController
#synthesize characterName;
- (NSString *)prefixName {
return #"FRED";
}
You are not required to synthesize accessors - you can perfectly well write them yourself, which is what is done in your example.
Further, a read-only property does not have to be based on an ivar, but can be computed, based on other ivars or properties. This can be useful if you use bindings to display values in the UI that are derived from other properties, provided you like that coding style.
Here is a simple example of a readonly property computed based on two other properties:
Header file:
#property double width;
#property double height;
#property (readonly) double area;
Implementation:
#synthesize width, height;
- (double)area
{
return width*height;
}
+ (NSSet *)keyPathsForValuesAffectingArea
{
return [NSSet setWithObjects:#"width", #"height", nil];
}
Now, whenever either one of width or height changes, the area property changes, too, and its changes are propagated to its listeners (thanks to keyPathsForValuesAffectingArea.)
You can also see the fullName example in this doc.

iPhone Objective-C Basic Example Question (about Properties)

Sorry I couldn't give a more descriptive title. My question (after looking at the code below) is what statusText is. Is it an IBOutlet or a UILabel? How does "#property (retain,nonatomic) UILabel *statusText" work? Does that statement mean that statusText is a property???
Thanks for answering. If there are any questions, please feel free to comment.
Button_FunViewController.h
#interface Button_Fun2ViewController : UIViewController {
IBOutlet UILabel *statusText;
}
#property (retain,nonatomic) UILabel *statusText;
- (IBAction)buttonPressed: (id)sender;
Button_FunViewController.m
#synthesize statusText;
- (IBAction)buttonPressed: (id)sender {
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:#"%# button was pressed.", title];
statusText.text = newText;
[newText.release];
}
IBOutlet evaluates to nothing, it's just a hint to Interface Builder that it's a 'connection'.
And yes, statusText is a property then. There are three statements needed for a property (on a non-"modern runtime" system) – an ivar declaration, a #property statement, and a synthesize statement.
statusText is an instance variable of type UILabel*. The IBOutlet keyword simply makes that instance variable available to Interface Builder.
#property declares accessor and/or mutator methods for the given property. It's equivalent to declaring -statusLabel and -setStatusLabel: methods.
You can use #synthesize to automatically implement these -statusLabel and -setStatusLabel: methods. The nonatomic and retain keywords define the behaviour of these automatically-generated methods.
Alternatively, you can implement the -statusLabel and -setStatusLabel: methods yourself.
what is statusText ?
statusText is a UILabel in your code example
Is it an IBOutlet or a UILabel?
Both.
UILabel is a type (a pointer to UILabel component that you use in GUI)
IBOutlet marks variable for Interface Builder application, so that it knows to show it as Outlet. During compilation IBOutlet is compiled out, it is defined in NSNibDeclarations.h as:
#define IBOutlet
How does #property
(retain,nonatomic) UILabel
*statusText work?
You can create accessors (getters/setters) for a variable by hand, no need to use property. You can just have UILabel *statusText and implement your getter/setters by hand.
You can have accessors declared by compiler by defining variable as a #property and then either use #synthesize to create accessors in .m file or again you declare the accessors yourself (you can override default accessors that would be generated)
You can have readwrite or readonly property - meaning either both setter and getter gets generated or only getter.
You can use copy, retain or assign for setter (see more about memory management about the tree optons copy/retain/assign)
There are some other options like nonatomic/atomic which has to do with generating mutexes and lock variable before access and so on (see more about properties)
For example if you have variable
NSString * string;
defining it as readwrite property and then synthesising you get the compiler to generate for you:
#property (copy, readwrite) NSString * string
then using
#synthesize string;
generates something like:
- (NSString *) string
{
return string;
}
- (void)setString:(NSString *)str
{
NSString * copy = [str copy];
[string release];
string = copy;
}
Does that statement mean that
statusText is a property???
Yes you defined it as a property as explained above.
There are couple of concepts involved here.
Definition of variable, defining it as IBOutlet for Interface Builder, declare variables as properties so that compiler generates getters/setters for you, defining type of getters/setters such as access method, memory management and locking.
I hope this explains your questions and if you follow the link you will find the explanation by Apple which I believe is quite clear about how to use properties.
Sorry for the horrible formatting ...
If you put IBOutlet in there like that, it will allow you to link the item while you're in interface builder.
Once you have the IBOutlet setup, you can open that class's nib and then select the File's Owner, then go to the inspector and drag a link from the Connections tab to the object.
This let's you then make changes to the object in your class's code and it will change the linked object. So once you link "statusText" to a specific UILabel, you can then use statusText.text = #"new text" in your class's code to modify the item, for example.
Also, IBAction allows you to make links in the same way. You can drag a connection from a control's event to the file owner and then select any method within that class that has IBAction as a return value.
There are in fact two statusText "things" in your example. There is a statusText object of type UILabel, and there is a statusText function created by #synthesize. When you use statusText from inside a method you are reffering to the object, not the function. Use self.statusText to use the property/function.