Reputation: 421
I am having issues getting something to work in objective c. I have the following protocol:
@protocol PFItem <NSObject>
- (NSNumber *)weight;
- (NSNumber *)cost;
@end
This protocol is implemented in the following class:
@interface PFItemObject : NSObject <NSCoding, PFItem> {
NSString *name;
NSNumber *weight;
NSNumber *cost;
}
@property (retain, nonatomic) NSString *name;
@property (retain, nonatomic) NSNumber *weight;
@property (retain, nonatomic) NSNumber *cost;
@property (readonly) NSString *className;
+ (id)itemWithString:(NSString *)string;
@end
Now, this works well for me, except when I use the PFItemObject as a superclass like so:
@interface PFWeaponObject : PFItemObject <NSCoding, PFItem> {
NSString *damage;
NSString *critical;
NSString *range;
NSNumber *attackBonus;
NSNumber *damageBonus;
WeaponTypes type;
BOOL isTwoHanded;
}
@property (retain, nonatomic) NSString *damage;
@property (retain, nonatomic) NSString *critical;
@property (retain, nonatomic) NSString *range;
@property (retain, nonatomic) NSNumber *attackBonus;
@property (retain, nonatomic) NSNumber *damageBonus;
@property WeaponTypes type;
@property BOOL isTwoHanded;
+ (PFWeaponObject *)unarmedWeapon;
@end
The +itemWithString:
method, in the PFWeaponObject
works like this:
+ (id)itemWithString:(NSString *)string {
NSArray *components = [string componentsSeparatedByString:@";"];
PFWeaponObject *weapon = [[PFWeaponObject alloc] init];
[weapon setName:[components objectAtIndex:0]];
[weapon setWeight:[NSNumber numberWithFloat:[[components objectAtIndex:1] floatValue]]];
[weapon setCost:[NSNumber numberWithInt:[[components objectAtIndex:2] intValue]]];
[weapon setDamage:[components objectAtIndex:3]];
[weapon setCritical:[components objectAtIndex:4]];
[weapon setRange:[components objectAtIndex:5]];
[weapon setType:[[components objectAtIndex:6] integerValue]];
[weapon setAttackBonus:[NSNumber numberWithInt:[[components objectAtIndex:7] intValue]]];
[weapon setDamageBonus:[NSNumber numberWithInt:[[components objectAtIndex:8] intValue]]];
[weapon setIsTwoHanded:[[components objectAtIndex:9] boolValue]];
return [weapon autorelease];
}
I assumed that, because I have inherited from the PFItemObject, I should be able to assign values to the superclass' properties without problem. But when I do the following:
- (void)testItemCreationStrings {
NSString *weaponString = @"+1 Greatsword;25;2500;2d6;x3;Melee;5;1;1;YES";
PFWeaponObject *sampleWeapon = [PFWeaponObject itemWithString:weaponString];
}
All properties from the superclass (PFItemObject
) all return @"+1 Greatsword"
. Did I miss something somewhere that I should have done?
Thanks for any help you can provide, and please, feel free to ask for more information if you need it.
Upvotes: 0
Views: 286
Reputation: 1283
Edit because the question was edited. I think I have a solution.
Try this:
+ (id)itemWithString:(NSString *)string {
NSArray *components = [string componentsSeparatedByString:@";"];
PFWeaponObject *weapon = [[PFWeaponObject alloc] init];
[weapon setName:[components objectAtIndex:0]];
[weapon setWeight:[NSNumber numberWithInt:[[components objectAtIndex:1] intValue]]];
[weapon setCost:[NSNumber numberWithInt:[[components objectAtIndex:2] intValue]]];
// etc.
// etc.
}
I think you are not setting the behavior and format of the NSNumberFormatter, and I also think that using it is unnecessary. There are other examples of how to use number formatters if you really want to do it that way for some reason. If there is a reason you want/need to, I can provide direction.
Upvotes: 1