gpt4 book ai didi

ios - AFNetworking 2.0.0-RC2 : No visible @interface for 'DNHackerNewsAPIClient' declares the selector 'getPath:parameters:success:failure:' ?

转载 作者:行者123 更新时间:2023-11-29 03:37:35 26 4
gpt4 key购买 nike

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

DNAAppDelegate.h:

#import <UIKit/UIKit.h>

@interface DNAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

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

@end

DNAAppDelegate.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 上发生了变化。您现在应该使用以下一个:

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

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

关于ios - AFNetworking 2.0.0-RC2 : No visible @interface for 'DNHackerNewsAPIClient' declares the selector 'getPath:parameters:success:failure:' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18946213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com