NSObject 和 NSMutableArray 问题

发布于 2024-11-10 07:59:08 字数 3126 浏览 2 评论 0原文

你好,我有这个 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 数组并在其中添加几个“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 等值的正确方法是什么?

Hello I have this NS Object:

@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

Now I want to create an NSMutable Array and add several "Checkins" inside of it.

When I am doing this:

 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];
}

I am getting error:

-[NSObject setTime:]: unrecognized selector sent to instance for
checkinsA.time = [NSString stringWithFormat:@"%@",[user objectForKey:@"created_time"]];

What is the right way to change value of the checkinsA.time checkins.id etc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

oО清风挽发oО 2024-11-17 07:59:08

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

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

You're getting that error because checkinsA isn't a Checkin object, its an NSObject (since that's what you alloced) and NSObject doesn't have a setTime method. Try this instead:

Checkin *checkinsA = [[Checkin alloc] init]; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文