更好的辦法是incapsulate所有發件人的信息到JSON,例如,讓我們發送的郵件用戶的位置:
#define kLatitude @"latitude"
#define kLongitude @"longitude"
#define kMessage @"message"
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"12.23423424" forKey:kLatitude];
[dict setObject:@"-2.13123333" forKey:kLongitude];
[dict setValue:@"Hello, this is my location" forKey:kMessage];
// to JSON:
NSError *error = nil;
NSData* nsdata = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
// send message
NSString* jsonString =[[NSString alloc] initWithData:nsdata encoding:NSUTF8StringEncoding];
[[QBChat instance] sendMessage:jsonString toRoom:self.currentRoom];
和接收消息:
-(void)chatRoomDidReceiveMessage:(QBChatMessage *)message fromRoom:(NSString *)roomName{
// JSON parsing
NSData *data = [message.text dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSString *message = [jsonDict objectForKey:kMessage];
NSString *latitude = [jsonDict objectForKey:kLatitude];
NSString *longitude = [jsonDict objectForKey:kLongitude];
}
我收到來自chatroomDidRecieveMessage的消息,自定義參數爲nil,但設置它們。爲什麼? – Roman