Question

I have an NSArray that I'm using in my iOS application which is holding data of three types:

NSDate, NSString, and NSNumber

What I would like to do is iterate this NSArray in a for loop to check to see if the objects are null, however, I'm unsure how to do this because the array contains objects of different types instead of one single type. This is what I am thinking of doing:

for (id widget in myArray)
{
    if ([widget isKindOfClass:[NSDate class])
    {
         if (widget == nil) {
            widget = @"";
         }   

    }

    else if ([widget isKindOfClass:[NSString class])
    {
         if (widget == nil) {
            widget = @"";
         }   

    }

    else if ([widget isKindOfClass:[NSNumber class])
    {
         if (widget == nil) {
            widget = @"";
         }   

    }

}

However, I am getting the compilation error: "Fast enumeration variables can't be modified by ARC by default; declare the variable __strong to allow this." I am not sure ahead of time what type the object is going before the iteration, so how do I get around this?

Was it helpful?

Solution

NSArray can't hold nil values. Just check for NSNull

for (id widget in myArray)
 {
    if ([widget isKindOfClass:[NSNull class]])
     //do what you need
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top