gpt4 book ai didi

objective-c - 继承的属性不起作用

转载 作者:行者123 更新时间:2023-11-29 04:44:03 25 4
gpt4 key购买 nike

我在 objective-c 中无法实现某些功能。我有以下协议(protocol):

@protocol PFItem <NSObject>

- (NSNumber *)weight;
- (NSNumber *)cost;

@end

该协议(protocol)在以下类中实现:

@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

现在,这对我来说效果很好,除非我像这样使用 PFItemObject 作为父类(super class):

@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

PFWeaponObject 中的 +itemWithString: 方法的工作原理如下:

+ (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];
}

我认为,因为我是从 PFItemObject 继承的,所以我应该能够毫无问题地为父类(super class)的属性赋值。但是当我执行以下操作时:

- (void)testItemCreationStrings {
NSString *weaponString = @"+1 Greatsword;25;2500;2d6;x3;Melee;5;1;1;YES";
PFWeaponObject *sampleWeapon = [PFWeaponObject itemWithString:weaponString];
}

父类(super class) (PFItemObject) 中的所有属性均返回 @"+1 Greatsword"。我是否错过了一些我应该做的事情?

感谢您提供的任何帮助,如果需要,请随时询问更多信息。

最佳答案

编辑,因为问题已被编辑。我想我有一个解决方案。

试试这个:

+ (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.
}

我认为您没有设置 NSNumberFormatter 的行为和格式,而且我也认为使用它是没有必要的。如果您出于某种原因确实想这样做,还有其他如何使用数字格式化程序的示例。如果您出于某种原因想要/需要,我可以提供指导。

关于objective-c - 继承的属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9909224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com