1

I have this code where I populate an array:

func loadObjectsFromRealm() {
    private var myObjects: [MyObjects] = []
    guard let realm = try? Realm() else { return }
    myObjects = realmUtility.getAll(realm)
}

how can I check if the object is nil or not when I am going to use them? For example:

func getFirst() {
var myFirstObj = myObjects[0]    
}

Is there something to check if the object myFirstObject is real or not?

cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
  • I am not much concerned with the issue, but i think guard will not allow the nil object to be store in your realm as it will execute the else block and return. so no need to check for nil latter. Are you getting any nil object?? – Janmenjaya Sep 16 '16 at 06:43
  • If your concern is to check nil You can do by this if (myFirstObj == nil){ print("No object to see here") }else{ print("myFirstObj = \(myFirstObj)"); } – Janmenjaya Sep 16 '16 at 06:49
  • Possible duplicate of [Safe (bounds-checked) array lookup in Swift, through optional bindings?](http://stackoverflow.com/questions/25329186/safe-bounds-checked-array-lookup-in-swift-through-optional-bindings) – HAS Sep 16 '16 at 07:04
  • Could you accept one of these answers atleast? – andromedainiative Apr 30 '18 at 12:17

5 Answers5

3
// Check for specific object with primary key

func objectExist (id: String) -> Bool {
        return realm.object(ofType: MyObject.self, forPrimaryKey: id) != nil
}

// Returns true or false
objectExist(id: "1")
andromedainiative
  • 4,414
  • 6
  • 22
  • 34
1

You can try an usual Swift if statement:

if let myFirstObj = myObjects.first
   // Do anything with the first object
} else {
   print("No first object!")
}

I think that's enough for this case.

Orlando
  • 1,509
  • 2
  • 19
  • 27
1

I wanted to comment on the answer by @andromedainiative, but I don't have enough rep points.

'id' can't be a String - needs to be an Int;

func objectExists(id: Int) -> Bool {
        return realm.object(ofType: MyObject.self, forPrimaryKey: id) != nil
    }

//Returns true of false

objectExists(1)
Joey Slomowitz
  • 179
  • 1
  • 12
  • That is not true at all: https://stackoverflow.com/questions/26152254/how-to-set-primary-key-in-swift-for-realm-model – andromedainiative Apr 30 '18 at 12:51
  • @andromedainiative. Checking the id using an Int is working 100% for me. Were you able to get it working using the String value? I noticed the answer you're referencing is a couple years old. – Joey Slomowitz May 07 '18 at 04:53
0

I tried it following way, you can check like this. Here I have just created a nil object and checked, you can do the same for your object

 let obj :UserProfile? = nil;

    if (obj == nil){
        print("nil objUser");
    }else{
        print("objUser = \(objUser)")
    }

output : nil objUser

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
0

you may check object is really exist before access it

if var myObj: MyObjects = myObjects.indices.contains(index) ? myObjects[index] : nil {
    // myObj is exist
}
larva
  • 4,687
  • 1
  • 24
  • 44