计算变量的类

时间:2013-09-23 12:41:29

标签: objective-c class-variables

我正在创建一个关于形状的简单命令行应用程序。我已经制作了边变量的颜色和数量,并在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]);
    }

1 个答案:

答案 0 :(得分:0)

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

#import "Shape.h"

@interface Shape (CalculableName)

@property (nonatomic, strong) NSString *calculatedName;

@end

形状+ CalculableName.m

#import "Shape+CalculableName.h"

@implementation Shape (CalculableName)

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

@end

并且offtopic,使用switch操作符比使用ifs

更好