Domanda

I want to get comments for a photo from a web server. Server returns an array that includes comments. Is it possible to attach a block instead of comment array to NSMutableDictionary? I want the block to return the comment and insert it's value to dictionary.

I mean some think like this (but it gives compile errors):

 NSArray* (^commentsBlock)(id responseObject) = ^(id responseObject){
        return responseObject;
 };


[self fetchCommentsForNode:[fileInfo objectForKey:@"nid"]
                                       success: commentsBlock];

            VDPhoto *photo = [VDPhoto photoWithProperties:
                              @{@"imageView": imageview,
                                @"title": [fileInfo objectForKey:@"title"],
                                @"comments" : commentsBlock,
                                }];

            [photos addObject:photo];
È stato utile?

Soluzione

Further to the discussion in the comments you probably want to do something like this...

Do something inline in the block for fetchCommentsForNode:success: - update the dictionary:

NSMutableDictionary *properties = [@{@"imageView": imageview,
                                         @"title": [fileInfo objectForKey:@"title"]} mutableCopy];

[self fetchCommentsForNode:[fileInfo objectForKey:@"nid"] success:^(id responseObject){
       properties[@"comments"] = responseObject;
       return responseObject;
}];

VDPhoto *photo = [VDPhoto photoWithProperties:properties];

[photos addObject:photo];

All you have to do is make sure the @property in the VDPhoto you save the properties to in the init method is strong, and not copy and then you can look at the dictionary and you will have your comments set once the success block has been called.

EDIT:

An even better option would be to add a @property (nonatomic, copy) NSArray *comments property to VDPhoto, and then set the result on the fetchCommentsForNode: on that:

VDPhoto *photo = [VDPhoto photoWithProperties:@{@"imageView": imageview,
                                                    @"title": [fileInfo objectForKey:@"title"]}];

[photos addObject:photo];

[self fetchCommentsForNode:[fileInfo objectForKey:@"nid"] success:^(id responseObject){
       photo.comments = responseObject;
       return responseObject;
}];

Altri suggerimenti

No, an object can't "become" another object. What you want to do is have the block insert the results array in the dictionary, rather than "become" the results array.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top