Recently I have worked with Core Data. When I want to set a default value for some fields, I came up with this problem:
So I made a simple represent:
We have 2 class Parent and Child, in which Child inherit from Parent.
// Parent.h
#interface Parent : NSObject
#property (strong, nonatomic) NSString *lastName;
// Child.h
#interface Child : Parent
In Parent class, I made a custom getter to set a default value when nothing is set:
// Parent.h
- (NSString *)lastName
{
if (_lastName) {
return _lastName;
} else {
return #"Parent Default Name";
}
}
But I cannot make a custom default value for the field "name" which Child inherits from its Parent.
// Child.h
#implementation Child
- (NSString *)lastName
{
if (super.lastName) {
return super.lastName;
} else {
return #"Child Default Name";
}
}
The main function to test:
int main(int argc, const char * argv[])
{
#autoreleasepool {
Parent *newParent = [[Parent alloc] init];
newParent.lastName = #"newParentName";
NSLog(#"Parent: %#", newParent.lastName);
Child *newChild = [[Child alloc] init];
NSLog(#"Child: %#", newChild.lastName);
}
return 0;
}
Apparently, #"Child Default Name" is never reach. The returned values would be #"Parent Default Name".
So my question here is: How can I set a custom getter for the field the Child class inherits from Parent without define an overriding property?
// Parent.h
#interface Parent : NSObject
{
NSString *_lastName; //just add this line.
}
#property (strong, nonatomic) NSString *lastName;
If you want another name to use, you can try this:
// Parent.h
#interface Parent : NSObject
{
NSString *_lastNameAlias; //can be used in child
}
#property (strong, nonatomic) NSString *lastName;
//Parent.m
#synthesize lastName = _lastNameAlias;
// Parent.m
- (NSString *)lastName
{
if (!_lastName) {
_lastName = #"Parent Default Name";
}
return _lastName;
}
// Child.m
- (NSString *)lastName
{
if (!_lastName) { //Or _lastNameAlias
_lastName = #"Child Default Name";
}
return _lastName;
}
And as Steven Fisher mentioned, lastname is different with lastName, but I think it is a spelling mistake.
Note:
- (NSString *)lastname
vs
- (NSString *)lastName
Objective-C is case sensitive. Make sure they have the same case and the behaviour you want should happen automatically.
That said, calling self.lastName is pretty weird (assuming you settle on lastName for both). I'm not sure that will work. I suggest calling [super lastName] instead.
The problem is that the parent implementation of the lastName method never returns nil. It either returns the current (non-nil) value of _lastName or it returns the default parent string. In neither case does it return nil. Hence the if [super.lastName] check will always return true.
To avoid the problem, the child getter needs to access the instance variable directly. This can only be done if the parent class explicitly declares the instance variable in the header file. So parent.h should be
#interface Parent : NSObject
{
NSString *_lastName;
}
#property (strong, nonatomic) NSString *lastname;
#end
and then the implementation in child.m can be
- (NSString *)lastname
{
if ( _lastName )
return( _lastName );
else
return #"Child default name";
}
Related
Suppose that I have a class that holds a mutable array. I want to make sure that if other classes ask for the array they will get a non-mutable type, but in the owning class, it is actually an instance of NSMutableArray, so that I can add and remove items.
#import "Person.h"
#class Asset;
#interface Employee : Person
{
NSMutableArray *_assets;
}
#property (nonatomic,copy) NSArray *assets;
-(void)addAssets:(Asset *)a;
The question is, do I have to modify the accessor methods into something like this, or will it automatically behave like I want?
#import "Employee.h"
#import "Asset.h"
#implementation Employee
/* Accessors for assets properties
-(NSArray *)assets
{
return [_assets copy];
}
-(void)setAssets:(NSArray *)assets
{
_assets = [assets mutableCopy ];
}
*/
-(void)addAssets:(Asset *)a
{
//is assets nil?
if (!_assets) {
//Create Array
_assets = [[NSMutableArray alloc]init];
}
[_assets addObject:a];
}
ppalancica's answer is incorrect. The copy attribute means only that the setter will take a copy when the property is set. The synthesized getter will not return a copy. You must implement that behavior yourself:
- (NSArray *)assets
{
return [_assets copy];
}
You might want to make an internal-only accessor that doesn't make a copy. You could also redeclare the property privately; client code will then be contracted to treat the array it requests as immutable.
This code demonstrates that the synthesized getter returns the uncopied object:
#import <Foundation/Foundation.h>
#interface ArrayReturner : NSObject
#property (copy, nonatomic) NSArray * array;
#end
#implementation ArrayReturner
{
NSMutableArray * _array;
}
- (BOOL)myArrayIsIdenticalTo:(NSArray *)otherArray
{
return _array == otherArray;
}
#end
int main(int argc, const char *argv[])
{
#autoreleasepool {
ArrayReturner * a = [ArrayReturner new];
[a setArray:#[#1, #2]];
NSArray * returnedArray = [a array];
// Does not throw
NSCAssert([a myArrayIsIdenticalTo:returnedArray],
#"Returned array is a separate instance.");
}
return 0;
}
Because you already specified the attribute "copy" for the array property, there is no need to override the getter and setter. The compiler will do all the heavy work for you.
If you specify "strong" instead, the getter and setter would look like:
-(NSArray *)assets
{
return _assets;
}
-(void)setAssets:(NSArray *)assets
{
_assets = assets;
}
And that may be a problem.
There is actually a WWDC conference that explains all these details. For NSString properties it is more recommended to use copy, and you can see it a lot like that in the iOS SDK frameworks.
I have this class, which is a port from a C# abstract class; this is the .h file:
#interface Schedule : NSObject {
}
#property (strong, nonatomic) NSDate *apptStartTime;
#property (strong, nonatomic) NSDate *apptEndTime;
#property (strong, nonatomic) NSString *key;
-(BOOL) occursOnDate: (NSDate *) timeOfAppointment;
#end
This is the .m file for Schedule:
#implementation Schedule {
}
#synthesize apptStartTime;
#synthesize apptEndTime;
#synthesize key;
/**
The OccursOnDate method is abstract and must be implemented by subclasses. When passed a date, the schedulers must determine if an appointment falls on that date. If one does, the method should return true. If not, the method returns false.
*/
-(BOOL) occursOnDate: (NSDate *) dateOfAppointment {
return YES:
}
Because it's a C# abstract class, it has to be overridden (or subclassed) which I have done here (this is the .h file):
#interface SingleSchedule : Schedule {
}
#property (strong,nonatomic) NSDate *apptDate;
-(BOOL) occursOnDate: (NSDate *)date;
#end
This is the .m file:
#implementation SingleSchedule {
}
#synthesize apptDate;
-(BOOL) occursOnDate: (NSDate *)date {
return (apptDate == date); // <--------- TODO where is apptDate set?
}
This is where I call the occursOnDate class, expecting to get the subcclass, but I get the superclass class instead:
-(void) addAppointmentsForDate:(NSDate *)checkDate scheduleSet: (NSMutableSet *)setOfSchedules appointmentSet:(NSMutableSet *)setOfAppts {
Schedule *sc = [[Schedule alloc]init];
Appointment *newAppt = [[Appointment alloc]init];
NSArray *scheduleArray = [setOfSchedules allObjects];
for(int i = 0; i < scheduleArray.count; i++) {
if([sc occursOnDate: checkDate]) { // <-------- method called is the superclass, not the override
newAppt = [self generateAppointment:checkDate andSchedule: scheduleArray [i]];
[setOfAppts addObject:newAppt];
}
}
}
What am I missing here to be able to get the subclass method and not the other? (I have looked at SO and Google but found nothing which would answer this question exactly).
The base class implementation is being called because sc is of type Schedule, not SingleSchedule. When you instantiate a class, the new object is aware of it's own implementation and it's base class chain, but the object is not aware of it's inherited classes.
Maybe what you want is:
SingleSchedule *sc = [[SingleSchedule alloc]init];
I have the below code which simply read a bunch of cards and inputs their names into a Mutable Array if they are not present beforehand.
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSMutableArray *allCards = [[NSMutableArray alloc] init];
char cardAvailable = '\0';
do {
Card *newCard = [[Card alloc] init];
NSLog(#"What is the Card Name ?\n");
char cName[20];
scanf("%s",cName);
NSString *nName = [NSString stringWithCString:cName encoding:1];
[newCard setName:nName];
if([newCard wasMatch:allCards]) {
NSLog(#"Card Already Present");
} else {
NSLog(#" NEW CARD NAME %# %# %s",[newCard getName],newCard.name, cName);
[newCard addGlobal:newCard toArray:allCards];
}
NSLog(#"Is there another Card to Input?");
scanf("%c",&cardAvailable);
} while(cardAvailable != 'N' || cardAvailable != 'n');
NSLog(#":::: Card Names Are ::::");
for(Card *card in allCards)
{
NSLog(#"%#",card.name);
}
}
return 0;
}
However, first - The name is simply NOT SET. I don't know why this is happening. Today was my first day ever with Objective C, so please pardon me if this is too simple.
The Card class files are -
#import <Foundation/Foundation.h>
#interface Card : NSObject
#property(nonatomic) BOOL chosen;
#property (nonatomic, getter = getName) NSString *name;
-(BOOL)wasMatch:(NSMutableArray*) allCards;
-(void)addGlobal:(Card*) aCardName toArray:(NSMutableArray*) allCards;
-(void)setName:(NSString *)name;
-(void)setChosen:(BOOL)chosen;
#end
I get the error here - in the add global line.
#import "Card.h"
#implementation Card
#synthesize chosen = _chosen;
-(BOOL)chosen
{
return _chosen;
}
-(void)setChosen:(BOOL)chosen
{
_chosen = chosen;
}
#synthesize name = _name;
-(NSString*)getName
{
return _name;
}
-(void)setName:(NSString*)name
{
name = _name;
}
-(BOOL)wasMatch:(NSMutableArray *)allCards
{
for(Card *card in allCards)
{
if([self.name isEqualToString:card.name])
{
return true;
}
}
return false;
}
-(void)addGlobal:(Card *)aCardName toArray:(NSMutableArray *)allCards
{ NSLog(#" THE NS STRING %#",aCardName.name);
[allCards addObject:aCardName.name];
}
#end
Your setter is wrong.
-(void)setName:(NSString*)name
{
name = _name;
}
You assign the current instance variable (_name) to the parameter.
This should be the other way around.
-(void)setName:(NSString*)name
{
_name = name;
}
But you don't need this at all. There are various problems with your code.
Objective-C does not use getFoo for ordinary getters.
#property (nonatomic, getter = getName) NSString *name; should be
#property (copy, nonatomic) NSString *name;
And unless you are writing code in a ancient version of Objective-C (which you shouldn't) you don't need explicit getters and setters. In recent version you don't even have to explicitly synthesize.
In newer version of Objective-C your Card class should look more like this:
#interface Card : NSObject
#property (assign, nonatomic) BOOL chosen;
#property (copy, nonatomic) NSString *name;
-(BOOL)wasMatch:(NSMutableArray*) allCards;
-(void)addGlobal:(Card*) aCardName toArray:(NSMutableArray*) allCards;
#end
#import "Card.h"
#implementation Card
-(BOOL)wasMatch:(NSMutableArray *)allCards
{
for(Card *card in allCards)
{
if([self.name isEqualToString:card.name])
{
return YES;
}
}
return NO;
}
-(void)addGlobal:(Card *)aCardName toArray:(NSMutableArray *)allCards
{ NSLog(#" THE NS STRING %#",aCardName.name);
[allCards addObject:aCardName.name];
}
#end
Way less code. You might want to look for a more up to date learning resource.
The rest of your code looks questionable as well. I honestly don't see the point in addGlobal:toArray:. But I don't want to make this answer longer than necessary. ;-)
Also, for the love of your future self, don't use 1 in NSString *nName = [NSString stringWithCString:cName encoding:1]; use the constant. In your case NSASCIIStringEncoding. But because it's no longer 1980 you most likely want to use NSUTF8StringEncoding instead of ASCII.
As I said, please get more recent and decent learning resources.
In the -setName: property accessor method, you assign the the _name instance variable value to the name parameter. It should be swapped like this:
-(void)setName:(NSString*)name
{
_name = name;
}
To learn Objective-C i reccomment the most recent Stanford lectures by Paul Hegarty. CS193p the class is called. You can find all materials, and excellent videos on iTunes U. Other then Apple resource, there is no better then this.
The code is probably the best way to see what I am trying to do:
AcInfo.h:
#interface AcInfo : NSManagedObject {
#private
}
#property (nonatomic, retain) NSString *registrationNumber;
#end
AcInfo.m:
#implementation AcInfo
#dynamic registrationNumber;
#end
AnotherClass.h:
#interface AnotherClass : NSObject {
}
#property (nonatomic, retain) AcInfo *detailItem;
#property (nonatomic, retain) IBOutlet UITextField *registrationNumberTextField;
- (void)setDetailItemValueFromUIElement:(id *)uiElement forAcInfoTarget:(id *)acInfoTarget;
#end
AnotherClass.m:
#import "AcInfo.h"
#implementation AnotherClass
#synthesize detailItem, registrationNumberTextField;
- (void)viewDidLoad
{
[super viewDidLoad];
registrationNumberTextField.text = #"Test";
// I expect this to set detailItem.registrationNumber to the value of
// registrationNumberTextField.text (Test) but it doesn't change anything!
setDetailItemValueFromUIElement:registrationNumberTextField forAcInfoTarget:detailItem.registrationNumber;
}
- (void)setDetailItemValueFromUIElement:(id *)uiElement forAcInfoTarget:(id *)acInfoTarget
{
if ([(id)uiElement isKindOfClass:[UITextField class]]) {
// This doesn't do anything when it returns!
(NSString *)acInfoTarget = (UITextField *)uiElement.text
return;
}
}
#end
In short, I want acInfoTarget to call the getter [detailObject registrationNumber] and the setter [detailObject setRegistrationNumber] in the setDetailItemValueFromUIElement: function...
You can set or read properties by name using
// setter
NSString *propertyName = #"myProperty";
[object setValue:newValue forKey:propertyName];
// getter
id value = [object valueForKey:propertyName];
This is slower than using the normal dot notation, though, and it's frequently (though not always) a sign of poorly-designed code.
Also note that id is a pointer type, so you probably don't actually mean "(id*)".
Your code wants to look something like this, I think:
- (void)setDetailItemValueFromUIElement:(id)uiElement forAcInfoTarget:(NSString*)acInfoTarget {
if ([(id)uiElement isKindOfClass:[UITextField class]]) {
NSString *newValue = ((UITextField*)uiElement).text;
[self.detailItem setValue:newValue forKey:acInfoTarget];
}
}
Properties are just syntax sugar for a couple of accessor methods. They are not, in essence, variables so you shouldn't treat them as such. If you want to affect a property, then what you wanting to do is call a method. So you should pass a id and selector parameter and not pointer to a variable type.
Given the following definition of a class with retain properties:
#interface FeedEntry : NSObject<NSCoding>
{
NSURL* url;
NSData* source;
}
#property (retain) NSURL* url;
#property (retain) NSData* source;
#end
#implementation FeedEntry
#synthesize url;
#synthesize source;
-(void)encodeWithCoder:(NSCoder*)coder
{
[coder encodeObject:url forKey:#"url"];
[coder encodeObject:source forKey:#"source"];
}
Why does the url property in initWithCoder method need the "retain":
-(id)initWithCoder:(NSCoder*)coder
{
url = [[coder decodeObjectForKey:#"url"] retain];
source = [coder decodeObjectForKey:#"source"];
NSLog(#"got url=%#\n", url);
return self;
}
Specifically, why doesn't the synthesized "get url" method retain the object? (I'm guessing the source property will need a retain as well).
Quick answer:
When you set:
url = [[coder decodeObjectForKey:#"url"] retain];
you are not using the #property. You are manually setting the value of the instance variable url. You must, therefore, also manually retain the value.
To set the variable using the synthesized properties, you would instead call:
[self setUrl:[coder decodeObjectForKey:#"url"]];
or
self.url = [coder decodeObjectForKey:#"url"];
Either of these forms would make use of the synthesized methods, and handle the retain automatically.
Details:
In Objective-C, the #property and #synthesize keywords automatically create the getter and setter methods for you:
#interface MyClass
{
id someValue;
}
#property (retain) id someValue;
#end
#implementation MyClass
#synthesize someValue;
#end
Is equivalent to:
#interface MyClass
{
id someValue;
}
- (id)someValue;
- (void)setSomeValue:(id)newValue;
#end
#implementation MyClass
- (id)someValue { return someValue; }
- (void)setSomeValue:(id)newValue
{
[newValue retain];
[someValue release];
someValue = newValue;
}
#end
This creates an important distinction between the "internal" member variable and the property having the same name. If you reference the member variable by name, you are bypassing the synthesized property methods.