访问对象的属性作为方法的参数

时间:2012-12-31 03:27:38

标签: objective-c object methods

我正在学习目标-c,所以请原谅我的无知。我如何使用对象的属性作为该对象的实例方法的参数?例如,我创建了一个对象FamilyMember,其中包含两个参数:name和preference(枚举)。如果我想将这些属性用作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

2 个答案:

答案 0 :(得分:0)

我可以看到你在其方法中使用了object的属性。在这种情况下,您无需传递这些值,因为您可以直接访问它们。


<强>更新

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

答案 1 :(得分: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];

  

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

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