Swift NSSet& CoreData [英] Swift NSSet & CoreData

查看:134
本文介绍了Swift NSSet& CoreData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想移动一个客观的C& CoreData应用程序到Swift和iOS,并通过NSSet对象进行迭代打砖块:

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生成了以下类:

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
}

在第三类中,我添加了一个需要通过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
    }
}

上面注释之后的行和错误AnyObject没有成员命名的分数

The line after the comment above gives and error "AnyObject does not have a member named score"

目标-C方法看起来像这样,工作正常:

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);
}

这是关于Swift的第三天,所以我可能会错过一些很明显...但目前我真的不能得到它。

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 !

推荐答案

for aresponse in self.response.question.responses {
    // this next line throws an error
    var scoreVal = aresponse.score.integerValue

问题是,对于...响应中的响应是通过NSSet迭代。 Swift不知道 NSSet中的是什么,因此它将每个值 aresponse 视为AnyObject,它没有属性。因此,当您尝试获取 aresponse score 属性时,编译器会提示。

The problem is that for aresponse in ... responses is iterating through an NSSet. Swift doesn't know what is in the NSSet, so it treats each value aresponse as an AnyObject, which has no properties. Thus, the compiler complains when you try to get the score property of aresponse.

,知道每个值 aresponse 是一个响应。因此,您需要通过投射(使用作为)来 Swift。注意,你实际上在你的Objective-C代码中这样做:

You, on the other hand, know that each value aresponse is a Response. So you need to tell Swift this, by casting (with as). Note that you were in fact doing that in your Objective-C code:

for(Response *aresponse in self.response.question.responses)

但你忘了在Swift中做同样的事情。你可以用各种方式做到;例如你可以在self.response.question.responses中编写这样的事情:

But you have forgotten to do the same thing in Swift. You can do it in various ways; for example you could write this sort of thing:

for aresponse in self.response.question.responses {
    var scoreVal = (aresponse as Response).score.integerValue

我们知道这个变量真的是什么。

That gets us past the compiler, which now shares in our knowledge of what this variable really is.

一旦你习惯了,你就会亲吻Swift的行为。所有这一次,在Objective-C,你是在做什么样的对象,你实际上是一个 id 假设编译器让你发送任何消息给它 - 所以,你会经常崩溃与无法识别的选择器。在Swift中,无法识别的选择器崩溃基本上是不可能的,因为指定了一切的类型,编译器不会让你首先向对象发送不合适的消息。

Once you get used to this, you will be kissing the ground that Swift walks on. All this time, in Objective-C, you were making assumptions about what kind of object you had, but in fact what you had was an id and the compiler was letting you send any message at all to it - and so, you would often crash with "unrecognized selector". In Swift, an "unrecognized selector" crash is basically impossible because the type of everything is specified and the compiler won't let you send an inappropriate message to an object in the first place.

这篇关于Swift NSSet& CoreData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