我有这个数组:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *firstname = [defaults objectForKey:kFirstName];
NSString *middlename = [defaults objectForKey:kMiddleName];
NSString *lastname = [defaults objectForKey:kLastName];
NSString *gender = [defaults objectForKey:kGender];
NSString *bloodtype = [defaults objectForKey:kBloodType];
NSString *civilstatus = [defaults objectForKey:kCivilStatus];
NSString *nationality = [defaults objectForKey:kNationality];
NSArray *nameArray = [NSArray arrayWithObjects:firstname, middlename, lastname, gender, bloodtype, civilstatus, nationality, nil];
我想在数组中创建一个循环,以检查NSString对象是否为空或具有值。
目前,我有一个不使用数组的这个凌乱的if语句:
if (([firstname length] || [middlename length] || [lastname length] || [gender length] || [bloodtype length] || [civilstatus length] || [nationality length]) == 0)
{
NSLog(@"test1");
}
我该怎么做?提前谢谢!
答案 0 :(得分:0)
您可以在阵列上使用enumerateObjectsUsingBlock
,这对于此类任务非常方便:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/enumerateObjectsUsingBlock:
或者只是使用快速枚举:
for (NSString *item in nameArray)
if ([item length])
NSLog(@"test1");