0

这是我第一次发帖。我一直害怕发布任何东西,因为我是 C++ 的菜鸟,但我不得不说我似乎很快就赶上了,因为我是一位经验丰富的 html、css、javascript 开发人员。

好的,所以我正在做这个实现 twitter 和 Matts TwitterEngine 的项目,事情开始融合在一起。我有一个表格视图,收集登录用户的时间线。您可以发布推文并查看时间线,但在尝试合并“@”用户“属性时遇到了一些问题。

我得出的结论是,这是因为它是一个 NSDictionary,其中包含多个数组。例如@user 由用户、scree_name 等组成,所以这里有一些代码,也许可以看看是否有人可以帮我解决这个问题。

在 NSObject 的 .h 文件中,我将调用一个

-(NSString*)author;

在 .m 文件中

 @implementation Tweet

 -(id)initWithTweetDictionary:(NSDictionary*)_contents {

if(self == [super init]) {
    
    contents = _contents;
    [contents retain];
}

return self;
}

-(NSString*)author {

return [[contents objectForKey:@"user"] objectForKey:@"screen_name"];
}

现在这是我将其称为 View 的地方,并且我相信我有问题,因为我仍在学习数组和可变数组并且不知道该调用什么。

在我的 TwitterViewController.h 中,我首先显然导入了工作正常的 SA_OAuth 引擎和控制器,然后我使用了两个 MutableArray,如下所示

  MGTwitterEngine *_twitter;
  SA_OAuthTwitterEngine *_engine;
  NSMutableArray *tweets;
  NSMutableArray *authors;

在 twitterview.m 我导入第一个 .h 然后调用

 //Connect to twitter API
- (void)viewDidAppear:(BOOL)animated {
    
    if(_engine) return;
    
    _engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
    _engine.consumerKey = @"key here =P";
    _engine.consumerSecret = @"secret here =P";
    
    UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
    
    if (controller) 
        [self presentModalViewController: controller animated: YES];
    else {
        tweets = [[NSMutableArray alloc] init];
        authors = [[NSMutableArray alloc] init];

然后我返回 MutableArray 的计数并尝试将其显示在推文旁边,以便在时间轴上显示实际推文的人员 screen_name

    - (UITableViewCell *)tableView:(UITableView *)tableView      cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {

  //add subtitle
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle     reuseIdentifier:@"cell"];

    }
    
    [cell.textLabel setNumberOfLines:7];
    [cell.textLabel setText:[(Tweet*)[tweets objectAtIndex:indexPath.row] tweet]];
    //-------THIS IS WHERE I KEEP GETTING A SIGBRT DURING LOAD "dump saying return count 0-----------[cell.textLabel setText:[(Tweet*)[authors objectAtIndex:indexPath.row] author]];------

   //[cell.detailTextLabel setText:[(Tweet*)[retweetCount objectAtIndex:indexPath.row]      reTweetCount]];
    



#pragma mark SA_OAuthTwitterController Delegate

- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
    
    NSLog(@"Authenticated with user %@", username);
    
    tweets = [[NSMutableArray alloc] init];
    authors =[[NSMutableArray alloc] init];
    
    [self updateStream:nil];
}





- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier {
    
    tweets = [[NSMutableArray alloc] init];
    
    for(NSDictionary *d in statuses) {
        
        NSLog(@"See dictionary: %@", d);
        
        Tweet *tweet = [[Tweet alloc] initWithTweetDictionary:d];
        [tweets addObject:tweet];
        [retweetCount addObject:tweet];
        [tweet release];
    }
    
    [self.tableView reloadData];
}


