我设置 Category .h 文件的方式有什么问题?

What is wrong with the way I have set up the Category .h file?

我有一个 iOS 9.0 应用程序,我正在尝试添加应用程序内购买; (我以前从未使用过 IAP,但在网上找到了一些代码,希望能帮助我入门)。

为了维护简单,我决定将代码驻留在一个类别中(我之前只使用过一次类别)。也就是说,我对类别的实际结构有疑问。这是类别中的 .h 文件代码:

#import "SettingsViewController.h"
#import <StoreKit/StoreKit.h>

@interface SettingsViewController (Purchases)

@end

#define kInAppPurchaseManagerProductsFetchedNotification
@"kInAppPurchaseManagerProductsFetchedNotification"

@interface InAppPurchaseManager : NSObject <SKProductsRequestDelegate>  {

SKProduct *proUpgradeProduct;
SKProductsRequest *productsRequest;
}

@end

这是.m代码:

#import "SettingsViewController+Purchases.h"

@implementation SettingsViewController (Purchases)

- (void)requestProUpgradeProductData {

NSSet *productIdentifiers = [NSSet setWithObject:@"com.runmonster.runmonsterfree.upgradetopro" ];
productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];

// we will release the request object in the delegate callback
}

#pragma mark -
#pragma mark SKProductsRequestDelegate methods

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response  {

NSArray *products = response.products;
proUpgradeProduct = [products count] == 1 ? [[products firstObject] retain] : nil;
if (proUpgradeProduct)
{
    NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
    NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
    NSLog(@"Product price: %@" , proUpgradeProduct.price);
    NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
}

for (NSString *invalidProductId in response.invalidProductIdentifiers)
{
    NSLog(@"Invalid product id: %@" , invalidProductId);
}

// finally release the reqest we alloc/init’ed in requestProUpgradeProductData
[productsRequest release];

[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
}

@end

问题是 SKProduct 和 SKProductsRequest 对 .m 文件不可用,我确定这是由我设置 .h 文件的方式引起的。将不胜感激。

您似乎正试图将某个类别用于不当目的。特别是,看起来您的类别应该包含 属性 声明。但是,正如 the documentation 所说:

Categories can be used to declare either instance methods or class methods but are not usually suitable for declaring additional properties. It’s valid syntax to include a property declaration in a category interface, but it’s not possible to declare an additional instance variable in a category. This means the compiler won’t synthesize any instance variable, nor will it synthesize any property accessor methods. You can write your own accessor methods in the category implementation, but you won’t be able to keep track of a value for that property unless it’s already stored by the original class.