[英]Objective C - How check if all elements (NSMutableString) of NSMutableArray has a value?
I store user's answers inside NSMutableString element of array.我将用户的答案存储在数组的 NSMutableString 元素中。 i want to show a pop up (call popGoResultsAlert) when all of the array's element has the value and user would complete the questionary , i search over the internet but didn't succeed to find the solution for that.
当数组的所有元素都具有值并且用户将完成问题时,我想显示一个弹出窗口(调用 popGoResultsAlert),我在互联网上搜索但没有成功找到解决方案。
here is my code :这是我的代码:
in first class在头等舱
[UserData updateUserAnswerValues:[userData objectForKey:@"userAnswerValues"]];
//second class ,part of the userData.m //第二类,userData.m的一部分
static NSMutableArray *userAnswerValues;
+ (NSMutableArray *)getAnswersCompleted {
return userAnswerValues;
}
+ (void)updateUserAnswerValues:(NSArray *)newAnswerValues
{
userAnswerValues = [[NSMutableArray alloc] init];
[userAnswerValues addObjectsFromArray:newAnswerValues];
}
+ (void)updateUserAnswerValueWithIndex:(NSUInteger)index andValue:(NSMutableString*)value
{
[userAnswerValues setObject:value atIndexedSubscript:index];
}
in 3rd class:在第三类:
- (void)viewWillAppear:(BOOL)animated
{
NSMutableArray *userAnswerValues = [UserData getAnswersCompleted];
for (int i=0; i<userAnswerValues.count; i++) {
id object = userAnswerValues[i];// similar to [myArray objectAtIndex:0]
if(object && [object length] !=0 && i!=0)
{
[self popGoResultsAlert];
break;
} else if (!object) {
continue;
}
}
}
Edit : //4th class , here seems the origin data is populated to the userAnswerValues base on what user pick from PickerView , that index has the value and that value is put by selectedOptionValue variable ... I have debug the userAnswerValues and notice the value has 56 elements like userAnswerValues = ( "", "", "", "", "", "", "", "", 4, 3, 4, 3, "", ... (fill the rest with ,"" until 56 items> , "" ); so whenever a user select a value in PickerView one of the above element filled out with index number , so my question is how search through these mutable array and see if all item has value...编辑://4th class ,这里似乎原始数据根据用户从 PickerView 选择的内容填充到 userAnswerValues,该索引具有值并且该值由 selectedOptionValue 变量放置......我已经调试了 userAnswerValues 并注意到该值有 56 个元素,如 userAnswerValues = ( "", "", "", "", "", "", "", "", 4, 3, 4, 3, "", ...(用,"" 直到 56 个项目> , "" ); 所以每当用户在 PickerView 中选择一个值时,上述元素之一用索引号填充,所以我的问题是如何搜索这些可变数组并查看是否所有项目都有价值。 ..
NSUInteger index = [self.currentQuestion.index integerValue];
[UserData updateUserAnswerWithIndex:index andValue:self.selectedOptionIndex];
Option *selectedOption = [self.currentOptions objectAtIndex:[self.selectedOptionIndex integerValue]];
NSString *selectedOptionValue = selectedOption.value;
[UserData updateUserAnswerValueWithIndex:index andValue:selectedOptionValue];
there is 3 section of questionary that store all the answer's value in a single and same array.问题的 3 部分将所有答案的值存储在一个相同的数组中。 with above code , the alert pop up whenever each of 3 section would be completed , my intend is it would only show pop up when all 3 section is completed....
使用上面的代码,每当 3 个部分中的每一个完成时都会弹出警报,我的意图是它只会在所有 3 个部分完成时才显示弹出......
I have test [object length] !=0 , ![object isEqual:[NSNull null]] , ![object isEqualToString:@""]
with no luck , all of the 3 has same behaviour (show pop up after each section is completed ,我有测试
[object length] !=0 , ![object isEqual:[NSNull null]] , ![object isEqualToString:@""]
没有运气,所有 3 有相同的行为(在每个部分后显示弹出完全的 ,
These project has not written by me from the scratch so i am trying to debug and fix some of it's bugs , when i debug userAnswerValues , i see that the items of NSMutableArray had different type in different elements type, as you see item 32th is _NSFConstantString with empty value , while the item 33th is NSTagedPointerString... please see the attached file for more details.这些项目不是我从头开始编写的,所以我试图调试和修复它的一些错误,当我调试 userAnswerValues 时,我看到 NSMutableArray 的项目在不同的元素类型中有不同的类型,正如你看到的第 32 项是 _NSFConstantString值为空,而第 33 项是 NSTagedPointerString...请参阅附件了解更多详情。
What I think you need is very similar to the previous answer.我认为您需要的与之前的答案非常相似。 Here, I'll translate to your data names.
在这里,我将转换为您的数据名称。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
BOOL showPopup = YES; // Assume success
NSMutableArray *userAnswerValues = [UserData getAnswersCompleted];
// Try to go all the way through the array without finding an empty string
for (int i = 0; i < userAnswerValues.count; i++) {
id object = userAnswerValues[i];
if ([object length] == 0) {
showPopup = NO; // Something is not filled in
break;
}
}
if (showPopup) {
[self popGoResultsAlert];
}
}
Bool nilBool = 0;
for (int i = 0 ; i < yourArray.count; i ++)
{
NSString *value = [yourArray ObjectAtIndex:i];
if([value isEqual: @""] || value == nil)
{
nilBool = 1;
}
}
if(nilBool)
{
//one if your values is nil!
}
else
{
//all values are filled, you are good to go
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.