title: LinkingObjects one to many in Realm
body: <p>I have stop which have many directions. I've managed to do it using function 'linkingObjects' but since last update it's deprecated and I should use object 'LinkingObjects'. </p>
  
  <p><strong>Stop</strong></p>
  
  <pre><code>class Stop: Object {
      dynamic var name:String = ""
      var directions = List&lt;Direction&gt;()
  
      override static func primaryKey() -&gt; String? {
          return "name"
      }
  }
  </code></pre>
  
  <p><strong>Old Direction</strong></p>
  
  <pre><code>class Direction: Object {
      dynamic var tag:String = ""
      var stop:Stop? {
          return linkingObjects(Stop.self, forProperty: "directions").first
      }
  }
  </code></pre>
  
  <p>When I apply my previous approach to new object then I always get <code>nil</code> </p>
  
  <p><strong>New Direction with nil returned by LinkingObjects</strong></p>
  
  <pre><code>class Direction: Object {
      dynamic var tag:String = ""
      let stop = LinkingObjects(fromType: Stop.self, property: "directions").first //always return nil
  }
  </code></pre>
  
  <p>But here I'm getting array with one element. So it works as it should.</p>
  
  <p><strong>New Direction with nil returned by LinkingObjects</strong></p>
  
  <pre><code>class Direction: Object {
      dynamic var tag:String = ""
      let stops = LinkingObjects(fromType: Stop.self, property: "directions")
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Question</strong></p>
  
  <p>Is there other way to use 'LinkingObjects' rather than this last example because using in every time 'direction.stop.first?.name' instead of 'direction.stop?.stop'? </p>
  
  <p>Of course, I could use in 'direction' function that will take always pick first element in 'stops' but maybe I don't have to.</p>
  
  <h2>UPDATE</h2>
  
  <p>In meanwhile I’m using this. This isn’t ideal solution, but works.</p>
  
  <pre><code>private let stops:LinkingObjects&lt;Stop&gt; = LinkingObjects(fromType: Stop.self, property: "directions")
  var stop:Stop? {
      return self.stops.first
  }
  </code></pre>