-1

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
syedfa
  • 2,801
  • 1
  • 41
  • 74
  • The posted code makes no sense. It's not clear what you are trying to achieve. – Nikolai Ruhe Oct 08 '13 at 15:56
  • And why do you say that? – syedfa Oct 08 '13 at 16:03
  • I would like to check to see if any of the objects, that are of different types, null, and if so, assign a default value to them. – syedfa Oct 08 '13 at 16:06
  • 1
    An *object* cannot be "null" (nor `nil`). A *pointer* can be `nil`, meaning it does not point to an object and thus, there's no way to determine the type. Your question is like asking: "I have a bag of apples, pears and oranges. If I take out none, of what kind would that be?" – Nikolai Ruhe Oct 08 '13 at 16:25

1 Answers1

1

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

for (id widget in myArray)
 {
    if ([widget isKindOfClass:[NSNull class]])
     //do what you need
 }
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161