Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios [AppCommons respondsToSelector::]:发送到解除分配实例的消息_Ios_Objective C_In App Purchase - Fatal编程技术网

Ios [AppCommons respondsToSelector::]:发送到解除分配实例的消息

Ios [AppCommons respondsToSelector::]:发送到解除分配实例的消息,ios,objective-c,in-app-purchase,Ios,Objective C,In App Purchase,AppCommons.h #import <Foundation/Foundation.h> #import <StoreKit/StoreKit.h> @interface AppCommons : NSObject <SKProductsRequestDelegate,SKRequestDelegate> - (void) getInAppPrice : (NSString *) inAppIdentifier; @end 这是我的课程,用于获取应用程

AppCommons.h

#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>

@interface AppCommons : NSObject <SKProductsRequestDelegate,SKRequestDelegate>
- (void) getInAppPrice : (NSString *) inAppIdentifier;
@end
这是我的课程,用于获取应用程序内购买的成本。我使用下面的行从另一个类调用getInAppPrice方法

AppCommons *appCommon = [[AppCommons alloc] init];
[appCommon getInAppPrice:inAppProductIdentifier];
收到响应时,我收到以下错误

“*-[AppCommons respondsToSelector:]:消息发送到解除分配的实例0xd4f0580


请帮助..谢谢。

我怀疑您这样做是在一种方法的范围内,如下所示:

- (void)myMethod {
    AppCommons *appCommon = [[AppCommons alloc] init];
    [appCommon getInAppPrice:inAppProductIdentifier];
}
这意味着当方法返回时,
appCommon
将被销毁

而是创建一个实例变量或属性,使
AppCommons
对象的生存期超过此方法调用

MyClass.h:

@class AppCommons;
@interface MyClass : NSObject {
    AppCommons *_appCommons;
}
...
@end
MyClass.m:

#import "MyClass.h"
#import "AppCommons.h"

@implementation MyClass

- (id)init {
    self = [super init];
    if (self) {
        _appCommons = [[AppCommons alloc] init];
    }
    return self;
}

- (void)myMethod {
    [_appCommons getInAppPrice:inAppProductIdentifier];
}

@end

我怀疑您是在方法的范围内这样做的,例如:

- (void)myMethod {
    AppCommons *appCommon = [[AppCommons alloc] init];
    [appCommon getInAppPrice:inAppProductIdentifier];
}
这意味着当方法返回时,
appCommon
将被销毁

而是创建一个实例变量或属性,使
AppCommons
对象的生存期超过此方法调用

MyClass.h:

@class AppCommons;
@interface MyClass : NSObject {
    AppCommons *_appCommons;
}
...
@end
MyClass.m:

#import "MyClass.h"
#import "AppCommons.h"

@implementation MyClass

- (id)init {
    self = [super init];
    if (self) {
        _appCommons = [[AppCommons alloc] init];
    }
    return self;
}

- (void)myMethod {
    [_appCommons getInAppPrice:inAppProductIdentifier];
}

@end

我怀疑您是在方法的范围内这样做的,例如:

- (void)myMethod {
    AppCommons *appCommon = [[AppCommons alloc] init];
    [appCommon getInAppPrice:inAppProductIdentifier];
}
这意味着当方法返回时,
appCommon
将被销毁

而是创建一个实例变量或属性,使
AppCommons
对象的生存期超过此方法调用

MyClass.h:

@class AppCommons;
@interface MyClass : NSObject {
    AppCommons *_appCommons;
}
...
@end
MyClass.m:

#import "MyClass.h"
#import "AppCommons.h"

@implementation MyClass

- (id)init {
    self = [super init];
    if (self) {
        _appCommons = [[AppCommons alloc] init];
    }
    return self;
}

- (void)myMethod {
    [_appCommons getInAppPrice:inAppProductIdentifier];
}

@end

我怀疑您是在方法的范围内这样做的,例如:

- (void)myMethod {
    AppCommons *appCommon = [[AppCommons alloc] init];
    [appCommon getInAppPrice:inAppProductIdentifier];
}
这意味着当方法返回时,
appCommon
将被销毁

