gpt4 book ai didi

ios - 使用 Swift 返回的 block 调用 Objective-C 方法会导致 EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-11-30 13:25:12 26 4
gpt4 key购买 nike

从单例中,我尝试调用以下代码,退出 request() 方法时出现崩溃:

EXC_BAD_ACCESS堆栈以顶部的 swift_unknownRelease 结束。

let userService = UserService()

userService.request(user, data: data) { (dict:[NSObject : AnyObject]!, error:NSError!) in
if let err = error {
log.error("Add, delete or modified request: \(err)")
}
}

obj-c方法是:

- (void)request:(UserEntity *)userEntity data:(MyData *)data withCompletion:(void(^)(NSDictionary *dict, NSError *error))completion
{
NSString *url = [NSString stringWithFormat:@"%@?Id=%@&ype=%@",k_SERVER_REQUES, userEntity.contactID, data];

NSOperation *reqOp = [self requestOperationWithMethod:@"POST"
URL:url
withParameter:nil
success:^(NSInteger status, NSString *message, id data) {
NSError *error;

if (status == 1)
{ //If no error
User *user = [userEntity fetchUserWithoutOverwriting];
[[NSNotificationCenter defaultCenter] postNotificationName:kNotifDataChanged object:friendModel];
}else{
error = [NSError errorWithDomain:@"data" code:-1 userInfo:@{@"status":[NSNumber numberWithInteger:status]}];
}
if (completion) completion(data, error);

} failed:^(NSString* s) {
if(completion) completion(nil, [NSError errorWithDomain:@"data" code:-1 userInfo:@{@"detail":s}]);
}];

NSOperation *refreshOp = [[FeedService new] refreshProfileOperation:userEntity.contactID.longLongValue];

// We are using NSOperation and dependency to ensure that refreshOp is executed before followOp.
// So that the changes in followOp doesn't get overwritten by the refreshOp

[reqOp addDependency:refreshOp];
[[[AFHTTPRequestOperationManager manager] operationQueue] addOperation:refreshOp];
[[[AFHTTPRequestOperationManager manager] operationQueue] addOperation:reqOp];
}

99% 的时间都可以正常工作。但我有一个用户触发了系统崩溃,我不知道为什么,因为它似乎是相同的。

首先,如果我将调用更改为:

userService.request(user, data: data, withCompletion: nil) 

然后它就永远不会崩溃。所以崩溃实际上是在退出完成 block 时发生的。

进入区 block 时的参数值为:

dict    [NSObject : AnyObject]! Some
error NSError! nil None

我想知道为什么会发生这种情况,以及为什么它只发生在这个特定的用户身上......

最佳答案

在 Objective-C 方法中的这一行:

failed:^(NSString* s) {
if(completion) completion(nil, [NSError errorWithDomain:@"data" code:-1 userInfo:@{@"detail":s}]);
}

您可以在其中提供空的Dictionary而不是nil

关于ios - 使用 Swift 返回的 block 调用 Objective-C 方法会导致 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37296287/

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