NSObject 和 NSMutableArray 问题
你好,我有这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到该错误是因为
checkinsA
不是Checkin
对象,它是一个NSObject
(因为这是您分配的)和NSObject
没有setTime
方法。试试这个:You're getting that error because
checkinsA
isn't aCheckin
object, its anNSObject
(since that's what you alloced) andNSObject
doesn't have asetTime
method. Try this instead: