将NSNumber更改为Int/Double后出现错误 [英] Errors after changing NSNumber to Int/Double

查看:74
本文介绍了将NSNumber更改为Int/Double后出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个课程可以从Firestore获取数据:

  struct Spty:可识别{var id:字符串var spty:字串var r:NSNumbervar g:NSNumbervar b:NSNumber}SptyViewModel类:NSObject,ObservableObject {@Published var specialties = [Spty]()@Published var search ="func fetchData(){让db = Firestore.firestore()db.collection("specialities").addSnapshotListener {(querySnapshot,error)在保护文档= querySnapshot else {返回}self.specialities = documents.documents.compactMap {(doc)->卑鄙的?在设id = doc.documentID如果让spty = doc.get("spty")为?细绳,令r = doc.get("r")为?NSNumber,令g = doc.get("g")为?NSNumber,令b = doc.get("b")为?NSNumber {返回Spty(id:id,spty:spty,r:r,g:g,b:b)}别的{返回零}}}}} 

然后我使用ForEach显示了它:

  ForEach(sptyModel.specialities){spty inNavigationLink(目的地:More()){HStack {文字(spty.spty).foregroundColor(.yellow).frame(width:UIScreen.main.bounds.width-30).frame(高度:105).background(Color(UIColor(红色:CGFloat(截断:spty.r)/255.0,绿色:CGFloat(截断:spty.g)/255.0,蓝色:CGFloat(截断:spty.b)/255.0,alpha:1.0))).cornerRadius(38).padding(.bottom,20)}}} 

由于需要使用 Codable ,我需要将 NSNumber 更改为 Int/Double .只需在SptyViewModel类中对其进行更改,我在 ForEach

中遇到以下错误

我该怎么解决

解决方案

您的问题不是 ForEach ,而是 .background(...)

CGFloat(截断:NSNumber)仅接受NSNumber.

  ForEach(sptyModel.specialities){spty inNavigationLink(目的地:文本("MORE"))){HStack {文字(spty.spty).foregroundColor(.yellow).frame(width:UIScreen.main.bounds.width-30).frame(高度:105).background(Color(UIColor(红色:CGFloat(截断:NSNumber(值:spty.r)))/255.0,绿色:CGFloat(截断:NSNumber(值:spty.g))/255.0,蓝色:CGFloat(截断:NSNumber(值:spty.b))/255.0,alpha:1.0))).cornerRadius(38).padding(.bottom,20)}}} 

I have this class to get data from Firestore:

 struct Spty: Identifiable{
  var id: String
  var spty: String  
  var r: NSNumber
  var g: NSNumber
  var b: NSNumber

}

class SptyViewModel: NSObject, ObservableObject{
  @Published var specialities = [Spty]()
  @Published var search = ""
   func fetchData(){
    let db = Firestore.firestore()
    db.collection("specialities").addSnapshotListener { (querySnapshot, error) in
        guard let documents = querySnapshot else {return }
        self.specialities = documents.documents.compactMap { (doc) -> Spty? in
           let id = doc.documentID
          if   let spty = doc.get("spty") as? String,
            let r = doc.get("r") as?  NSNumber,
            let g = doc.get("g") as?  NSNumber,
            let b = doc.get("b") as?  NSNumber{
            
            return Spty(id: id, spty: spty, r: r , g: g , b: b )
            }
            else{
                return nil
            }
        }
    }
}

 }

And I Displayed it using ForEach:

     ForEach(sptyModel.specialities){ spty in
         NavigationLink(destination: More()) {
                       HStack{
                                
                         Text(spty.spty).foregroundColor(.yellow)
                           .frame(width: UIScreen.main.bounds.width - 30)
                           .frame(height: 105)
                           .background(Color(UIColor(red: CGFloat(truncating: spty.r) / 255.0, green: CGFloat(truncating: spty.g) / 255.0, blue: CGFloat(truncating: spty.b) / 255.0, alpha: 1.0)))
                           .cornerRadius(38)
                           .padding(.bottom, 20)
                            }       
                    }
                }

Because of the necessity of the usage of Codable, I need to change NSNumber to Int/Double. After simply just changing it in the class SptyViewModel, I got the following errors in ForEach

How can I solve them

解决方案

Your problem is not with the ForEach , but the .background(...)

CGFloat(truncating:NSNumber) accept NSNumber only .

ForEach(sptyModel.specialities){ spty in
                NavigationLink(destination: Text("MORE")) {
                    HStack{
                        Text(spty.spty).foregroundColor(.yellow)
                            .frame(width: UIScreen.main.bounds.width - 30)
                            .frame(height: 105)
                            .background(Color(UIColor(red: CGFloat(truncating: NSNumber(value: spty.r)) / 255.0, green: CGFloat(truncating: NSNumber(value: spty.g)) / 255.0, blue: CGFloat(truncating: NSNumber(value: spty.b)) / 255.0, alpha: 1.0)))
                            .cornerRadius(38)
                            .padding(.bottom, 20)
                    }
                }
            }

这篇关于将NSNumber更改为Int/Double后出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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