【问题标题】:Setting delegate causes crash设置委托会导致崩溃
【发布时间】:2012-04-01 17:33:07
【问题描述】:

我从 UIViewController 调用 NSObject,并将视图控制器设置为 NSObject 的委托。

执行此代码时,由于 EXC_BAD_ACCESS,应用程序在 模拟器 上完全崩溃(这在此处如何应用?)。当应用在真实设备上运行时,应用在第三次执行代码时崩溃。真实设备日志的输出如下:

(First time)
2012-04-01 13:29:05.154 The Record[19044:707] Done

----
(Second time)
2012-04-01 13:29:06.274 The Record[19044:707] Done

----
(Third time)
2012-04-01 13:29:07.772 The Record[19044:707] -[ViewController setDelegate:]: unrecognized selector sent to instance 0xde2e6e0
2012-04-01 13:29:07.773 The Record[19044:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController setDelegate:]: unrecognized selector sent to instance 0xde2e6e0'
*** First throw call stack:
(0x3211088f 0x37515259 0x32113a9b 0x32112915 0x3206d650 0x537f7 0x4f82b 0x3206a3fd 0x379eafaf 0x37ab076b 0x3206a3fd 0x379eafaf 0x379eaf6b 0x379eaf49 0x379eacb9 0x379eb5f1 0x379e9ad3 0x379e94c1 0x379cf83d 0x379cf0e3 0x3144322b 0x320e4523 0x320e44c5 0x320e3313 0x320664a5 0x3206636d 0x31442439 0x379fde7d 0x4dd8d 0x4dd38)
terminate called throwing an exception(lldb)

我只是调用 NSObject 并将视图控制器设置为委托。

Authorization *authorization = [[Authorization alloc] init];
authorization.delegate = self;

当我取出authorization.delegate = self; 时,应用程序不会崩溃,但我显然需要那条线。

编辑:这是代码。我正在使用@protocol。授权.h:

#import <Foundation/Foundation.h>
#import "KeychainItemWrapper.h>

@protocol AuthorizationDelegate <NSObject>
- (void)done:(BOOL *)done downloadURL:(NSURL *)downloadURL username:(NSString *)username password:(NSString *)password reason:(NSString *)reason;
@end

@interface Authorization : NSObject {

    id<AuthorizationDelegate> delegate;
    KeychainItemWrapper *keychain;

}

-(id)init;

@property (nonatomic, assign) id<AuthorizationDelegate> delegate;

@end

----
//Authorization.m

#import "Authorization.h"
#import "ASIFormDataRequest.h"
#import "JSON.h"

@implementation Authorization

@synthesize delegate;

-(id)init {

    keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"exampleID" accessGroup:nil];
    NSString *passPhrase = [keychain objectForKey:(id)kSecValueData];


    // Start request
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.website.com/authorization.php"]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:passPhrase forKey:@"passphrase"];
    [request setPostValue:@"YES" forKey:@"receivingAuthorization"];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request setDidFailSelector:@selector(requestFailed:)];
    [request setDelegate:self];
    [request startAsynchronous];

}

-(void)requestFinished:(ASIHTTPRequest *)request {

    if (request.responseStatusCode == 200) {

        NSString *responseString = [request responseString];
        NSDictionary *responseDict = [responseString JSONValue];
        NSString *downloadString = [responseDict objectForKey:@"downloadURL"];
        NSString *username = [responseDict objectForKey:@"username"];
        NSString *password = [responseDict objectForKey:@"password"];

        if ((downloadString != nil) && (username != nil) && (password != nil)) {

            NSURL *downloadURL = [NSURL URLWithString:downloadString];

            [delegate done:YES downloadURL:downloadURL username:username password:password reason:@"Received Authorization"];

         } else {

            [delegate done:NO downloadURL:nil username:nil password:nil reason:@"Invalid Request"];

        }

    } else if (request.responseStatusCode == 400) {

        if ([[request responseString] compare:@"Invalid passphrase"] == 0) {

            [delegate done:NO downloadURL:nil username:nil password:nil reason:@"Incorrect Credentials"];

        } else if ([[request responseString] compare:@"Invalid request"] == 0) {

            [delegate done:NO downloadURL:nil username:nil password:nil reason:@"Invalid Request"];

        }

    } else {

        [delegate done:NO downloadURL:nil username:nil password:nil reason:@"Invalid Request"];

    }

}

@end


----

//ViewController.h

@interface ViewController : UIViewController <IssuesPickerDelegate, IssueTableDelegate, AuthorizationDelegate> {

    Authorization *authorization;

}

@property (nonatomic, retain) Authorization *authorization;

----

//ViewController.m

@synthesize authorization;

-(void)done:(BOOL *)done downloadURL:(NSURL *)downloadURL username:(NSString *)username password:(NSString *)password reason:(NSString *)reason {

NSLog(@"Done");

if ((done == YES) && ([reason compare:@"Received Authorization"] == 0)) {


} else if ([reason compare:@"Incorrect Credentials"] == 0) {

    Login *login = [[Login alloc] init];

}

}


-(void)getAuthorization {

//this part here causes the crash

authorization = [[Authorization alloc] init];
authorization.delegate = self;

}

【问题讨论】:

  • 错误消息说它因为在一个名为ViewController 的类上设置了委托而崩溃,但您说的是该类是Authorization。你能详细说明/澄清一下吗?
  • 请发布您的代码,我们可以帮助您。
  • @ParthBhatt 我正在查看该链接。给我一秒钟,伙计们!
  • @JackHumphries:您是否正确创建了@property@synthesize 即属性并合成了您的委托声明?如果这对你没有帮助。请发布您的代码,以便我们更好地帮助您。
  • 我建议您将您的 id 委托重命名为 id authDelegate 以避免歧义和清晰的理解。

标签: iphone objective-c ios ipad delegates


【解决方案1】:

我忘记从-(id)init 返回一个值。我要返回self。我的新-(id)init 代码如下。

-(id)init {

    self = [super init];

    keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"exampleID" accessGroup:nil];
    NSString *passPhrase = [keychain objectForKey:(id)kSecValueData];         

    // Start request
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.website.com/authorization.php"]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:passPhrase forKey:@"passphrase"];
    [request setPostValue:@"YES" forKey:@"receivingAuthorization"];
    [request setDidFinishSelector:@selector(requestFinished:)];
    [request setDidFailSelector:@selector(requestFailed:)];
    [request setDelegate:self];
    [request startAsynchronous];

    return self;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多