而是创建一个实例变量或属性,使
AppCommons
对象的生存期超过此方法调用

MyClass.h:

@class AppCommons;
@interface MyClass : NSObject {
    AppCommons *_appCommons;
}
...
@end
MyClass.m:

#import "MyClass.h"
#import "AppCommons.h"

@implementation MyClass

- (id)init {
    self = [super init];
    if (self) {
        _appCommons = [[AppCommons alloc] init];
    }
    return self;
}

- (void)myMethod {
    [_appCommons getInAppPrice:inAppProductIdentifier];
}

@end

这不是答案本身,而是对错误原因的解释。特洛伊敌人给出的答案绝对正确,但不能准确解释情况

您正在调用某个函数,该函数执行以下操作:

- (void)myMethod {
    AppCommons *appCommon = [[AppCommons alloc] init];
    [appCommon getInAppPrice:inAppProductIdentifier];
}
在这个函数中,你调用
getInAppPrice
,它创建一个新对象,并将该新对象的委托设置为你的
appCommon
。然后该对象在后台等待

这就是你的问题所在。当第二个对象在后台等待时,你的
appCommon
对象被释放,因为代码到达函数末尾并释放
appCommon
。如果你的
SKProductsRequest
委托属性不弱,那么你的
appCommon
仍然会有一个reta计数为1,但由于不是,因此函数结束后,
appCommon
的保留计数为0,因此ARC将内存视为可重用并释放对象

然后在后台从
SKProductsRequest
对象获得响应。它检查它的委托指针(指针仍然是有效指针,但现在它指向一个已释放的对象(或者更糟的是,一个完全不同的对象))查看它是否响应委托函数,即调用已释放的对象时


因此,trojanfoe关于将
appCommons
对象设置为类变量,使其在函数结束后重新命名的答案是正确的。retained

这不是答案本身,而是对错误原因的解释。trojanfoe给出的答案绝对正确,但不能准确解释情况

您正在调用某个函数,该函数执行以下操作:

- (void)myMethod {
    AppCommons *appCommon = [[AppCommons alloc] init];
    [appCommon getInAppPrice:inAppProductIdentifier];
}
在这个函数中,你调用
getInAppPrice
,它创建一个新对象,并将该新对象的委托设置为你的
appCommon
。然后该对象在后台等待

这就是你的问题所在。当第二个对象在后台等待时,你的
appCommon
对象被释放,因为代码到达函数末尾并释放
appCommon
。如果你的
SKProductsRequest
委托属性不弱,那么你的
appCommon
仍然会有一个reta计数为1,但由于不是,因此函数结束后,
appCommon
的保留计数为0,因此ARC将内存视为可重用并释放对象

然后在后台从
SKProductsRequest
对象获得响应。它检查它的委托指针(指针仍然是有效指针,但现在它指向一个已释放的对象(或者更糟的是,一个完全不同的对象))查看它是否响应委托函数,即调用已释放的对象时


因此,trojanfoe关于将
appCommons
对象设置为类变量,使其在函数结束后重新命名的答案是正确的。retained

这不是答案本身,而是对错误原因的解释。trojanfoe给出的答案绝对正确,但不能准确解释情况

您正在调用某个函数,该函数执行以下操作:

- (void)myMethod {
    AppCommons *appCommon = [[AppCommons alloc] init];
    [appCommon getInAppPrice:inAppProductIdentifier];
}
在这个函数中,你调用
getInAppPrice
,它创建一个新对象,并将该新对象的委托设置为你的
appCommon
。然后该对象在后台等待

这就是你的问题所在。当第二个对象在后台等待时,你的
appCommon
对象被释放,因为代码到达函数末尾并释放
appCommon
。如果你的
SKProductsRequest
委托属性不弱,那么你的
appCommon
仍然会有一个reta计数为1,但由于不是,因此函数结束后,
appCommon
的保留计数为0,因此ARC将内存视为可重用并释放对象

然后在后台从
SKProductsRequest
对象获得响应。它检查它的委托指针(指针仍然是有效指针,但现在它指向一个释放的对象(或者更糟的是,一个完全不同的对象))以