- (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier {
  
    
    authors = [[NSMutableArray alloc] init];
    
    for (NSDictionary *d in userInfo) {
     
    
    NSLog(@"See dictionary: %@", d);
       
        
        Tweet *tweet = [[Tweet alloc] initWithTweetDictionary:d];
        [authors addObject:tweet];
        [tweet release];
        
    }
    
    [self.tableView reloadData];

    
}

所以就像我说的“推文”正在工作,我可以通过 addObject 方法添加其他对象,例如转发计数,但我一生都无法弄清楚为什么我不能让用户屏幕名称显示在单元格中! !

我最终还想与化身异步并添加一个 RT 按钮,所以如果有人对实现这些的简单方法有任何建议,那也很棒!

我很感激我能得到的任何帮助!!!感谢希望很快听到有人帮助我了解 NSDictionary、NSArrays 和 MutableArrays,因为我相信这就是问题所在。或者,也许我只是没有正确地填充表格,无论我被难住了,并且可以使用一些帮助!

衷心感谢

安东尼

倾倒:

2011-05-29 03:27:21.477 ThemeCatcher[10409:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x01524be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x016795c2 objc_exception_throw + 47
    2   CoreFoundation                      0x0151a6e5 -[__NSArrayM objectAtIndex:] + 261
    3   ThemeCatcher                        0x00024498 -[TwitterVeiwController tableView:cellForRowAtIndexPath:] + 600
    4   UIKit                               0x008037fa -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
    5   UIKit                               0x007f977f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
    6   UIKit                               0x0080e450 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
    7   UIKit                               0x00806538 -[UITableView layoutSubviews] + 242
    8   QuartzCore                          0x004a8451 -[CALayer layoutSublayers] + 181
    9   QuartzCore                          0x004a817c CALayerLayoutIfNeeded + 220
    10  QuartzCore                          0x004a137c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
    11  QuartzCore                          0x004a10d0 _ZN2CA11Transaction6commitEv + 292
    12  QuartzCore                          0x004d17d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
    13  CoreFoundation                      0x01505fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
    14  CoreFoundation                      0x0149b0e7 __CFRunLoopDoObservers + 295
    15  CoreFoundation                      0x01463bd7 __CFRunLoopRun + 1575
    16  CoreFoundation                      0x01463240 CFRunLoopRunSpecific + 208
    17  CoreFoundation                      0x01463161 CFRunLoopRunInMode + 97
    18  GraphicsServices                    0x01f42268 GSEventRunModal + 217
    19  GraphicsServices                    0x01f4232d GSEventRun + 115
    20  UIKit                               0x0079e42e UIApplicationMain + 1160
    21  ThemeCatcher                        0x00002c29 main + 121
    22  ThemeCatcher                        0x00002ba5 start + 53
    23  ???                                 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
Current language:  auto; currently objective-c
(gdb)     

更新!!!!!!想发布我的解决方案以防其他人阅读此内容。我讨厌浏览帖子,用户找到了他们的 awnser,但不分享他们的解决方案以帮助其他人。所以我不会成为那个人,告诉你们我是如何做到这一点的!

解决方案:

首先正确地调用你的数组,就像下面 Danny 谈到的那样,不要遵循我的方法,因为它可能会在以后给你自己造成问题。" 我在两次通话中仍然使用 MutableArray,但会在不久的将来尝试更改。

现在确保所有数组的格式正确并“连接你的点”=P 但这是踢球者!下面是 MGTwitterEngine 的一部分,它解释了我的问题。起初我调用一个整数“1”,引擎认为我试图为整个用户数组获取一个“screen_name”,所以很明显它会通过一个 SIGABRT 并说你想在这里做什么!=P

只是想澄清我之前所说的关于整数和定义用户的内容。在

MGTwitterRequestTypes.h towards the bottom theres this line of code:typedef enum _     



MGTwitterResponseType {
MGTwitterStatuses           = 0,    // one or more statuses
MGTwitterStatus             = 1,    // exactly one status
MGTwitterUsers              = 2,    // one or more user's information
MGTwitterUser               = 3,    // info for exactly one user
MGTwitterDirectMessages     = 4,    // one or more direct messages
MGTwitterDirectMessage      = 5,    // exactly one direct message
MGTwitterGeneric            = 6,    // a generic response not requiring parsing
MGTwitterMiscellaneous      = 8,    // a miscellaneous response of key-value      pairs
MGTwitterImage              = 7,    // an image
 #if YAJL_AVAILABLE
MGTwitterSearchResults      = 9,    // search results
#endif
 }    MGTwitterResponseType;

// 这个键被添加到返回的每条推文或直接消息中, // 带有一个包含 MGTwitterRequestType 的 NSNumber 值。// 这旨在帮助客户端应用程序聚合更新。

定义 TWITTER_SOURCE_REQUEST_TYPE @"source_api_request_type"

我希望我能帮助其他人完成添加 scree_name ......基本上你可以将它用于其中的任何“文本”信息,例如 ReTweet Counts 等......等等......现在我需要弄清楚如何加载他们表格视图中的配置文件图像....我让它用这行代码加载图像,但由于某些奇怪的原因它不会加载到表格视图中。它甚至可以正确地将文本推到上面,就像它们是那里的图像一样,但没有

对此的任何建议将不胜感激!!!

NSString *imageurl = [avatarsURL objectAtIndex:0]; 
 NSLog(@"http://api.twitter.com/1/users/profile_image/username.json");   
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL   URLWithString:imageurl]]; 
UIImage *tableImage = [[UIImage alloc] initWithData:imageData]; 

