I am trying to move an objective C & CoreData app to Swift and iOS and hit a brick wall with iterating through NSSet objects:
Xcode has generated these classes:
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
}
then in the 3rd class I am adding a method that needs to iterate though an NSSet..
class Answer: NSManagedObject {
@NSManaged var evaluation: Evaluation
@NSManaged var response: Response
func scoreGap() -> Int {
var max = 0
for aresponse in self.response.question.responses {
// this next line throws an error
var scoreVal = aresponse.score.integerValue
if scoreVal > max { max = scoreVal }
}
return max - self.response.score.integerValue
}
}
The line after the comment above gives and error "AnyObject does not have a member named score"
The Objective-C method looks like this and works fine:
// 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 > max) max = aresponse.score.longValue;
}
max = max - self.response.score.longValue;
return @(max);
}
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 !