Value to Dictionary

Hi,

I have an object in swift like this:

    import UIKit

class Center: NSObject{

var avatar : String!
var cover : String!
var name : String!
var url : String!
var location : [LocationCenter]!

init(fromDictionary dictionary: NSDictionary){
    avatar = dictionary["avatar"] as? String
    cover = dictionary["cover"] as? String
    name = dictionary["name"] as? String
    url = dictionary["url"] as? String
    location = [LocationCenter]()
    if let locationArray = dictionary["location"] as? [NSDictionary]{
        for dic in locationArray{
            let value = LocationCenter(fromDictionary: dic)
            location.append(value)
        }
    }
}

func toDictionary() -> NSDictionary
{
    let dictionary = NSMutableDictionary()
    if avatar != nil{
        dictionary["avatar"] = avatar
    }
    if cover != nil{
        dictionary["cover"] = cover
    }
    if name != nil{
        dictionary["name"] = name
    }
    if url != nil{
        dictionary["url"] = url
    }
    if location != nil{
        var dictionaryElements = [NSDictionary]()
        for locationElement in location {
            dictionaryElements.append(locationElement.toDictionary())
        }
        dictionary["location"] = dictionaryElements
    }
    return dictionary
}

@objc required init(coder aDecoder: NSCoder)
{
    avatar = aDecoder.decodeObject(forKey: "avatar") as? String
    cover = aDecoder.decodeObject(forKey: "cover") as? String
    name = aDecoder.decodeObject(forKey: "name") as? String
    url = aDecoder.decodeObject(forKey: "url") as? String
    location = aDecoder.decodeObject(forKey: "location") as? [LocationCenter]

}

@objc func encodeWithCoder(aCoder: NSCoder)
{
    if avatar != nil{
        aCoder.encode(avatar, forKey: "avatar")
    }
    if cover != nil{
        aCoder.encode(cover, forKey: "cover")
    }
    if name != nil{
        aCoder.encode(name, forKey: "name")
    }
    if url != nil{
        aCoder.encode(url, forKey: "url")
    }
    if location != nil{
        aCoder.encode(location, forKey: "location")
    }
    
}

}

In my ViewController (in objectivec), I must take the values and set them in a TableView. I want to take the “name” value. I write:

-(UITableViewCell *)tableView:(UITableView *)tableView discussionCellForAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dict = [_arrayCentri objectAtIndex:indexPath.row];
    NSLog(@"dictdictdict %@",((Center *)dict));

}

The array _arrayCentri is NSMutableArray. If, I write:

-(UITableViewCell *)tableView:(UITableView *)tableView discussionCellForAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dict = [_arrayCentri objectAtIndex:indexPath.row];
    NSLog(@"dictdictdict %@",((Center *)dict).name);

The application crashes. Why?

Thanks! :wink:

In attached, the json!

http://pompeiapp.altervista.org/resultstotal.json

Help me. I can not solve. Thanks!

Is it crashing on the *NSLog(@“dictdictdict %@”,((Center )dict).name); statement?
What displays with the *NSLog(@“dictdictdict %@”,((Center )dict)); statement?

What class of objects are stored in the _arrayCentri array?

[quote=“rcasey, post:4, topic:28254, full:true”]
Is it crashing on the NSLog(@“dictdictdict %@”,((Center *)dict).name); statement?[/quote]
The app crashes with this NSLog.

[quote=“rcasey, post:4, topic:28254, full:true”]
What displays with the NSLog(@“dictdictdict %@”,((Center *)dict)); statement?[/quote]
All values in the array with this NSLog.

[quote=“rcasey, post:4, topic:28254, full:true”]
What class of objects are stored in the _arrayCentri array?[/quote]
NSMutableArray normal. There isn’t an array of custom objects. How do I print only the “name” value?

Does _arrayCentri contain Center objects or is it an array of NSDictionary objects?

You are pulling dict out of the array as an NSDictionary, but then you are casting it to be a Center object in your NSLog statement.

Like @rcasey, I think your array is an array of of NSDictionary objects.
Please add the code where you populate your array, and add the crash log you get when thee app crashes.

In my NSDictionary, there is the response of the json. Not an array of custom objects.

If _arrayCentri is an array that contains NSDictionary objects, then you want to use dictionary syntax rather than looking for properties of an object. Try this:
*NSDictionary dict = [_arrayCentri objectAtIndex:indexPath.row];
NSLog(@“dictdictdict %@”,dict[“name”]);

Ok, now all okay. Thanks!

1 Like