0

我正在制作一个关于形状的简单命令行应用程序。我已经制作了颜色和边数变量,并在 if 语句中使它们成为可设置的属性。我需要能够获取形状的名称,但不能将其设置为变量。所以类需要计算并返回名称,而不是它是一个可设置的属性。

你能否告诉我最好的方法来做到这一点,因为我不确定。我尝试将 if 语句放在实现文件中,但 XCode 不喜欢它。

任何建议都会被采纳。

类头文件:

@interface shape : NSObject

@property (nonatomic, strong) NSString *numberOfSides;
@property (nonatomic, strong) NSString *name;
@property int *sides;

@property (nonatomic, strong) NSString *colour;

void waitOnCR (void);

Main.m 
#import <Foundation/Foundation.h>
#import "shape.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        //Array of colours thats index is randomly selected from when the program is run
        NSArray *colours = [NSArray arrayWithObjects:@"Red",@"Blue",@"Green", @"Yellos", @"Ornage", @"Purple", @"Pink", nil];
        NSUInteger random = arc4random() % [colours count];

        NSLog(@"Enter a number from between 3-8");

              float user;

        scanf("%f" , &user);

        if (user == 3)
        {
            shape *myShape = [[shape alloc]init];
            [myShape setSides:@3];
            [myShape setColour:@"Red"];

            NSLog(@"The %@ shape has %@ and is called a %@", [myShape colour], [myShape sides], [myShape name]);
        }
        else if (user == 4)
        {
            shape *myShape = [[shape alloc]init];
            [myShape setSides:@4];
            [myShape setColour:@"Blue"];

            NSLog(@"The %@ shape has %@ and is called a %@", [myShape colour], [myShape sides], @"Square");
        }
        else if (user == 5)
        {
            shape *myShape = [[shape alloc]init];
            [myShape setName:@"Pentagon"];
            [myShape setNumberOfSides:@"5"];

            NSLog(@"The %@ shape has %@ and is called a %@", [colours objectAtIndex:random], [myShape numberOfSides], [myShape name]);

        }
        else if (user == 6)
        {
            shape *myShape = [[shape alloc]init];
            [myShape setName:@"Hexagon"];
            [myShape setNumberOfSides:@"6"];

            NSLog(@"The %@ shape has %@ and is called a %@", [colours objectAtIndex:random], [myShape numberOfSides], [myShape name]);

        }
        else if (user == 7)
        {
            shape *myShape = [[shape alloc]init];
            [myShape setName:@"Heptagon"];
            [myShape setNumberOfSides:@"7"];

            NSLog(@"The %@ shape has %@ and is called a %@", [colours objectAtIndex:random], [myShape numberOfSides], [myShape name]);
        }
        else if (user == 8)
        {
            shape *myShape = [[shape alloc]init];
            [myShape setName:@"Octagon"];
            [myShape setNumberOfSides:@"8"];

            NSLog(@"The %@ shape has %@ and is called a %@", [colours objectAtIndex:random], [myShape numberOfSides], [myShape name]);
    }
4

1 回答 1

0

如果你需要计算名字,你可以写这样的东西。形状+可计算名称.h

#import "Shape.h"

@interface Shape (CalculableName)

@property (nonatomic, strong) NSString *calculatedName;

@end

形状+可计算名称.m

#import "Shape+CalculableName.h"

@implementation Shape (CalculableName)

- (NSString *)calculatedName {
    if (self.name) {
        return self.name;
    }
    return [NSString stringWithFormat:@"%@ %@", self.colour, self.numberOfSides ];
}

@end

和题外话,使用 switch 运算符比使用 ifs 更好的做法

于 2013-09-23T13:50:40.107 回答