NSObject和NSMutableArray问题

时间:2011-05-27 22:06:50

标签: iphone nsmutablearray nsobject

您好我有这个NS对象:

@interface Checkin : NSObject {

    NSString *name;
    NSString *profID;
    NSString *time;
    NSString *lon;
    NSString *lat;
    NSString *country;
    NSString *city;
    NSString *place;
    NSString *photoURL;
    NSMutableArray *taggedID;
    NSMutableArray *taggedName;
}

@property(nonatomic, retain)NSString *name;
@property(nonatomic, retain)NSString *profID;
@property(nonatomic, retain)NSString *time;
@property(nonatomic, retain)NSString *lon;
@property(nonatomic, retain)NSString *lat;
@property(nonatomic, retain)NSString *country;
@property(nonatomic, retain)NSString *city;
@property(nonatomic, retain)NSString *place;
@property(nonatomic, retain)NSString *photoURL;
@property(nonatomic, retain)NSMutableArray *taggedID;
@property(nonatomic, retain)NSMutableArray *taggedName;

@end

现在我想创建一个NSMutable Array并在其中添加几个“Checkins”。

当我这样做时:

 Checkin *checkinsA = [[NSObject alloc] init]; 

 NSDictionary *decodedJson = result;   
 NSArray *users = [decodedJson objectForKey:@"data"];

for(NSDictionary *user in users) {
    NSLog(@"Created item: %@ \n", [user objectForKey:@"created_time"]);
     checkinsA.time = [NSString stringWithFormat:@"%@",[user objectForKey:@"created_time"]];
    NSDictionary *fromData = [user objectForKey:@"from"];
     NSLog(@"user id is: %@ \n", [fromData objectForKey:@"id"]);
      checkinsA.profID = [fromData objectForKey:@"id"];

     NSLog(@"user name is: %@\n ", [fromData objectForKey:@"name"]);
      checkinsA.name =[fromData objectForKey:@"name"];

    NSDictionary *placeData = [user objectForKey:@"place"];
    NSDictionary *locationData = [placeData objectForKey:@"location"];
     NSLog(@"City: %@ \n", [locationData objectForKey:@"city"]);
      checkinsA.city = [locationData objectForKey:@"city"];

     NSLog(@"Country: %@ \n", [locationData objectForKey:@"country"]);
      checkinsA.country = [locationData objectForKey:@"country"];

     NSLog(@"Latitude: %@ \n", [locationData objectForKey:@"latitude"]);
     checkinsA.lat = [locationData objectForKey:@"latitude"];

     NSLog(@"Longitude: %@ \n", [locationData objectForKey:@"longitude"]);
     checkinsA.lon = [locationData objectForKey:@"longitude"];

     NSLog(@"Place name: %@ \n", [placeData objectForKey:@"name"]);
     checkinsA.place = [placeData objectForKey:@"name"];

    NSDictionary *tagData = [user objectForKey:@"tags"];
    NSArray *tagDataArray = [tagData objectForKey:@"data"];
    for(NSDictionary *tagData2 in tagDataArray){
      NSLog(@"tagged user id is: %@ \n", [tagData2 objectForKey:@"id"]);
        [checkinsA.taggedID addObject:[tagData2 objectForKey:@"id"]];
      NSLog(@"tagged user name is: %@\n ", [tagData2 objectForKey:@"name"]);
         [checkinsA.taggedName addObject:[tagData2 objectForKey:@"name"]];
    }

    [checkinArray addObject:checkinsA];
}

我收到错误:

  

- [NSObject setTime:]:发送到实例的无法识别的选择器   checkinsA.time = [NSString stringWithFormat:@“%@”,[user objectForKey:@“created_time”]];

更改checkinsA.time checkins.id等的值的正确方法是什么?

1 个答案:

答案 0 :(得分:6)

您收到该错误是因为checkinsA不是Checkin对象,而是NSObject(因为这是您分配的内容)而NSObject没有一个setTime方法。试试这个:

Checkin *checkinsA = [[Checkin alloc] init];