<分区>
<分区>
我必须找到一种方法来检查(按一下按钮)文本字段中的文本是否存在于 xml 文件中。
我认为如果我将 xml 文件作为数组加载,那么我可以使用 for 循环来查看它是否与数组中的结果相同。
for 循环根本不实用,你能解释一下我该如何编写解决此类问题的代码吗?
我希望如果对象不存在于整个数组中则显示警报
谢谢
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
cose = [[NSArray alloc] initWithObjects:@"giovanni",@"giulio",@"ciccio",@"panzo", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)prova {
for (NSString *myElement in cose) {
if ([myElement isEqualToString:textfield1.text]) {
label1.text = textfield1.text;
}
else {
UIAlertView *alertCellulare = [[UIAlertView alloc] initWithTitle:@"Attenzione!" message:@"connessione assente" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alertCellulare show];
}
}
}
最佳答案
快速枚举:
-(IBAction)prova
{
BOOL present = NO;
for (NSString *myElement in cose) {
if ([myElement isEqualToString:textfield1.text]) {
label1.text = textfield1.text;
present = YES;
break;
}
}
if (!present){
UIAlertView *alertCellulare = [[UIAlertView alloc] initWithTitle:@"Attenzione!" message:@"connessione assente" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alertCellulare show];
}
}
基于 block 的枚举
-(IBAction)prova
{
__block BOOL present = NO;
[cose enumerateObjectsUsingBlock:^(NSString *name, NSUInteger idx, BOOL *stop){
if ([name isEqualToString:textfield1.text]) {
label1.text = textfield1.text;
present = YES;
*stop = YES;
}
}];
if (!present){
UIAlertView *alertCellulare = [[UIAlertView alloc] initWithTitle:@"Attenzione!" message:@"connessione assente" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alertCellulare show];
}
}
关于ios - for循环与数组ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14788533/