几个星期以来,我一直在为某些事情苦苦挣扎,这让我的进步真正停滞不前。我已经在 SO 上问了几次问题,人们一直很有帮助,但没有人破解我做错了什么。这似乎是一件相当简单的事情,所以希望有人在那里有一个灯泡时刻并解决这个问题。我正在实现一个 TWRequest,结果在字典中返回,我正在循环遍历结果以提取推文的一部分并创建这些“文本”组件的数组。直接通过循环我正在打印数组的日志 - _twitterText 并且它打印得很好。在这个方法完成之后,看起来好像 _twitterText 正在被转储。我在我的 .h 文件中将它创建为一个强大的属性,并在 viewdidload 中创建了一个 ivar。仍然没有喜悦。如何保留此数组以在另一个方法中使用? 这是我的 .h 文件....
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "CustomCell.h"
#import "AppDelegate.h"
#import <Twitter/Twitter.h>
@interface MyViewController : UITableViewController <CLLocationManagerDelegate>
{
CLLocationManager *here;
}
@property(strong) NSDictionary *dict;
@property(strong) CLLocationManager *here;
@property (strong, nonatomic) NSMutableArray *twitterText;
- (void)fetchTweets;
@end </p>
这是我的 .m 实现文件......
#import "MyViewController.h"
@interface MyViewController ()
@end
@implementation MyViewController
@synthesize dict;
@synthesize twitterText = _twitterText;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_twitterText = [[NSMutableArray alloc] init];
here = [[CLLocationManager alloc] init];
here.delegate = self;
[here startUpdatingLocation];
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
NSLog(@"phrase carried over is %@", delegate.a);
[self fetchTweets];
}
- (void)fetchTweets
{
TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
@"http://search.twitter.com/search.json?q=%40wimbledon"]
parameters:nil requestMethod:TWRequestMethodGET];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if ([urlResponse statusCode] == 200)
{
// The response from Twitter is in JSON format
// Move the response into a dictionary and print
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
//NSLog(@"Twitter response: %@", dict);
NSArray *results = [dict objectForKey:@"results"];
//Loop through the results
for (NSDictionary *tweet in results) {
// Get the tweet
NSString *twittext = [tweet objectForKey:@"text"];
// Save the tweet to the twitterText array
[_twitterText addObject:twittext];
}
NSLog(@"MY ************************TWITTERTEXT************** %@", _twitterText);
}
else
NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
//cell.venueDetails.text = [_twitterText objectAtIndex:indexPath.row];
NSLog(@"MY ************************OTHER BIT THAT WONT PRINT************** %@", _twitterText);
return cell;
}