To retrieve values from an NSMutableArray where each object contains multiple values, you can use nested loops as you’ve attempted. However, the syntax for accessing elements of NSMutableArray and NSString is a bit different. Here’s the correct way to do it:
objectivefor (int i = 0; i < [array count]; i++) { NSArray *subArray = array[i]; // Assuming each object is an array of NSStrings for (int j = 0; j < [subArray count]; j++) { NSString *value = subArray[j]; NSLog(@"Value at index %d in object %d: %@", j, i, value); } }
This code assumes that array
is your NSMutableArray, and each object in array
is an NSArray containing NSString values. If your objects are not arrays, you might need to adjust the code accordingly based on the actual structure of your NSMutableArray.
المزيد من المعلومات
بالتأكيد! في Objective-C، يمكنك استخدام الدوال المتاحة في الكلاس NSArray
للوصول إلى عناصر المصفوفة الداخلية. على سبيل المثال، يمكنك استخدام الدالة objectAtIndex:
للوصول إلى العنصر في موضع محدد داخل المصفوفة الداخلية. إليك مثال:
objectivefor (int i = 0; i < [array count]; i++) { NSArray *subArray = [array objectAtIndex:i]; for (int j = 0; j < [subArray count]; j++) { NSString *value = [subArray objectAtIndex:j]; NSLog(@"Value at index %d in object %d: %@", j, i, value); } }
يمكنك أيضًا استخدام الدالة objectAtIndexedSubscript:
بدلاً من objectAtIndex:
بشكل مباشر كما في المثال التالي:
objectivefor (int i = 0; i < [array count]; i++) { NSArray *subArray = array[i]; for (int j = 0; j < [subArray count]; j++) { NSString *value = subArray[j]; NSLog(@"Value at index %d in object %d: %@", j, i, value); } }
هذه الطريقتان متشابهتان في النتيجة، لكن الطريقة الثانية تستخدم عملية فهرسة النص المؤشري لتكون أكثر قراءة وفهمًا.