在目标c中使用变量值作为动态代码?

时间:2013-12-22 16:31:50

标签: objective-c nsstring nsarray

我在网上寻找帮助时遇到了一些麻烦,这让我觉得我想做的事情可能无法实现,但无论如何我都会问!我是obj-c的新手,我只是为了一点乐趣而开发......

我试图根据从另一个数组中随机抽取的字符串来调用数组。我有一个名为pickerArray的NSArray来保存字符串:oneArray,twoArray作为示例。

我还有NSString(pickerString),NSArray(oneArray)和NSArray(twoArray)。

我可以随机选择字符串,我不能做的就是将这种刺痛的值用作'代码'

randomNumber = arc4random()%[pickerString count]; 

pickerString应该在代码中读取为oneArray或twoArray,具体取决于字符串
被选中,即

randomNumber = arc4random()%[oneArray count]; 

或:

randomNumber = arc4random()%[twoArray count]; 

取决于所挑选的字符串。

编辑:我当前的代码是:

@implementation FirstViewController{

IBOutlet UITextView *doThis;
}
- (IBAction)takeATurn:(id)sender {
int pickerNumber;
int randomNumber;
NSString *pickerString;
NSString *textString;
NSArray *pickerArray;
NSArray *oneArray;
NSArray *twoArray;

pickerArray = [NSArray arrayWithObjects:
               @"oneArray",
               @"twoArray",

               nil];

oneArray = [NSArray arrayWithObjects:
               @"example text to display",
               @"expmple text to display 2",

               nil];
twoArray = [NSArray arrayWithObjects:
               @"example text to display in array two",
               @"expmple text to display in array two 2",

               nil];

pickerNumber = arc4random()%[pickerArray count];

pickerString = [pickerArray objectAtIndex:pickerNumber];

randomNumber = arc4random()%[NSArray(pickerString) count];
     //does not compile - should read [oneArray count]; (or twoArray in place of oneArray

textString = [NSArray(pickerString) objectAtIndex:randomNumber];
     //same as above.


doThis.text = textString;
}

感谢您的帮助。 麦克

2 个答案:

答案 0 :(得分:2)

这是一个键值编码问题:

NSArray *arrayToUse = [self valueForKey:pickerString];

这将返回由pickerString命名的属性。

要按照您希望的方式工作,请将数组移动到实例属性而不是局部变量。


编辑:

这将是这样的:

@interface FirstViewController ()
@property (nonatomic, readwrite, weak) IBOutlet UITextView *doThis;

@property (nonatomic, readwrite, strong) NSArray *pickerArray;
@property (nonatomic, readwrite, strong) NSArray *oneArray;
@property (nonatomic, readwrite, strong) NSArray *twoArray;
@end

@implementation FirstViewController

// You can put this wherever you initialize the object (init, viewDidLoad, awakeFromNib, etc.)
// viewWillAppear just tends to work for a wide variety of design choices.
- (void)viewWillAppear:(BOOL)animated {
  self.pickerArray = @[
                       @"oneArray",
                       @"twoArray",
                     ];

  self.oneArray = @[
                    @"example text to display",
                    @"expmple text to display 2",
                  ];

  self.twoArray = @[
                    @"example text to display in array two",
                    @"expmple text to display in array two 2",
                  ];
}

- (IBAction)takeATurn:(id)sender {
  int pickerNumber = arc4random_uniform([self.pickerArray count]);
  NSString *pickerString = pickerArray[pickerNumber];
  NSArray *array = [self valueForKey:pickerString];
  int randomNumber = arc4random_uniform(array count]);
  NSString *textString = array[randomNumber];
  self.doThis.text = textString;
}

答案 1 :(得分:0)

您可以将代码块放入方法中并尝试解决方案:

https://stackoverflow.com/a/4446914/584663