Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何修复可以在swift中将[NSDictionary]转换为NSDictionary的错误?_Ios_Swift_Uitableview_Swift2 - Fatal编程技术网

Ios 如何修复可以在swift中将[NSDictionary]转换为NSDictionary的错误?

Ios 如何修复可以在swift中将[NSDictionary]转换为NSDictionary的错误?,ios,swift,uitableview,swift2,Ios,Swift,Uitableview,Swift2,我试图在tableviewcells上显示我的json数据。但是,我在将json结果添加到我的配置文件homemodel时遇到了问题。我的请求管理器中出现错误,该错误表示无法将[NSDictionary]转换为NSDictionary 进口基础 类coreValueHomeModel{ //MARK: - properties var total: String = "total" var commentId: String = "commentId" //construct init(jso

我试图在tableviewcells上显示我的json数据。但是,我在将json结果添加到我的配置文件homemodel时遇到了问题。我的请求管理器中出现错误,该错误表示无法将[NSDictionary]转换为NSDictionary

进口基础 类coreValueHomeModel{

//MARK: - properties
var total: String = "total"
var commentId: String = "commentId"

//construct
init(jsonData: NSDictionary){
    total = jsonData.objectForKey("total") as? String ?? "total"
    commentId = jsonData.objectForKey("commentId") as? String ?? "commentId"
}

//construct
init(total: String, commentId: String) {
    self.total = total
    self.commentId = commentId
}

//prints object's UserInformation
var description: String {
    return "total:\(total), commentId:\(commentId)"
}
//MARK: - properties
var id: String = "id"
var name: String = "name"
var description: String = "description"
var color: String = "color"

//construct
init(jsonData: NSDictionary){
    id = jsonData.objectForKey("id") as? String ?? "id"
    name = jsonData.objectForKey("name") as? String ?? "name"
    description = jsonData.objectForKey("description") as? String ?? "description"
    color = jsonData.objectForKey("color") as? String ?? "color"
}

//construct
init(id: String, name: String, description: String ,color: String) {
    self.id = id
    self.name = name
    self.description = description
    self.color = color
}

//prints object's UserInformation
var des: String {
    return "id: \(id), name: \(name), description: \(description), color: \(color)"

}
//MARK: - properties
var firstName: String? = "First Name"
var lastName: String? = "Last Name"
var location: String? = "location"
var title: String? = "title"
var score: String? = "score"
var received: String? = "received"
var given: String? = "given"
var coreValueResults = [coreValueHomeModel]()
var strengthResults = [StrengthHomeModel]()

init(jsonData: NSDictionary){
    firstName = jsonData.objectForKey("firstName") as? String ?? "First Name"
    lastName = jsonData.objectForKey("lastName") as? String ?? "Last Name"
    location = jsonData.objectForKey("location") as? String ?? "location"
    title = jsonData.objectForKey("title") as? String ?? "title"
    score = jsonData.objectForKey("score") as? String ?? "score"
    received = jsonData.objectForKey("received") as? String ?? "received"
    given = jsonData.objectForKey("given") as? String ?? "given"

    if let commentTotals = jsonData.objectForKey("commentTotals") as? [NSDictionary] {
        for commentTotal in commentTotals {
            let coreValue = coreValueHomeModel(jsonData: commentTotal)
            coreValueResults.append(coreValue)
        }

    }

    if let strengths = jsonData.objectForKey("strengths") as? [NSDictionary] {
        for strength in strengths {
            let strengthValue = StrengthHomeModel(jsonData: strength)
            strengthResults.append(strengthValue)
        }

    }

}

init(){
    firstName = nil
    lastName = nil
    location = nil
    title = nil
    score = nil
    received = nil
    given = nil
    coreValueResults = []
    strengthResults = []
}
}

进口基础 班级强度家庭模型{

//MARK: - properties
var total: String = "total"
var commentId: String = "commentId"

//construct
init(jsonData: NSDictionary){
    total = jsonData.objectForKey("total") as? String ?? "total"
    commentId = jsonData.objectForKey("commentId") as? String ?? "commentId"
}

//construct
init(total: String, commentId: String) {
    self.total = total
    self.commentId = commentId
}

//prints object's UserInformation
var description: String {
    return "total:\(total), commentId:\(commentId)"
}
//MARK: - properties
var id: String = "id"
var name: String = "name"
var description: String = "description"
var color: String = "color"

//construct
init(jsonData: NSDictionary){
    id = jsonData.objectForKey("id") as? String ?? "id"
    name = jsonData.objectForKey("name") as? String ?? "name"
    description = jsonData.objectForKey("description") as? String ?? "description"
    color = jsonData.objectForKey("color") as? String ?? "color"
}

//construct
init(id: String, name: String, description: String ,color: String) {
    self.id = id
    self.name = name
    self.description = description
    self.color = color
}

//prints object's UserInformation
var des: String {
    return "id: \(id), name: \(name), description: \(description), color: \(color)"

}
//MARK: - properties
var firstName: String? = "First Name"
var lastName: String? = "Last Name"
var location: String? = "location"
var title: String? = "title"
var score: String? = "score"
var received: String? = "received"
var given: String? = "given"
var coreValueResults = [coreValueHomeModel]()
var strengthResults = [StrengthHomeModel]()

init(jsonData: NSDictionary){
    firstName = jsonData.objectForKey("firstName") as? String ?? "First Name"
    lastName = jsonData.objectForKey("lastName") as? String ?? "Last Name"
    location = jsonData.objectForKey("location") as? String ?? "location"
    title = jsonData.objectForKey("title") as? String ?? "title"
    score = jsonData.objectForKey("score") as? String ?? "score"
    received = jsonData.objectForKey("received") as? String ?? "received"
    given = jsonData.objectForKey("given") as? String ?? "given"

    if let commentTotals = jsonData.objectForKey("commentTotals") as? [NSDictionary] {
        for commentTotal in commentTotals {
            let coreValue = coreValueHomeModel(jsonData: commentTotal)
            coreValueResults.append(coreValue)
        }

    }

    if let strengths = jsonData.objectForKey("strengths") as? [NSDictionary] {
        for strength in strengths {
            let strengthValue = StrengthHomeModel(jsonData: strength)
            strengthResults.append(strengthValue)
        }

    }

}

init(){
    firstName = nil
    lastName = nil
    location = nil
    title = nil
    score = nil
    received = nil
    given = nil
    coreValueResults = []
    strengthResults = []
}
}

进口基础 类配置文件HomeModel{

//MARK: - properties
var total: String = "total"
var commentId: String = "commentId"

//construct
init(jsonData: NSDictionary){
    total = jsonData.objectForKey("total") as? String ?? "total"
    commentId = jsonData.objectForKey("commentId") as? String ?? "commentId"
}

//construct
init(total: String, commentId: String) {
    self.total = total
    self.commentId = commentId
}

//prints object's UserInformation
var description: String {
    return "total:\(total), commentId:\(commentId)"
}
//MARK: - properties
var id: String = "id"
var name: String = "name"
var description: String = "description"
var color: String = "color"

//construct
init(jsonData: NSDictionary){
    id = jsonData.objectForKey("id") as? String ?? "id"
    name = jsonData.objectForKey("name") as? String ?? "name"
    description = jsonData.objectForKey("description") as? String ?? "description"
    color = jsonData.objectForKey("color") as? String ?? "color"
}

//construct
init(id: String, name: String, description: String ,color: String) {
    self.id = id
    self.name = name
    self.description = description
    self.color = color
}

//prints object's UserInformation
var des: String {
    return "id: \(id), name: \(name), description: \(description), color: \(color)"

}
//MARK: - properties
var firstName: String? = "First Name"
var lastName: String? = "Last Name"
var location: String? = "location"
var title: String? = "title"
var score: String? = "score"
var received: String? = "received"
var given: String? = "given"
var coreValueResults = [coreValueHomeModel]()
var strengthResults = [StrengthHomeModel]()

init(jsonData: NSDictionary){
    firstName = jsonData.objectForKey("firstName") as? String ?? "First Name"
    lastName = jsonData.objectForKey("lastName") as? String ?? "Last Name"
    location = jsonData.objectForKey("location") as? String ?? "location"
    title = jsonData.objectForKey("title") as? String ?? "title"
    score = jsonData.objectForKey("score") as? String ?? "score"
    received = jsonData.objectForKey("received") as? String ?? "received"
    given = jsonData.objectForKey("given") as? String ?? "given"

    if let commentTotals = jsonData.objectForKey("commentTotals") as? [NSDictionary] {
        for commentTotal in commentTotals {
            let coreValue = coreValueHomeModel(jsonData: commentTotal)
            coreValueResults.append(coreValue)
        }

    }

    if let strengths = jsonData.objectForKey("strengths") as? [NSDictionary] {
        for strength in strengths {
            let strengthValue = StrengthHomeModel(jsonData: strength)
            strengthResults.append(strengthValue)
        }

    }

}

init(){
    firstName = nil
    lastName = nil
    location = nil
    title = nil
    score = nil
    received = nil
    given = nil
    coreValueResults = []
    strengthResults = []
}
}

以及
NSData(contentsOfURL:)
检索的数据示例:


您正在尝试将数组(
[NSDictionary]
)转换为单个
NSDictionary
,这是不允许的。访问您必须访问的阵列以获取您要查找的阵列

比如说

let myProfile = profile.firstObject

它是
NSDictionary
而不是
[NSDictionary]

let profile = jsonData!.objectForKey("profile") as? NSDictionary
您的代码还存在其他问题,如以下问题,这些问题没有意义,可能会导致错误

for profile in profile {
    print(profile)
}

一个有效的json字符串可以由数组或字典定义。如果您不知道json字符串的格式,那么应该在操作其内容之前检查其类型

do {
    if let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? NSDictionary {
        //got a dictionary, do your works with this dictionary
    } else if let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? NSArray {
        //got an array, do your works with this dictionary
    } else {
        //is not a valid json data
    }
} catch let _error as NSError {
   //got error
}

在我看来,对象被序列化为一个直接的字典,而不是一个字典数组。您能否发布通过NSData(contentsOfURL:)检索的相关数据片段?我以json格式发布了我试图检索的数据。您的数据片段非常清楚地显示了您的问题:
profile
是一本字典。您正试图将其转换为一系列词典。将您的演员阵容从
[NSDictionary]
更改为
NSDictionary
我按照您提到的方式进行了更改。它给了我一个错误,说profilehomemodel没有成员类型appendI,我做了您提到的更改。它给了我一个错误,说profilehomemodel没有成员类型append@VigneshKrish你的代码有几个问题,我很抱歉不能为你解决所有问题。提示:JSON节点永远不会是
[NSDictionary]
,您可能不想使用for循环访问
NSDictionary