【问题标题】:Error calling class method in obj-c在 obj-c 中调用类方法时出错
【发布时间】:2018-03-16 06:34:27
【问题描述】:

我是 obj-c 的新手,我正在尝试为 square connect lib 创建一个 Cordova 插件。

我正在尝试创建 SCCAPIRequest 类的实例。 但我收到一个错误:

 error: no known class method for selector
  'requestWithCallbackURL:amount:locationID:notes:metadata:supportedTenderTypes:error:'

这是我的功能

- (void)requestCharge: (CDVInvokedUrlCommand *)command {
int amount = [[command.arguments objectAtIndex: 0] intValue];
NSDictionary* options = [command.arguments objectAtIndex: 1];

CDVPluginResult *pluginResult;

if( amount < 0 || amount == 0) {
 NSLog(@"Error: Ammount to charge is 0");
 NSString *errorResponse = @"Error: Ammount to charge is 0";
 pluginResult = [CDVPluginResult 
 resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorResponse];
}

[self setOptions:options];

NSError *error = nil;
SCCAPIRequest *request = [SCCAPIRequest requestWithCallbackURL:[NSURL 
URLWithString:yourCallbackURLString]
                                      amount:amount
                                locationID:self.locationID
                                      notes:self.note
                                    metadata:self.metadata
                              supportedTenderTypes:self.tenders
                                        error:&error];


NSData *response = [NSKeyedArchiver archivedDataWithRootObject:request];

if(error) {
  pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR 
  messageAsArrayBuffer:response];
} else {
 pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK 
  messageAsArrayBuffer:response];
}

[self.commandDelegate sendPluginResult:pluginResult 
callbackId:command.callbackId];
}

提前致谢!

【问题讨论】:

    标签: ios objective-c cordova cordova-plugins


    【解决方案1】:

    错误日志说明了您需要知道的一切。没有方法'requestWithCallbackURL:amount:locationID:notes:metadata:supportedTenderTypes:error:'。您正在调用 SCCAPIRequest 的不存在的 init 方法。

    如果您要在其头文件中查看该类的 init 方法,特别是在此链接上:https://github.com/square/SquarePointOfSaleSDK-iOS/blob/master/Sources/SCCAPIRequest.h,您会看到可以使用的两种不同的 init 方法。

    + (nullable instancetype)requestWithCallbackURL:(nonnull NSURL *)callbackURL
                                             amount:(nonnull SCCMoney *)amount
                                     userInfoString:(nullable NSString *)userInfoString
                                         locationID:(nullable NSString *)locationID
                                              notes:(nullable NSString *)notes
                                         customerID:(nullable NSString*)customerID
                               supportedTenderTypes:(SCCAPIRequestTenderTypes)supportedTenderTypes
                                  clearsDefaultFees:(BOOL)clearsDefaultFees
                    returnAutomaticallyAfterPayment:(BOOL)autoreturn
                                              error:(out NSError *__nullable *__nullable)error;
    
    + (nullable instancetype)requestWithCallbackURL:(nonnull NSURL *)callbackURL
                                             amount:(nonnull SCCMoney *)amount
                                     userInfoString:(nullable NSString *)userInfoString
                                         merchantID:(nullable NSString *)merchantID
                                              notes:(nullable NSString *)notes
                                         customerID:(nullable NSString*)customerID
                               supportedTenderTypes:(SCCAPIRequestTenderTypes)supportedTenderTypes
                                  clearsDefaultFees:(BOOL)clearsDefaultFees
                    returnAutomaticallyAfterPayment:(BOOL)autoreturn
                                              error:(out NSError *__nullable *__nullable)error __deprecated_msg("Use requestWithCallbackURL:amount:userInfoString:locationID:notes:customerID:supportedTenderTypes:clearsDefaultFees:returnAutomaticallyAfterPayment:error: instead.");
    

    所以使用其中一个来正确地创建SCCAPIRequest 的实例。

    【讨论】:

    • 你能给我指一个关于如何使用'init'方法的教程吗?就像我说的我是 obj-c 的新手。
    • 嗨@DanielCuesta,我在上面发布的代码是您在创建SCCAPIRequest 的新实例时调用的函数。您只需像上面那样简单地调用它:SCCAPIRequest *request = [SCCAPIRequest ...... lalala]
    • 我能够编译,但我得到一个错误SCCAPIRequest encodeWithCoder:]: unrecognized selector sent to instance
    • 更新问题。
    • 使用上面的两种方法,Daniel。
    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    相关资源
    最近更新 更多