将fetchedResultsController转换为NSArray?

时间:2014-06-03 00:13:24

标签: ios objective-c nsarray nsfetchedresultscontroller

我正在使用(SOMessaging)https://github.com/SocialObjects-Software/SOMessaging

我想将此代码用于核心数据fetchedResultsController

问题是当实现上面的messagesViewController代码时,你必须返回一个像这样的消息的NSarray:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.myImage      = [UIImage imageNamed:@"arturdev.jpg"];
    self.partnerImage = [UIImage imageNamed:@"jobs.jpg"];


    [self loadMessages];
}

- (void)loadMessages
{
    self.dataSource = [[[ContentManager sharedManager] generateConversation] mutableCopy];
}



- (NSMutableArray *)messages
{
    return self.dataSource;
}
- (void)configureMessageCell:(SOMessageCell *)cell forMessageAtIndex:(NSInteger)index
{
    SOMessage *message = self.dataSource[index];

    // Adjusting content for 3pt. (In this demo the width of bubble's tail is 3pt)
    if (!message.fromMe) {
        cell.contentInsets = UIEdgeInsetsMake(0, 3.0f, 0, 0); //Move content for 3 pt. to right
        cell.textView.textColor = [UIColor blackColor];
    } else {
        cell.contentInsets = UIEdgeInsetsMake(0, 0, 0, 3.0f); //Move content for 3 pt. to left
        cell.textView.textColor = [UIColor whiteColor];
    }

    cell.userImageView.layer.cornerRadius = self.userImageSize.width/2;

    // Fix user image position on top or bottom.
    cell.userImageView.autoresizingMask = message.fromMe ? UIViewAutoresizingFlexibleTopMargin : UIViewAutoresizingFlexibleBottomMargin;

    // Setting user images
    cell.userImage = message.fromMe ? self.myImage : self.partnerImage;
}



The example calls this function to generate sample messages:

- (NSArray *)generateConversation
{
    NSMutableArray *result = [NSMutableArray new];
    NSArray *data = [NSArray arrayWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Conversation" ofType:@"plist"]]];


    for (NSDictionary *msg in data) {
        SOMessage *message = [[SOMessage alloc] init];
        message.fromMe = [msg[@"fromMe"] boolValue];
        message.text = msg[@"message"];
        message.type = [self messageTypeFromString:msg[@"type"]];
        message.date = [NSDate date];

        int index = (int)[data indexOfObject:msg];
        if (index > 0) {
            SOMessage *prevMesage = result.lastObject;
            message.date = [NSDate dateWithTimeInterval:((index % 2) ? 2 * 24 * 60 * 60 : 120) sinceDate:prevMesage.date];
        }

        if (message.type == SOMessageTypePhoto) {
            message.media = UIImageJPEGRepresentation([UIImage imageNamed:msg[@"image"]], 1);
        } else if (message.type == SOMessageTypeVideo) {
            message.media = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:msg[@"video"] ofType:@"mp4"]];
            message.thumbnail = [UIImage imageNamed:msg[@"thumbnail"]];
        }

        [result addObject:message];
    }

    return result;
}

意味着消息数组中的所有消息都必须是SOMessage对象。

我想使用核心数据,所以我将使用fetchedResultsController如何从fetchedresultscontroller加载我的数据。

我真的不想开始转换为NSarray,因为我不确定在添加更多数据和重新加载数据时会发生什么。

如果你能给我一些帮助的代码示例,那就太棒了。

如果你想在github上看到它的完整代码,你可以看到它是如何工作的。

0 个答案:

没有答案