2

我正在学习objective-c,所以请原谅我的无知。如何使用对象的属性作为该对象的实例方法的参数?例如,我创建了一个带有两个参数的对象 FamilyMember,名称和首选项(一个枚举)。如果我想将这些属性用作dinnerPrefMethod 方法的参数,该怎么做。这是我目前所拥有的,如您所见,如果我想调用该方法,我必须手动输入该人的偏好。我的目标是能够调用该方法,并将对象的两个属性作为参数。

@implementation FamilyMember

@synthesize name, preference;


-(void) dinnerPrefMethod : (dinnerPreference) x; {
    NSLog(@"Lets see what %@ wants for dinner", name);
    switch( x )
{
    case pizza:
        NSLog(@"%@ wants pizza", name);
        break;
    case steak:
        NSLog(@"%@ wants steak", name);
        break;
    case seafood:
        NSLog(@"%@ wants seafood", name);
        break;
    case dunno:
        NSLog(@"%@ doesn't know", name);
        break;
}
}
@end
4

2 回答 2

0

正如我所看到的,您在其方法中使用了对象的属性。在这种情况下,您不必传递这些值,因为您可以直接访问它们。


更新

-(void) dinnerPrefMethod {
    NSLog(@"Lets see what %@ wants for dinner", self.name);
    switch(self.preference)
    {
    case pizza:
        NSLog(@"%@ wants pizza", self.name);
        break;
    case steak:
        NSLog(@"%@ wants steak", self.name);
        break;
    case seafood:
        NSLog(@"%@ wants seafood", self.name);
        break;
    case dunno:
        NSLog(@"%@ doesn't know", self.name);
        break;
    }
}
于 2012-12-31T03:49:26.873 回答
0

如果我想将这些属性用作dinnerPrefMethod 方法的参数,该怎么做。[...] 我的目标是能够调用该方法,并将对象的两个属性作为参数。

听起来您想避免这样做:

FamilyMember *familyMember = [[FamilyMember alloc] init];
[familyMember setName:@"First Last"];
[familyMember setPreference:dunno];
[familyMember announceDinnerPreference];

-[FamilyMember announceDinnerPreference]您的修改版本在哪里dinnerPrefMethod:

- (void)announceDinnerPreference
{
    NSLog(@"Let's see what %@ wants for dinner...", [self name]);
    switch ([self preference]) {
        case pizza:
            NSLog(@"%@ wants pizza.", [self name]);
            break;
        case steak:
            NSLog(@"%@ wants steak.", [self name]);
            break;
        case seafood:
            NSLog(@"%@ wants seafood.", [self name]);
            break;
        case dunno:
            NSLog(@"%@ doesn't know.", [self name]);
            break;
    }
}

也许你想定义一个方法-[FamilyMember setName:preference:announceDinnerPreference:]

- (void)setName:(NSString *)name preference:(dinnerPreference)preference announceDinnerPreference:(BOOL)announce
{
    [self setName:name];
    [self setPreference:preference];
    if (announce) {
        [self announceDinnerPreference];
    }
}

这将允许你写

FamilyMember *familyMember = [[FamilyMember alloc] init];
[familyMember setName:@"First Last" preference:dunno announceDinnerPreference:YES];

不过,坦率地说,这在风格上非常奇怪。


一种更时尚的方法是定义-initWithName:preference:

- (id)initWithName:(NSString *)name preference:(dinnerPreference)preference
{
    self = [super init];
    if (self) {
        [self setName:name];
        [self setPreference:preference];
    }
    return self;
}

然后你就可以写了

FamilyMember *familyMember = [[FamilyMember alloc] initWithName:@"First Last" preference:dunno];
[familyMember announceDinnerPreference];

所以我可以在方法中使用对象的属性而不说它们是参数吗?

是的。对象的实例变量可以在其实例方法中访问。

于 2012-12-31T03:50:51.410 回答