Xcode:如何将其放入循环? [英] Xcode: How do I put this into a loop?

查看:95
本文介绍了Xcode:如何将其放入循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对xcode相当陌生,并试图使我的代码更高效.我有下面的代码,我想将重复的行末尾放入一个循环(数组中还有很多项目).我敢肯定这很简单,但是我不知道如何在循环中求值.显然,我也问Google错误的问题,因为它无济于事!

I am fairly new to xcode and trying to make my code more efficient. I have the code below and I want to put the repeated lines at the end into a loop (there are many more items in the array). I am sure it's very simple but I can't figure out how to evaluate the variables within the loop. I am obviously asking Google the wrong question too as it isn't helping!

GlossaryDetailViewController *dController = segue.destinationViewController;
glossaryDict = glossaryArray [indexPath.row];
dController.detailLabelText = [glossaryDict objectForKey:@"Explanation"];
dController.detailTitle = [glossaryDict objectForKey:@"Term"];
NSMutableArray *labelArray;
labelArray = [glossaryDict objectForKey:@"Label"];
dController.labelString0 = labelArray[0];
dController.labelString1 = labelArray[1];

我知道如何创建循环,即for(int i = 0; i

任何指针都值得赞赏.谢谢!

I know how to create the loop ie for(int i=0;i

Any pointers much appreciated. Thanks!

推荐答案

如果您要问如何使它们自动化:

If you are asking how to automate these :

dController.labelString0 = labelArray[0];
dController.labelString1 = labelArray[1];
dController.labelStringxx=labelArray[xx];

那么您有两种方法:

对于GlossaryDetailViewController

for(NSInteger i=1; i<labelArray.count; i++){
    dController.labelString[i]=labelArray[i];
}

或者,使用键值编码(如果限制您不要更改类别):

Or, use Key Value Coding (if you are restricted not to change the class):

for (NSInteger i=1; ;i++) { //still here you need to put 1 
    NSString *propertyName=[NSString stringWithFormat:@"labelString%d",i];
    if([dController respondsToSelector:NSSelectorFromString(propertyName)]){
        [dController setValue:labelArray[i] forKey:propertyName];
    }
    else break;
}

这篇关于Xcode:如何将其放入循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