ios - 无法为类型 'item' Swift iOS 调用初始值设定项

标签 ios swift

我正在尝试将 JSON 数据附加到数组。在循环时它不断推送错误,“无法调用初始化程序以类型为参数(id:JSON,标题:JSON,详细信息:String,ImageURL:String)来键入'item''

enter image description here

'Item' 初始值设定项如下所示: 导入 UIKit

class Item: NSObject {
    var id: String, title: String, detail: String, imageURL: String

    init(id: String, title: String, detail: String, imageURL: String) {
        self.id = id
        self.title = title
        self.detail = detail
        self.imageURL = imageURL
    }
}

最佳答案

你的构造函数期望得到String并且你正在发送JSON

我猜你正在使用 SwiftyJSON

因此将其编辑为

id    : result[i]["id"].stringValue
title : result[i]["title"].stringValue

如果您不使用 SwiftJSON 将其转换为字符串

result[i]["id"] as! String //warning, this is not safe and better with if let

关于ios - 无法为类型 'item' Swift iOS 调用初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32164993/

相关文章:

ios - Swift indexPathForSelectedRow 从 subview 返回 nil

iphone - 移动 safari 和 iOS 应用程序中的唯一标识符

swift - 查找数组中每个对象的属性总和

struct - 将结构内的 C 字符串转换为 Swift 字符串

ios - 替换后退按钮但保持滑动以返回

ios - Swift 如何通过点击 map 注释上的右侧附件标注按钮来查看 View

ios - iOS 中的 CleverTap 通知设置

ios - -[UITableView _endCellAnimationsWithContext :] Getting Core Data error while trying to update tableview 中的断言失败

ios - 如何使用 AVPlayerViewController 检测播放控件显示的切换?

ios - 无法使用类型为 'NSURL' 的参数列表调用类型为 '(fileURLWithPath: NSURL)' 的初始值设定项