我正在使用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);
}];
看起來好像DNHackerNewsAPIClient類沒有編譯器所抱怨的方法。 DNHackerNewsAPIClient類的代碼是什麼? –
@Benoit更新的問題。 – program247365