0

我正在尝试编写一个使用 wunderground api 的天气应用程序...我有一个 apiKey 并在下面的代码中使用它,但在调试区域中看不到任何结果... Xcode 向我显示警告在“EXPRESSION RESULT UNUSED”一行中......这是问题吗?有人可以帮我吗?

#import "WeatherForecast.h"
#import "MainViewController.h"

@implementation WeatherForecast

- (void) queryServiceWithState:(NSString *)state
                andCity:(NSString *)city
                withParent:(UIViewController *)controller {
viewController = (MainViewController *)controller;
responseData = [NSMutableData data];
apiKey = @"c5f79118382c6e91";

NSString *url =
[NSString stringWithFormat:
 @"http://api.wunderground.com/api/%@/conditions/q/%@//%@.xml",
 apiKey, state, city];

theURL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:theURL];
[[NSURLConnection alloc] initWithRequest:request delegate:self];//EXPRESSION RESULT     UNUSED
}


#pragma mark NSURLConnection Delegate Methods

- (NSURLRequest *)connection:(NSURLConnection *)connection
         willSendRequest:(NSURLRequest *)request
        redirectResponse:(NSURLResponse *)response{
@autoreleasepool {
    theURL = [request URL];
}
return request;
}

- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}


-(void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data {
[responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error = %@",error);
}

- (void)connectionDidFinishiLoading: (NSURLConnection *)connection {
NSString *content =
[[NSString alloc]initWithBytes:[responseData bytes]
                        length:[responseData length]
                      encoding:NSUTF8StringEncoding];
NSLog ( @"Data = %@",content);

//...Insert code to parse the content here...

[viewController updateView];

}

@end

我的应用程序还有另外 2 个 .m 文件,可能是其中一个错误

#import "MainViewController.h"
#import "WeatherForecast.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad
{
 [super viewDidLoad];
[self refreshView:self];
}

- (IBAction)refreshView:(id)sender {
 [loadingActivityIndicator startAnimating];
 [self.forecast queryServiceWithState:@"UK" andCity:@"London" withParent:self];
}


- (void)updateView {

//...

[loadingActivityIndicator stopAnimating];
} 

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)showInfo:(id)sender
{    
FlipsideViewController *controller = [[FlipsideViewController alloc]               

initWithNibName:@"FlipsideViewController" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}

@end

#import "AppDelegate.h"
#import "WeatherForecast.h"
#import "MainViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    

(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.mainViewController = [[MainViewController alloc]     

initWithNibName:@"MainViewController" bundle:nil];

WeatherForecast *forecast = [[WeatherForecast alloc] init];
self.mainViewController.forecast = forecast;


self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{

}

- (void)applicationWillTerminate:(UIApplication *)application
{

}

@end

如果我尝试关闭互联网连接,我可以在调试区域看到“错误消息”,但如果我打开互联网连接,我只会看到活动指示器永远旋转......

谢谢你的回复......我感到很失落......

4

1 回答 1

0

你得到EXPRESSION RESULT UNUSED的是连接对象。您通常应该将其存储到一个属性中,以确保在您仍在使用它时它不会被破坏。

于 2013-07-20T22:12:44.037 回答