AFNetworking 2.0.0-RC2:'DNHackerNewsAPIClient'没有可见的@interface声明选择器'getPath:parameters:success:failure:'?

时间:2013-09-22 16:43:20

标签: ios objective-c afnetworking ios7 afnetworking-2

我正在通过Cocoapods将AFNetworking 2.0.0-RC2添加到我的项目中。尝试使用我的第一种方法来调用Web服务,从AFNetworking查看示例项目看起来很好。但由于某些原因我的项目,我无法让它工作。

DNAppDelegate.h:

#import <UIKit/UIKit.h>

@interface DNAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

+ (void)globalTimelinePostsWithBlock:(void (^)(NSArray *posts, NSError *error))block;

@end

DNAppDelegate.m:

#import "DNAppDelegate.h"
#import "AFNetworking.h"
#import "AFNetworkActivityIndicatorManager.h"
#import "DNHackerNewsAPIClient.h"

@class DNHackerNewsAPIClient;

@implementation DNAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //AFNetworking setup
    NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
    [NSURLCache setSharedURLCache:URLCache];
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];

    //Call API


    //Window work
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

+ (void)globalTimelinePostsWithBlock:(void (^)(NSArray *posts, NSError *error))block {
// ^ ERROR HAPPENS ON THIS METHOD
    [[DNHackerNewsAPIClient sharedClient] getPath:@"page" parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
        NSArray *postsFromResponse = [JSON valueForKeyPath:@"data"];



//        if (block) {
//            block([NSArray arrayWithArray:mutablePosts], nil);
//        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (block) {
            block([NSArray array], error);
        }
    }];
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or     SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its     current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the     user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

编辑:

DNHackerNewsAPIClient.h:

#import <Foundation/Foundation.h>
#import "AFHTTPClient.h"

@interface DNHackerNewsAPIClient : AFHTTPClient

+ (DNHackerNewsAPIClient *)sharedClient;

@end

DNHackerNewsAPIClient.m:

#import "DNHackerNewsAPIClient.h"
#import "AFJSONRequestOperation.h"

static NSString * const kDNHackerNewsAPIBaseURLString = @"http://api.ihackernews.com/";

@implementation DNHackerNewsAPIClient

//Setup the singleton to use throught the life of the application
+ (DNHackerNewsAPIClient *)sharedClient {
    static DNHackerNewsAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[DNHackerNewsAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kDNHackerNewsAPIBaseURLString]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

//    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
//    
//    // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
//  [self setDefaultHeader:@"Accept" value:@"application/json"];

    return self;
}

@end

我错过了什么?

编辑2:

为什么这似乎永远不会发生?

NSURL *url = [NSURL URLWithString:@"http://api.ihackernews.com/"];
DNHackerNewsAPIClient *HNClient = [[DNHackerNewsAPIClient alloc] initWithBaseURL:url];

[HNClient
    GET:@"page"
    parameters:nil
    success:^(NSHTTPURLResponse *response, id responseObject)
 {
        NSLog(@"It Worked: %@", response);
     NSLog(@"Y U NO WORK.");
 }
    failure:^(NSError *error) {
        NSLog(@"It Failed: %@", error);
 }];

2 个答案:

答案 0 :(得分:1)

那是因为某些方法签名在2.0上有所改变。您现在应该使用以下内容:

- (NSURLSessionDataTask *)GET:(NSString *)URLString
               parameters:(NSDictionary *)parameters
                  success:(void (^)(NSHTTPURLResponse *, id))success
                  failure:(void (^)(NSError *))failure

我强烈建议您将globalTimelinePostsWithBlock:放在APIClient内,不要在其他地方使用AFNetworking方法。这样,您可以封装AFNetworking,以便在方法更改时轻松修复。

答案 1 :(得分:0)

在您的appDelegate中,您正在使用转发声明。 (@class DNHackerNewsAPIClient)。你应该导入它。

#import "DNHackerNewsAPIClient.h"