iphone - EXC_BAD_ACCESS -iPhone objective-c

标签 iphone objective-c ios twitter exc-bad-access

我一直在尝试这个 Twitter api 的东西,它真的很令人困惑......

使用以下代码时,我不断收到 EXC_BAD_ACCESS 错误访问...这里有什么问题?

NSURL *followingURL = [NSURL URLWithString:@"https://api.twitter.com/1/users/lookup.json"];
// Pass in the parameters (basically '.ids.json?screen_name=[screen_name]')
id fromIntToNum = [NSNumber numberWithInteger: friID];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"159462573", @"user_id", nil];
// Setup the request
twitterRequest = [[TWRequest alloc] initWithURL:followingURL
                                                parameters:parameters
                                             requestMethod:TWRequestMethodGET];
// This is important! Set the account for the request so we can do an authenticated request. Without this you cannot get the followers for private accounts and Twitter may also return an error if you're doing too many requests
[twitterRequest setAccount:theAccount];
// Perform the request for Twitter friends
[twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    if (error) {
        /*
        // deal with any errors - keep in mind, though you may receive a valid response that contains an error, so you may want to look at the response and ensure no 'error:' key is present in the dictionary
        NSLog(@"%@",error);*/
    } else {
        /*NSError *jsonError = nil;
        // Convert the response into a dictionary
        NSDictionary *twitterGrabbedUserInfo = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&jsonError];
        // Grab the Ids that Twitter returned and add them to the dictionary we created earlier
        NSLog(@"%@", [twitterGrabbedUserInfo objectForKey:@"screen_name"]);*/
    }

}];

我分隔了我的代码失败的行...

什么可能导致此 Twitter API 问题?

以下行导致崩溃:::

[twitterRequest performRequestWithHandler:^(NSData *responseData,
                NSHTTPURLResponse *urlResponse, NSError *error) {

我有时也会收到此错误:

[__NSCFNumber credentialForAccount:]: unrecognized 

更新:我注释掉了处理程序并创建了 TwitterRequest 和 ivar,但它仍然崩溃...

最佳答案

在您的 block 中,您会查找错误,但如果出现错误,则将其记录下来并继续。您应该放入“else”语句,并且只有在没有错误的情况下才继续。

为什么不尝试注释掉处理程序中的所有代码 - 不要执行任何操作 - 看看是否会崩溃。然后尝试仅使用错误代码。然后尝试 JSON 序列化,最后一行。如果您能找到导致问题的 block 部分,将会有所帮助。

此外,我怀疑performRequestWithHandler:不会阻塞,但希望您在 block 内通知您的类请求已完成。如果是这样,则意味着“TWRequest *twitterRequest”应该是一个 ivar 或属性,并且您需要允许在处理程序完成时调用某些方法。您的崩溃可能是由于 ARC 在对象运行时重新分配对象造成的。

编辑: 请注意,TWRequest 类描述为:“使用 initWithURL:parameters:requestMethod: 方法来初始化新创建的 TWRequest 对象,传递所需的属性值。”它表示 PLURAL 属性,意味着超过 1 个。难道它还期望“credentialForAccount”属性?您必须阅读 Twitter 文档才能找到所有必需的属性。

编辑2: 好吧,我们甚至不知道你是否能达到你的指导员的水平。放一个 NSLog 在那里,但我怀疑它永远不会走那么远。如果为真,则存在三种可能性:

a) 它不喜欢这个 URL(尽管这看起来不错)

b)您缺少一些它期望的参数

c) id 不喜欢“theAccount”对象 - 它是有效的 ACAccount 对象吗?尝试 NSLogging。

它必须是这三件事之一。

关于iphone - EXC_BAD_ACCESS -iPhone objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11633026/

相关文章:

iphone - Apple 在哪个 WWDC session 视频中展示了用于本地化时间间隔格式化的 API?

ios - 将图像转换为 CGBitmapContext

ios - UILocalNotifications 算法

iphone - 我们如何以编程方式启用/禁用默认的 iPhone 相机?

ios - 如何通过容器 View Controller 判断使用了哪些单元格?

ios - UITableView 嵌套在没有边距的 UICollectionViewCell 中

java - 如何在 native 代码(Java、ObjectiveC/Swift)中读取 AsyncStorage

iphone - 使用 Cocos2d 自动 Sprite 动画

iphone - 在 iOS 应用程序的辅助 View 中时无法识别耳机按钮按下

iphone - NSDictionary:将 NSString 键转换为小写以在其上搜索字符串