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