Swift“无法将''类型的值转换为'String'类型
Posted
技术标签:
【中文标题】Swift“无法将\'\'类型的值转换为\'String\'类型【英文标题】:Swift "Cannot convert value of type ' ' to type ' String 'Swift“无法将''类型的值转换为'String'类型 【发布时间】:2020-07-04 12:41:19 【问题描述】:我知道我显然做错了什么,但我不知道如何解决这个问题。 我正在从我的 firebase 数据库中获取 Int 值,并希望在文本标签中显示它们。
但是我得到一个错误,说我的值是 nil 或者我的值不能被转换。
也许有人可以弄清楚如何实现我想做的事情。
我在以下行收到错误:
cell.itemValueLabel.text = ahpPrio
感谢大家的帮助。 :)
//
// AHPfinalPreferencesViewController.swift
// ProLabArtv2
//
// Created by Manu on 30.06.20.
// Copyright © 2020 Manuel Knott. All rights reserved.
//
import UIKit
class AHPfinalPreferencesViewController: UIViewController
// MARK: - Properties
@IBOutlet weak var tableView: UITableView!
var ahpPrios = [AHPPriority]()
let itemsForRating = ["Ability", "Accuracy", "Planning", "Quality of Work", "Productivity", "Punctuality", "Responsibility", "Job_Knowledge", "Creativity", "Initiative", "Job Commitment", "Organization Loyalty", "Supervisor Loyalty", "Dependability", "Honesty", "Cooperation", "Attitude", "Communication_Skils", "Judgment", "Interpersonal Relationship"]
override func viewDidLoad()
super.viewDidLoad()
UserService.ahpPref(for: User.current) (ahpPrios) in
self.ahpPrios = ahpPrios
self.tableView.reloadData()
// MARK: - Element Style
func configureTableView()
// remove separators for empty cells
tableView.tableFooterView = UIView()
// remove separators from cells
tableView.separatorStyle = .none
// MARK: - UITableViewDataSource
extension AHPfinalPreferencesViewController: UITableViewDataSource
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
let ahpPrio = ahpPrios[indexPath.row]
let cell = tableView.cellForRow(at: indexPath)
print(ahpPrios)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return ahpPrios.count
func numberOfSections(in tableView: UITableView) -> Int
return 1
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let ahpPrio = ahpPrios[indexPath.row]
let ratingItemT = itemsForRating[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "AHPPrefTableViewCell") as! AHPPrefTableViewCell
// cell.update(ahpPrio)
cell.itemPrefLabel.text = ratingItemT
cell.itemValueLabel.text = ahpPrio
// cell.nameLabel.text = ahpPrios[indexPath.row].memberName
return cell
// MARK: - UITableViewDelegate
extension AHPfinalPreferencesViewController: UITableViewDelegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
return 80
//
// AHPService.swift
// ProLabArtv2
//
// Created by Manu on 02.07.20.
// Copyright © 2020 Manuel Knott. All rights reserved.
//
import Foundation
import FirebaseDatabase
import FirebaseUI
import FirebaseAuth
struct AHPService
static let dateFormatter = ISO8601DateFormatter()
static func createAHP(forUser firUser: String)
let ajkpXc:Int = GLajkpXc
let ajkpXijr: Int = GLajkpXijr
let ajkpXqpa: Int = GLajkpXqpa
let ajkpXcoj: Int = GLajkpXcoj
let ajkpXlos: Int = GLajkpXlos
let ajkpXiracc: Int = GLajkpXiracc
let ajkpXdph: Int = GLajkpXdph
let cXijr: Int = GLcXijr
let cXqpa: Int = GLcXqpa
let cXcoj: Int = GLcXcoj
let cXlos: Int = GLcXlos
let cXiracc: Int = GLcXiracc
let cXdph: Int = GLcXdph
let ijrXqpa: Int = GLijrXqpa
let ijrXcoj: Int = GLijrXcoj
let ijrXlos: Int = GLijrXlos
let ijrXiracc: Int = GLijrXiracc
let ijrXdph: Int = GLijrXdph
let qpaXcoj: Int = GLqpaXcoj
let qpaXlos: Int = GLqpaXlos
let qpaXiracc: Int = GLqpaXiracc
let qpaXdph: Int = GLqpaXdph
let cojXlos: Int = GLcojXlos
let cojXiracc: Int = GLcojXiracc
let cojXdph: Int = GLcojXdph
let losXiracc: Int = GLlosXiracc
let losXdph: Int = GLlosXdph
let iraccXdph: Int = GLiraccXdph
let timestamp = dateFormatter.string(from: Date())
let ahpPriorities = AHPPriority(ajkpXc: ajkpXc, ajkpXijr: ajkpXijr, ajkpXqpa: ajkpXqpa, ajkpXcoj: ajkpXcoj, ajkpXlos: ajkpXlos, ajkpXiracc: ajkpXiracc, ajkpXdph: ajkpXdph, cXijr: cXijr, cXqpa: cXqpa, cXcoj: cXcoj, cXlos: cXlos, cXiracc: cXiracc, cXdph: cXdph, ijrXqpa: ijrXqpa, ijrXcoj: ijrXcoj, ijrXlos: ijrXlos, ijrXiracc: ijrXiracc, ijrXdph: ijrXdph, qpaXcoj: qpaXcoj, qpaXlos: qpaXlos, qpaXiracc: qpaXiracc, qpaXdph: qpaXdph, cojXlos: cojXlos, cojXiracc: cojXiracc, cojXdph: cojXdph, losXiracc: losXiracc, losXdph: losXdph, iraccXdph: iraccXdph)
let currentUser = User.current
let dict = ahpPriorities.dictValue
let ref = Database.database().reference().child("AHPRatings").child(currentUser.uid).childByAutoId()
ref.setValue(dict)
// UserService.swift
// ProLabArtv2
//
// Created by Manu on 20.05.20.
// Copyright © 2020 Manuel Knott. All rights reserved.
//
import Foundation
import FirebaseAuth.FIRUser
import FirebaseDatabase
import FirebaseUI
import FirebaseAuth
struct UserService
static let dateFormatter = ISO8601DateFormatter()
static func ahpPref(for user: User, completion: @escaping ([AHPPriority]) -> Void)
let ref = Database.database().reference().child("AHPRatings").child(user.uid)
ref.observe(DataEventType.value, with: (snapshot) in
guard let snapshot = snapshot.children.allObjects as? [DataSnapshot] else
return completion([])
let ahpPrios = snapshot.reversed().compactMap(AHPPriority.init)
completion(ahpPrios)
)
//
// AHPPriority.swift
// ProLabArtv2
//
// Created by Manu on 21.06.20.
// Copyright © 2020 Manuel Knott. All rights reserved.
//
import Foundation
import UIKit
import FirebaseDatabase.FIRDataSnapshot
class AHPPriority
var key: String?
let creationDate: Date
let ajkpXc: Int
let ajkpXijr: Int
let ajkpXqpa: Int
let ajkpXcoj: Int
let ajkpXlos: Int
let ajkpXiracc: Int
let ajkpXdph: Int
let cXijr: Int
let cXqpa: Int
let cXcoj: Int
let cXlos: Int
let cXiracc: Int
let cXdph: Int
let ijrXqpa: Int
let ijrXcoj: Int
let ijrXlos: Int
let ijrXiracc: Int
let ijrXdph: Int
let qpaXcoj: Int
let qpaXlos: Int
let qpaXiracc: Int
let qpaXdph: Int
let cojXlos: Int
let cojXiracc: Int
let cojXdph: Int
let losXiracc: Int
let losXdph: Int
let iraccXdph: Int
init(ajkpXc: Int, ajkpXijr: Int, ajkpXqpa: Int, ajkpXcoj: Int, ajkpXlos: Int, ajkpXiracc: Int, ajkpXdph: Int, cXijr: Int, cXqpa: Int, cXcoj: Int, cXlos: Int, cXiracc: Int, cXdph: Int, ijrXqpa: Int, ijrXcoj: Int, ijrXlos: Int, ijrXiracc: Int, ijrXdph: Int, qpaXcoj: Int, qpaXlos: Int, qpaXiracc: Int, qpaXdph: Int, cojXlos: Int, cojXiracc: Int, cojXdph: Int, losXiracc: Int, losXdph: Int, iraccXdph: Int)
self.creationDate = Date()
self.ajkpXc = ajkpXc
self.ajkpXijr = ajkpXijr
self.ajkpXqpa = ajkpXqpa
self.ajkpXcoj = ajkpXcoj
self.ajkpXlos = ajkpXlos
self.ajkpXiracc = ajkpXiracc
self.ajkpXdph = ajkpXdph
self.cXijr = cXijr
self.cXqpa = cXqpa
self.cXcoj = cXcoj
self.cXlos = cXlos
self.cXiracc = cXiracc
self.cXdph = cXdph
self.ijrXqpa = ijrXqpa
self.ijrXcoj = ijrXcoj
self.ijrXlos = ijrXlos
self.ijrXiracc = ijrXiracc
self.ijrXdph = ijrXdph
self.qpaXcoj = qpaXcoj
self.qpaXlos = qpaXlos
self.qpaXiracc = qpaXiracc
self.qpaXdph = qpaXdph
self.cojXlos = cojXlos
self.cojXiracc = cojXiracc
self.cojXdph = cojXdph
self.losXiracc = losXiracc
self.losXdph = losXdph
self.iraccXdph = iraccXdph
var dictValue: [String: Any]
let createdAgo = creationDate.timeIntervalSince1970
return ["created_at" : createdAgo,
"ajkpXc": ajkpXc,
"ajkpXijr ": ajkpXijr,
"ajkpXqpa": ajkpXqpa,
"ajkpXcoj": ajkpXcoj,
"ajkpXlos ": ajkpXlos,
"ajkpXiracc ": ajkpXiracc,
"ajkpXdph": ajkpXdph,
"cXijr": cXijr,
"cXqpa": cXqpa,
"cXcoj": cXcoj,
"cXlos": cXlos,
"cXiracc": cXiracc,
"cXdph": cXdph,
"ijrXqpa": ijrXqpa,
"ijrXcoj": ijrXcoj,
"ijrXlos": ijrXlos,
"ijrXiracc": ijrXiracc,
"ijrXdph": ijrXdph,
"qpaXcoj": qpaXcoj,
"qpaXlos": qpaXlos,
"qpaXiracc": qpaXiracc,
"qpaXdph": qpaXdph,
"cojXlos": cojXlos,
"cojXiracc": cojXiracc,
"cojXdph": cojXdph,
"losXiracc": losXiracc,
"losXdph": losXdph,
"iraccXdph": iraccXdph ]
init?(snapshot: DataSnapshot)
guard let dict = snapshot.value as? [String: Any],
let createdAgo = dict["created_at"] as? TimeInterval,
let ajkpXc = dict["ajkpXc"] as? Int,
let ajkpXijr = dict["ajkpXijr"] as? Int,
let ajkpXqpa = dict["ajkpXqpa"] as? Int,
let ajkpXcoj = dict["ajkpXcoj"] as? Int,
let ajkpXlos = dict["ajkpXlos"] as? Int,
let ajkpXiracc = dict["ajkpXiracc"] as? Int,
let ajkpXdph = dict["ajkpXdph"] as? Int,
let cXijr = dict["cXijr"] as? Int,
let cXqpa = dict["cXqpa"] as? Int,
let cXcoj = dict["cXcoj"] as? Int,
let cXlos = dict["cXlos"] as? Int,
let cXiracc = dict["cXiracc"] as? Int,
let cXdph = dict["cXdph"] as? Int,
let ijrXqpa = dict["ijrXqpa"] as? Int,
let ijrXcoj = dict["ijrXcoj"] as? Int,
let ijrXlos = dict["ijrXlos"] as? Int,
let ijrXiracc = dict["ijrXiracc"] as? Int,
let ijrXdph = dict["ijrXdph"] as? Int,
let qpaXcoj = dict["qpaXcoj"] as? Int,
let qpaXlos = dict["qpaXlos"] as? Int,
let qpaXiracc = dict["qpaXiracc"] as? Int,
let qpaXdph = dict["qpaXdph"] as? Int,
let cojXlos = dict["cojXlos"] as? Int,
let cojXiracc = dict["cojXiracc"] as? Int,
let cojXdph = dict["cojXdph"] as? Int,
let losXiracc = dict["losXiracc"] as? Int,
let losXdph = dict["losXdph"] as? Int,
let iraccXdph = dict["iraccXdph"] as? Int
else return nil
self.key = snapshot.key
self.creationDate = Date(timeIntervalSince1970: createdAgo)
self.ajkpXc = ajkpXc
self.ajkpXijr = ajkpXijr
self.ajkpXqpa = ajkpXqpa
self.ajkpXcoj = ajkpXcoj
self.ajkpXlos = ajkpXlos
self.ajkpXiracc = ajkpXiracc
self.ajkpXdph = ajkpXdph
self.cXijr = cXijr
self.cXqpa = cXqpa
self.cXcoj = cXcoj
self.cXlos = cXlos
self.cXiracc = cXiracc
self.cXdph = cXdph
self.ijrXqpa = ijrXqpa
self.ijrXcoj = ijrXcoj
self.ijrXlos = ijrXlos
self.ijrXiracc = ijrXiracc
self.ijrXdph = ijrXdph
self.qpaXcoj = qpaXcoj
self.qpaXlos = qpaXlos
self.qpaXiracc = qpaXiracc
self.qpaXdph = qpaXdph
self.cojXlos = cojXlos
self.cojXiracc = cojXiracc
self.cojXdph = cojXdph
self.losXiracc = losXiracc
self.losXdph = losXdph
self.iraccXdph = iraccXdph
【问题讨论】:
您不能将非文本对象分配给文本字段。您只能分配 text(字符串)。换句话说,有一个对象数组var ahpPrios = [AHPPriority]()
,代码读取其中一个对象let ahpPrio = ahpPrios[indexPath.row]
,并尝试将整个对象分配给单个文本字段cell.itemValueLabel.text = ahpPrio
,这是没有意义的。您需要获取该对象的一个属性并将其分配给cell.itemValueLabel.text = ahpPrio.some_property
。此外,还有一个结构在结构中,所以你也需要协调它。
【参考方案1】:
错误清楚地表明这里的类型不匹配。 cell.itemValueLabel.text
的类型为 String
,因此要设置其值,您必须提供 String
。虽然您提供的属性ahpPrio
不是String
,但它的类型是AHPPriority
。因此,要修复它,您需要执行以下操作:
cell.itemValueLabel.text = "\(ahpPrio.propertyName)" // where propertyName denotes a property in `AHPPriority`
【讨论】:
嘿@Frankenstein,感谢您的快速回答!我更新了问题,因为我忘了分享 AHPPriority 类。我的目标是遍历所有这些项目。因此,我猜在这种情况下调用特定的 propertyName 不起作用。再次感谢您的帮助! 嗨@Schaedel420 您现在应该知道在 SO 1 中的帖子应该处理一个问题。这篇文章应该解决你在 OP 中提到的错误。如果您有后续问题,请将其作为另一个问题发布并发表评论,下面的链接将尽力帮助您。 @Schaedel420 如果您的目标是迭代结构的属性在另一个结构中,您的问题中没有包含任何尝试这样做的代码。也许您应该澄清和更新您的问题,以便将答案集中在特定问题上。以上是关于Swift“无法将''类型的值转换为'String'类型的主要内容,如果未能解决你的问题,请参考以下文章