无法在数组中存储对象

时间:2013-11-22 05:20:21

标签: ios

这是Apprecord

@interface AppRecord : NSObject
@property (nonatomic, strong) NSString *appIcon;
@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSString * description;
@property (nonatomic, strong) NSString * location;
@property (nonatomic, strong) NSString * address;
@property (nonatomic, strong) NSString * contacts;
@property (nonatomic, strong) NSString * additional_info;
@property (nonatomic, strong) NSString * image;
@end

我正在解析json添加到App记录类的对象

#import "ParserClass.h"
#import "AppRecord.h"
@interface ParserClass ()

@property (nonatomic, strong) NSArray *appRecordList;
@property (nonatomic, strong) NSData *dataToParse;
@property (nonatomic, strong) NSMutableArray *workingArray;
@property (nonatomic, strong) AppRecord *workingEntry;
@property (nonatomic, strong) NSMutableString *workingPropertyString;
@property (nonatomic, strong) NSArray *elementsToParse;
@property (nonatomic, readwrite) BOOL storingCharacterData;
@end


@implementation ParserClass
- (id)initWithData:(NSData *)data
{
    self = [super init];
    if (self != nil)
    {
    _dataToParse = data;
    }
    return self;
 }

- (void)main
{
    self.workingArray = [NSMutableArray array];
    self.workingPropertyString = [NSMutableString string];
    self.workingArray=[[NSMutableArray alloc]init];

    NSDictionary *allData=[NSJSONSerialization JSONObjectWithData:_dataToParse options:0 error:nil];
    NSLog(@"%@",allData);

    for (NSDictionary *dict in allData)
    {

         NSLog(@"dict====%@",dict);
        self.workingEntry=[[AppRecord alloc]init];

        self.workingEntry.name=[dict objectForKey:@"name"];

        self.workingEntry.description=[dict objectForKey:@"description"];


        self.workingEntry.location=[dict objectForKey:@"location"];


        self.workingEntry.address=[dict objectForKey:@"address"];


        self.workingEntry.contacts=[dict objectForKey:@"contacts"];


        self.workingEntry.additional_info=[dict objectForKey:@"additional_info"];


        self.workingEntry.image=[dict objectForKey:@"image"];

        [self.workingArray addObject:self.workingEntry];

    }

    NSLog(@"WORKING ARRAY========%@",self.workingArray);// Not getting proper value of working array

 self.workingArray = nil;
self.workingPropertyString = nil;
self.dataToParse = nil;
}

@end 

我的问题是没有获得正确的工作数值,它只存储描述属性,但它应该存储赞赏对象,请帮忙。

输出

 alldata=

(
            {
            "additional_info" = "lOREN iPSUM";
            address = "1972 Hillview St. Sarasota,FL 34239";
            contacts = 8745674556;
            description = "Very cute place, awesome wait staff, great food.  I am here on vacation and it     was an awesome place to go to after a day relaxing at the beach.";
            id = 1;
          image = "http://..";
        location = "1972 Hillview St. Sarasota,FL 34239";
        name = "Beer Tasting at Hurricane Hanks";
    },
        {
        "additional_info" = gdfgdfg;
        address = "Farrer Place, Sydney, New South Wales, Australia";
        contacts = 3423423423423;
        description = restataurant;
        id = 16;
        image = "http://..";
        location = kolkata;
        name = "mosco ";
    }
)

WORKING ARRAY========(
"Very cute place, awesome wait staff, great food.  I am here on vacation and it was an awesome  place to go to after a day relaxing at the beach.",
restataurant
)
 First object name== Beer Tasting at Hurricane Hanks

2 个答案:

答案 0 :(得分:3)

首先删除self.workingArray

的2次初始化

请替换

@property (nonatomic, strong) NSString * description;
带有其他名称的

deccription

要了解原因,请点击此link

答案 1 :(得分:0)

for (NSDictionary *dict in allData)
{
AppRecord *createAppRecord=[[AppRecord alloc]init];
//Do some thing
[self.workingArray addObject:createAppRecord];
}

我认为这会对你有所帮助。