title: How do I pass a swift object to javascript (WKWebView / swift)
body: <p>I am working on passing data from swift to javascript inside a WKWebView</p>
  
  <p>I have a custom class:</p>
  
  <pre><code>class AllInfo: AnyObject {
      var title = "Special Title"
      var description = "Special Description"
  }
  </code></pre>
  
  <p>and initialize it with</p>
  
  <pre><code>var info = AllInfo()
  </code></pre>
  
  <p>I then have a WKWebView with which i pass a WKUserScript with which I have a source property of:</p>
  
  <pre><code>source: "changeDisplay('\(info)')"
  </code></pre>
  
  <p>My problem is how do I access this object in the javascript. I have attempted to access it like a javascript object as well as associative array with no luck. Here's the js funciton:</p>
  
  <pre><code>function changeDisplay(passedInfo) {
      document.querySelector('h1').innerHTML = passedInfo.title
      document.querySelector('h2').innerHTML = passedInfo.description
  }
  setTimeout(function () {changeDisplay();}, 5000);
  </code></pre>
  
  <p>EDIT: When I do attempt to access the object like this, I get undefined.</p>
  
  <p>So my questions are:</p>
  
  <p><strong>Can I pass an AnyObject to JavaScript and access it?
  If not, what type should I make the swift class so that I can easily pass it.</strong></p>
  
  <p>I am tempted to just create a javascript object in swift as a string and pass that, but I feel there's a better way.</p>
  
  <p>Thanks</p>
  
  <p>EDIT: I answered how I was able to pass data as JSON below.</p>