无法预览此文件-AppName.app可能已崩溃(SwiftUI)

时间:2019-07-15 22:04:04

标签: ios swift preview swiftui xcode11

如果我在任何结构上使用@EnviromentObject并在PreviewProvider中使用.enviromentObject修饰符,则预览不会加载,并且出现此错误:

  

TwitterSwiftUI.app崩溃:错误域=渲染服务代码= 12“渲染服务被中断” UserInfo = {NSLocalizedDescription =渲染服务被中断}

如果我删除与@EnviromentObject相关的所有内容,则预览工作正常。

struct Options : View {
@EnvironmentObject var tweetManager: TweetManager
var tweet: DBTweet

var containsSelfInRetweets: Bool {
    tweet.retweets.contains {
        $0.retweetUserID == UserDefaults.standard.getDBUser()!.userID
    }
}

var containsSelfInLikes: Bool {
    tweet.likes.contains {
        $0.likerUserID == UserDefaults.standard.getDBUser()!.userID
    }
}

var body: some View {
    HStack {
        //Comments
        HStack {
            // NavigationLink(destination: Text("Comment")) {
                Image(systemName: "bubble.left")
                    .foregroundColor(Color.gray)
                    .padding(.bottom, -2)
            //                }

            Text(tweet.comments.count.toString())
        }

        Spacer()

        // Retweets
        HStack {
            Image(systemName: "arrow.2.squarepath")
                .rotationEffect(Angle(degrees: 90))
                .foregroundColor(containsSelfInRetweets ? Color.green : Color.gray)

            Text(tweet.retweets.count.toString())
        }.tapAction {
            self.tweetManager.addOrDeleteRetweet(tweet: self.tweet)
        }

        Spacer()

        // Likes
        HStack {
            Image(systemName: containsSelfInLikes ? "heart.fill" : "heart")
                .foregroundColor(containsSelfInLikes ? Color.red : Color.gray)

            Text(tweet.likes.count.toString())
        }.tapAction {
            self.tweetManager.addOrDeleteLike(tweet: self.tweet)
        }

        Spacer()

        // Share
        Image(systemName: "square.and.arrow.up")
            .foregroundColor(Color.gray)
            .padding(.bottom, 5)

        Spacer()
    }.padding(.horizontal).padding(.vertical, 5)
}
}

#if DEBUG
struct Options_Previews : PreviewProvider {
    static var previews: some View {
        Options(tweet: tweetData[0])
            .environmentObject(TweetManager())
            .previewLayout(.sizeThatFits)
    }
}
#endif

0 个答案:

没有答案