図形に関する簡単なコマンド ライン アプリを作成しています。辺の色と数を変数にして、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]);
}