title: Swift NSSet & CoreData
body: <p>I am trying to move an objective C &amp; CoreData app to Swift and iOS and hit a brick wall with iterating through NSSet objects: </p>
  
  <p>Xcode has generated these classes:</p>
  
  <pre><code>class Response: NSManagedObject {
      @NSManaged var responseText: String
      @NSManaged var score: NSNumber
      @NSManaged var answers: NSSet
      @NSManaged var question: Question
  }
  
  class Question: NSManagedObject {
      @NSManaged var questionText: String
      @NSManaged var questionType: String
      @NSManaged var qualifier: Qualifier
      @NSManaged var responses: NSSet
  }
  </code></pre>
  
  <p>then in the 3rd class I am adding a method that needs to iterate though an NSSet..</p>
  
  <pre><code>class Answer: NSManagedObject {
  
      @NSManaged var evaluation: Evaluation
      @NSManaged var response: Response
  
      func scoreGap() -&gt; Int {
          var max = 0
          for aresponse in self.response.question.responses {
  
              // this next line throws an error
              var scoreVal = aresponse.score.integerValue
  
              if scoreVal &gt; max { max = scoreVal }
          }
          return max - self.response.score.integerValue
      }
  }
  </code></pre>
  
  <p>The line after the comment above gives and error "AnyObject does not have a member named score"</p>
  
  <p>The Objective-C method looks like this and works fine:</p>
  
  <pre><code>// returns the gap between the score of this answer and the max score
  -(NSNumber *)scoreGap
  {
      long max = 0;
      for(Response *aresponse in self.response.question.responses) {
          if(aresponse.score.longValue &gt; max) max = aresponse.score.longValue;
      }   
      max = max - self.response.score.longValue;
      return @(max);
  }
  </code></pre>
  
  <p>This is about day three on Swift so I may well be missing something really obvious... but at the moment I really don't get it !</p>