How to access the object
I would like to access the the _news items in a loop. But don't know how to get this done.
My Game.m looks like
#import "Game.h"
@implementation Game
@synthesize homename = _homename;
@synthesize guestname = _guestname;
@synthesize date = _date;
@synthesize gametype = _gametype;
@synthesize news = _news;
@synthesize gameId = _gameId;
-(NSString*)description{
return [NSString stringWithFormat:@"%@ gegen %@ (%@)", self.homename, self.guestname, self.gametype];
}
@end
My Game.h looks like
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
#import "News.h"
@interface Game : NSObject {
NSString* _homename;
NSString* _guestname;
NSString* _date;
NSString* _gametype;
NSNumber* _gameId;
// News* news;
@public
NSArray* _news;
}
@property (nonatomic, retain) NSString* homename;
@property (nonatomic, retain) NSString* guestname;
@property (nonatomic, retain) NSString* date;
@property (nonatomic, retain) NSString* gametype;
//@property (nonatomic, retain) News* news;
@property (nonatomic, retain) NSArray* news;
@property (nonatomic, retain) NSNumber* gameId;
@end
My News.h looks like
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
@interface News : NSObject {
NSString* _minute;
NSString* _title;
NSString* _bodytext;
NSString* _player;
}
@property (nonatomic, retain) NSString* minute;
@property (nonatomic, retain) NSString* title;
@property (nonatomic, retain) NSString* bodytext;
@property (nonatomic, retain) NSString* player;
@end
My news.m looks like
#import "News.h"
@implementation News
@synthesize player = _player;
@synthesize title = _title;
@synthesize bodytext = _bodytext;
@synthesize minute = _minute;
@end
And the code where I want to access the variable looks like:
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
NSLog(@"Loaded statuses: %@", objects);
HeaderText.text = [NSString stringWithFormat:@"%@", objects ];
// for(News* n in _news) NSLog([n _bodytext]);
// for (id object in objects) {
// NSLog(@"News = %@", object );
// }
}
The NSLog with objects looks good for the game. The next thing is that I want to do something like (above is more pseudo code than real code because I don't know how to do it right):
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
NSLog(@"Loaded statuses: %@", objects);
HeaderText.text = [NSString开发者_运维百科 stringWithFormat:@"%@", objects ];
// loop all
for (id object in objects) {
news = objects.news;
for (id mynews in news) {
NSLog(@"News minute = %@", news.minute );
NSLog(@"News title = %@", news.title );
NSLog(@"News bodytext = %@", news.bodytext );
NSLog(@"News player = %@", news.player );
}
}
}
How to do the getter/setter methods right (so that I can use it here)?
Sorry for the surely stupid question but I don't get the point with it.
Two things:
1stly, is the _news object a private variable, without having a property declaration (getters and setters for e.g.)? The '_variableName' format is usually used to denote private variables.
2ndly, if it is not a private variable, do all the items within the _news array belong to the same class?
If so, you can do a
for (NewsObject *theNewsObject in _news)
{
//code here
}
The for(id randomObject in array)
is useful when you don't know what type of object is in the array or if the objects contained in the array are of different types.
Now, again, all the objects inside the NewsObject ought to be public properties that can be accessed by other classes (they should have getters and setters).
Hope this helps. :)
EDIT FOR UPDATED QUESTION
So, if I'm getting your question correctly, you have a Game object, which has an array of News object inside it.
So, in your Game.h
NSString* _homename;
NSString* _guestname;
NSString* _date;
NSString* _gametype;
NSNumber* _gameId;
NSArray * _newsObjects; //declare it as NSMutableArray if you need to mutate it
Now, where you declare your properties;
@property(nonatomic, retain)NSArray *newsObjects
Synthesize it like you normally would in the Game.m file
You are creating the getters/setters automatically by using the @synthesize directive. It creates the getters and setters for you.
Again, from your code, it looks like the NSArray of objects that are passed through the method
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
consist of Game objects.
So, to access the News object from within the array of Game objects, import News.h and Game.h in the class where this method is being executed, and do the following:
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
NSLog(@"Loaded statuses: %@", objects);
// loop all
for (Game *gameObject in objects) {
NSArray *newsObjectArray = [gameObject newsObjects] //or gameObject.newsObject
for (News *mynews in newsObjectArray) {
NSLog(@"News minute = %@", mynews.minute );
NSLog(@"News title = %@", mynews.title );
NSLog(@"News bodytext = %@", mynews.bodytext );
NSLog(@"News player = %@", mynews.player );
}
}
}
What I found in your code that there was code that was being executed which was not declared in any part of the example that you posted.
What the code above will do is it will look through the Game object array (called objects
).
Then, for each gameObject
within that array, it will loop through the array of newsObject
for that gameObject
.
Hope this helps. :)
精彩评论