【问题标题】:Class calculating a variable计算变量的类
【发布时间】:2013-09-28 08:45:55
【问题描述】:

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

【问题讨论】:

  • 我们都应该查看我们的水晶球,还是您愿意提供代码?
  • 您能在此处发布带有该错误的代码 sn-p 吗?
  • 请您解释一下问题所在。你不能修改 Shape 类还是什么?

标签: objective-c class-variables


【解决方案1】:

如果你需要计算名字,你可以写这样的东西。 形状+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

而且题外话,使用 switch 运算符比使用 ifs 更好

【讨论】:

    猜你喜欢
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    相关资源
    最近更新 更多