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!