问题
In my debugger this is how my log statement is printing my messages object:
self.messages = (
"<lean: 0x7fcf98665140, objectId: vglE1UJ5KI, localId: (null)> {\n messageBody = Jj;\n recipientId = XvvxETqjph;\n senderId = XvvxETqjph;\n timestamp = \"1424991590106.210938\";\n}",
"<lean: 0x7fcf98667940, objectId: rgBFYBMKlU, localId: (null)> {\n messageBody = \"test 3 from ian\";\n recipientId = XvvxETqjph;\n senderId = Hoy7UjLzOh;\n timestamp = \"1424631667110.638184\";\n}",
"<lean: 0x7fcf98667f30, objectId: hB5uhwsYsu, localId: (null)> {\n messageBody = \"test 2 from user1\";\n recipientId = XvvxETqjph;\n senderId = VQzxWbDnal;\n timestamp = \"1424630904935.162109\";\n}",
"<lean: 0x7fcf986685b0, objectId: dOe2B9oq5b, localId: (null)> {\n messageBody = \"test 1\";\n recipientId = XvvxETqjph;\n senderId = XvvxETqjph;\n timestamp = \"1424630808309.478027\";\n}"
)
what exactly is this? It looks like an object of dictionaries but the objectId and localId variables are outside of the curly braces so im not sure.
How do I restructure this so that I can have each individual dictionary by itself?
Im getting this data from my backend on parse.com:
-(void)viewWillAppear:(BOOL)animated{
NSString *userId = [[PFUser currentUser] objectId];
PFQuery *query = [PFQuery queryWithClassName:@"lean"];
[query whereKey:@"recipientId" equalTo:userId];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
// We found messages!
masterMessages = (NSDictionary *)objects;
NSLog(@"self.messages = %@", masterMessages);
[self.tableView reloadData];
}
}];
}
回答1:
this is a simple NSArray which contains objects of type named lean. you can get the needed object by its index
UPDATE:
I can't be sure of course. I have no idea about lean's structure. But most likely it contains field (and I assume it must be a property) named messageBody. If it is really so then you can do the following:
NSMutableArray* messagesBySenderName = [NSMutableArray new];
for (id lean in objects)
{
NSDictionary *messageBody = [lean valueForKeyPath:@"messageBody"];
if ([lean[@"senderId"] isEqualToString:@"Hoy7UjLzOh"])
{
[messagesBySenderName addObject:messageBody];
}
}
Again - it is only an assumption. In no case I can guarantee it will work
回答2:
They are PFObjects (from the parse.com framework), and PFObject implements description in this particular way. PFObjects implement a dictionary-like interface that is fully described here.
With due respect to others, it is overly general to type them as id, and to use KVO to access their properties. Moreover, messageBody appears to be a column of type string. Treating as a dictionary will be unproductive.
PFObjects should be treated this way:
for (PFObject *pfObject in objects) {
NSString *objectId = [pfObject objectId];
NSDate *createdAt = [pfObject createdAt];
NSString *messageBody = [pfObject objectForKey:@"messageBody"];
NSString *senderId = [pfObject objectForKey:@"senderId"];
// and so on
NSLog(@"This object has id %@ created at %@, senderId %@", objectId, createdAt, senderId);
}
I'm aware of your other recent SO question regarding how to derive and array of senderIds from this array. Extracting senderId ought to be obvious from this code, but (a) you probably don't need a new array based on your UI need (a table of conversations), and (b) I think you're actually looking for senderName, and that might mean a substantial change to how you've implemented relationships between parse objects.
I'll try to answer further over on that other question, but this one is clearly related.
来源:https://stackoverflow.com/questions/29053822/how-to-restructure-object-of-nsdictionaries