Chapter 4: Using a list of strings to reference other objects

I’m working through the example with the “stickers” on the car. In the book, they show just one sticker being referenced and printed.

I tried adding a few other stickers. While the print(car.stickers) correctly lists all of the stickers in the list - the print(realm.objects(Sticker.self).filter("id IN %@", car.stickers)) only lists the first sticker.

Is this how it is supposed to work? I would assume that the filter command would end up listing all of the stickers instead of just the first one.

@freak4pc Can you please help with this when you get a chance? Thank you - much appreciated! :]

Check again your code, if you don’t see all the linked stickers maybe you’ve missed something. Here’s how to update the example in Chapter 4 to list 2 stickers instead of one:

Example.of("Referencing objects from a different Realm file") {
  // let's say we're storing those in "stickers.realm"
  let sticker = Sticker("Swift is my life")
  let sticker2 = Sticker("Xcode rocks")

  car.stickers.append(sticker.id)
  car.stickers.append(sticker2.id)

  print(car.stickers)

  try! realm.write {
    realm.add(car)
    realm.add(sticker)
    realm.add(sticker2)
  }

  print("Linked stickers:")
  print(realm.objects(Sticker.self)
    .filter("id IN %@", car.stickers))
}

This outputs:

ℹ️ Referencing objects from a different Realm file:
————————————————————————————————————————————————————————————
List<string> <0x608000045fa0> (
	[0] 05431193-93F4-44EF-BCA1-61437A9BE7A3,
	[1] 0D0914CE-BA34-46AC-ACE4-95D282DCDAF5
)
Linked stickers:
Results<Sticker> <0x7fa9e270dad0> (
	[0] Sticker {
		id = 05431193-93F4-44EF-BCA1-61437A9BE7A3;
		text = Swift is my life;
	},
	[1] Sticker {
		id = 0D0914CE-BA34-46AC-ACE4-95D282DCDAF5;
		text = Xcode rocks;
	}
)

1 Like

Seems like @icanzilb already took care of it :slight_smile: