2

I have a PersonsArray: NSMutableArray = [NSNull, NSNull, NSNUll, NSNull, NSNull, NSNUll, NSNull]. I needed seven slots that I then can fill with an Entity CoreData entry as AnyObject.

I need to do a for in loop on this NSMutableArray...

If the index slot is NSNull I want to pass to next index slot, if index slot is filled with my Object I want to execute code on this Object.


example PersonsArray: NSMutableArray = [
    NSNull,
    NSNull,
    NSNull,
    "<iswift.Person: 0x7f93d95d6ce0> (entity: Person; id: 0xd000000000080000 <x-coredata://8DD0B78C-C624-4808-9231-1CB419EF8B50/Person/p2> ; data: {\n    image = nil;\n    name = dustin;\n})",
    NSNull,
    NSNull,
    NSNull
]

Attempting

for index in 0..<PersonsArray.count {
        if PersonsArray[index] != NSNull {println(index)}
}

suggests a bunch of changes that don't work either, like

if PersonsArray[index] as! NSNull != NSNull.self {println(index)}

or

if PersonsArray[index] as! NSNull != NSNull() {println(index)}

NOTE: using NSNull is just a placeholder in NSMutableArray so that its count is ALWAYS 7 and I can replace any of the (7)slots with an Object. Should I be using something other than NSNull as my placeholder?

Chameleon
  • 1,608
  • 3
  • 21
  • 39
  • `NSNull` is probably defined as always returning `false` from any `==` or `!=` comparison. That's how null does in most places... – nhgrif Apr 29 '15 at 21:36

1 Answers1

5

NSNull() is a singleton object, therefore you can simply test if the array element is an instance of NSNull:

if personsArray[index] is NSNull { ... }

or use the "identical to" operator:

if personsArray[index] === NSNull() { ... }

Alternatively, you could use an array of optionals:

let personsArray = [Person?](count: 7, repeatedValue: nil)
// or more verbosely:
let personsArray : [Person?] = [ nil, nil, nil, nil, nil, nil, nil ]

using nil for the empty slots.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Wouldn't `is NSNull` work even if it weren't a singleton object? Doesn't it follow that because it's a singleton object that `===` would probably work (compare references?). – nhgrif Apr 29 '15 at 21:36
  • Since it's a singleton you can test if the array element `== [NSNull null]` (in Objective-C notation). – Hot Licks Apr 29 '15 at 21:36
  • @HotLicks Objective-C's `== [NSNull null]` is a reference comparison. `==` in Swift would be more like doing `isEqual:[NSNull null]]` in Objective-C, but `===` is Swift's reference comparison. – nhgrif Apr 29 '15 at 21:38
  • @nhgrif: I was just about to add the `===` version. – Martin R Apr 29 '15 at 21:41
  • turns out I was populating original PersonsArray = [[NSNull.self, NSNull.self, NSNull.self, NSNull.self, NSNull.self, NSNull.self,NSNull.self] and it isn't equal to PersonsArray = [NSNull(),NSNull(),NSNull(),NSNull(),NSNull(),NSNull(),NSNull()] – Chameleon Apr 29 '15 at 21:48
  • @Dustin: `NSNull.self` is the *class* (or *type*) object,that is probably not what you want. `NSNull()` is the "null object". – Martin R Apr 29 '15 at 21:49
  • @Martin R trying PersonsArray = [nil, nil, nil, nil, nil, nil, nil, nil, nil] produces error "Type 'AnyObject' does not conform to protocol 'NilLiteralConvertible'"... so just to be certain...Using NSNull() as my placeholder is okay or not... the `if personsArray[index] === NSNull() { ... }` works with NSNull() as placeholder – Chameleon Apr 29 '15 at 21:50
  • @Dustin: You cannot use NSMutableArray to store nils. Try the definition at the end of my answer. – Martin R Apr 29 '15 at 21:51
  • === NSNull() works, is there a good reason to use nil instead? – Chameleon Apr 29 '15 at 22:08
  • @Dustin You must use `NSNull()` if you're using an `NSArray`/`NSMutableArray()`. You can use `nil` if you have a Swift array of an optional type. – nhgrif Apr 29 '15 at 23:53
  • @Dustin: `nil` is the Swift way of expressing "no value". The advantage would be that you have a typed array `[Person?]`, so that the *compiler* can already check that only `Person` objects (or nil) is stored. In an `NSMutableArray` you can store almost anything. – Martin R Apr 30 '15 at 05:45
  • @MartinR would that solve my issue here http://stackoverflow.com/questions/29956065/how-to-deal-with-nsmutablearray-containing-coredata-entry-after-entry-is-deleted/29957579#29957579 – Chameleon Apr 30 '15 at 06:02
  • @Dustin: No, I don't think so. *This* question is about how to store "nothing" in an array. *That* question is about Core Data. – Martin R Apr 30 '15 at 06:04