无法为类型为'(RemoteConfigValue)'的参数列表调用类型'String'的初始化程序

时间:2019-03-29 06:33:41

标签: ios swift xcode firebase google-cloud-firestore

我正在尝试在Firebase上获取字符串(在对象内部)(使用Swift)


let currentDocument = db.collection("countries").document("United States")

        currentDocument.getDocument { (document, error) in
            if let document = document, document.exists {
                let cities = document.data()!["cities"] as? [AnyObject] // This grabs data from a Firebase object named `cities`, inside the object there are arrays that have two pieces of data (e.g. ["cityName" : "New York", "currentTemperature" : 38])
                for i in 0..<cities!.count {
                    let cityName = String(cities![i]["cityName"]!) // Here is where I get the error `Cannot invoke initializer for type 'String' with an argument list of type '(RemoteConfigValue)'`
                }
            } else {
                print("Document does not exist")

            }
        }

搜索此错误后,我发现的常规解决方案类似于these ones

但是即使应用了这些解决方案,例如:


if let cityName = cities![i]["cityName"]! as? String {
  print(cityName)
}

我仍然收到类似Cast from 'RemoteConfigValue' to unrelated type 'String' always fails

的错误

我该如何解决?

2 个答案:

答案 0 :(得分:0)

请阅读documentation

  

public void getAllSms() { Cursor cur; List<Sms> lstSms = new ArrayList<>(); try { Sms objSms; Uri message = Uri.parse("content://sms/"); ContentResolver cr = MainActivity.this.getContentResolver(); cur = cr.query(message, null, null, null, null); this.startManagingCursor(cur); int totalSMS = Objects.requireNonNull(cur).getCount(); if (cur.moveToFirst()) { for (int i = 0; i < totalSMS; i++) { objSms = new Sms(); objSms.setId(cur.getString(cur.getColumnIndexOrThrow("_id"))); objSms.setAddress(cur.getString(cur.getColumnIndexOrThrow("address"))); objSms.setMsg(cur.getString(cur.getColumnIndexOrThrow("body"))); objSms.setReadState(cur.getString(cur.getColumnIndex("read"))); objSms.setTime(cur.getString(cur.getColumnIndexOrThrow("date"))); if (cur.getString(cur.getColumnIndexOrThrow("type")).contains("1")) { objSms.setFolderName("inbox"); } else { objSms.setFolderName("sent"); } lstSms.add(objSms); cur.moveToNext(); } } } catch (Exception e) { e.printStackTrace(); } saveSms(lstSms); }

     

此类为Remote Config参数值提供了包装,并提供了将参数值作为不同数据类型获取的方法。

所以你必须写这样的东西

class RemoteConfigValue : NSObject, NSCopying

答案 1 :(得分:0)

尝试

let currentDocument = db.collection("countries").document("United States")

        currentDocument.getDocument { (document, error) in
            if let document = document, document.exists {
                let cities = document.data()!["cities"] // This grabs data from a Firebase object named `cities`, inside the object there are arrays that have two pieces of data (e.g. ["cityName" : "New York", "currentTemperature" : 38])
                for i in 0..<cities!.count {
                    if let cityName = cities[i]["cityName"]! as? String {
                      print(cityName)
                    }
                }
            } else {
                print("Document does not exist")

            }
        }