2013-09-22 80 views
0

我正在使用AFNetworking 2.0.0-RC2通过Cocoapods添加到我的项目中。尝试使用我的第一个方法,调用web服务,查看来自AFNetworking的示例项目,它看起来很好。但出于某种原因,我的项目,我不能得到它的工作。AFNetworking 2.0.0-RC2:'DNHackerNewsAPIClient'没有可见的@interface声明选择器'getPath:parameters:success:failure:'?

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); 
}]; 
+0

看起来好像DNHackerNewsAPIClient类没有编译器所抱怨的方法。 DNHackerNewsAPIClient类的代码是什么? –

+0

@Benoit更新的问题。 – program247365

回答

1

这是因为2.0上的某些方法签名已更改。您现在应该使用下列之一:

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

我强烈建议你把你globalTimelinePostsWithBlock:APIClient内,不使用AFNetworking方法其他地方。通过这种方式,您可以封装AFNetworking,方便每次修改方法进行修复。

+0

是的,没有更多的错误!谢谢。我更新了我的问题,现在我似乎无法获取HTTP GET块来开火。 Hmmmm ... – program247365

0

在你的appDelegate中你正在使用一个前向声明。 (@class DNHackerNewsAPIClient)。你应该导入它。

#import "DNHackerNewsAPIClient.h" 
相关问题