[cell.imageView setImage:tableImage]; 
CGSize imageSize = CGSizeMake(45,40);   
UIGraphicsBeginImageContext(imageSize); 
CGRect imageRect = CGRectMake(0.2, 0.1, imageSize.width, imageSize.height); 
[tableImage drawInRect:imageRect]; 
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();    

....我在转储中完全没有错误“我必须删除超链接,因为垃圾邮件问题,我是新成员....仅供参考”

2011-05-29 17:26:02.950 ThemeCatcher[2974:207]    

http:api.twitter.com/1/users/profile_image/username.json
2011-05-29 17:26:02.980 ThemeCatcher[2974:207]   

http:api.twitter.com/1/users/profile_image/username.json
2011-05-29 17:26:02.981 ThemeCatcher[2974:207]   
http:api.twitter.com/1/users/profile_image/username.json

2011-05-29 17:26:02.982 ThemeCatcher[2974:207]   
http:api.twitter.com/1/users/profile_image/username.json
2011-05-29 17:26:02.983 ThemeCatcher[2974:207]   
http:api.twitter.com/1/users/profile_image/username.json
2011-05-29 17:26:02.984 ThemeCatcher[2974:207]     
http:api.twitter.com/1/users/profile_image/username.json
2011-05-29 17:26:02.985 ThemeCatcher[2974:207]      
http://api.twitter.com/1/users/profile_image/username.json
2011-05-29 17:26:02.987 ThemeCatcher[2974:207]   
http://api.twitter.com/1/users/profile_image/username.json
2011-05-29 17:26:06.550 ThemeCatcher[2974:207] Request Suceeded: BF0E18DB-A2CC-4823-AC02-   
6E182127BBF7

我可以离开 2 =P

4

1 回答 1

0

好的,有几个问题(我会把内存管理相关的东西留到最后):

一般问题

首先,您的实现numberOfSectionsInTableView:tableView:numberOfRowsInSection:外观如何?

接下来,每次你viewDidAppear:被调用并且你已经登录时,你基本上都会忘记所有旧推文:tweets = [[NSMutableArray alloc] init];

但更成问题的是,您每次接收数据时都会这样做:

- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier {

    tweets = [[NSMutableArray alloc] init]; // <= No!
    ...
}

然后有一个我不明白的问题,因为我不了解图书馆: yourauthors和 yout有什么区别tweets?(两者都是推文数组——坦率地说,我不明白这一点。)

如果正确理解了您的崩溃,那么当您尝试从authors数组中检索推文并假设您的作者数组中至少具有与数组相同数量的对象时,就会发生这种情况tweets。我不太了解 MGTwitterEngine,但从我从其文档中推断出来的内容,只有在您发送了对用户个人资料的明确请求userInfoReceived:forRequest:后才会调用。 因此,您的假设显然是错误的也就不足为奇了。

此外,无论如何,您应该能够从推文本身获取在时间线中显示推文所需的所有信息(除了化身的实际图像数据之外)。

内存管理

您正在泄露您将收到的每条推文:

尽管您在将每条推文添加到tweetsorauthors数组(很好)后注意正确地发布它,但每当有新数据进入时,您就会忘记这些数组。您可能不应该首先替换整个所述数组,而是 - 相反— 只需向它们添加新的东西,但既然你决定走那条路,请确保在分配新数组之前释放旧数组。

就个人而言,我建议一种不同的方法,并且tweets只在 中创建一次数组initWithStyle:,(并且根本不用费心创建一个authors数组)只根据需要添加和删除它 - 正确处理它dealloc并调用removeAllObjects它,当用户注销(如果您不完全处理视图控制器,在这种情况下)。

于 2011-05-29T09:33:57.570 回答