我正在尝试将NSMutableDictionary
传递给NSNotification
到其他班级。
但是在发布NSMutableDictionary
应用程序崩溃时。
任何人都可以帮忙吗?
我正在尝试这个
NSMutableDictionary *temp = [[NSMutableDictionary alloc]init];
NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
temp = [responseString JSONValue];
NSLog(@"webdata is %@",temp);
NSLog(@"inside usersignup success");
[[NSNotificationCenter defaultCenter] postNotificationName:CNotifySignupSucess object:temp];
[temp release];
答案 0 :(得分:1)
首先,您需要阅读一些iOS编程基础知识。而且,
NSMutableDictionary *temp = [[NSMutableDictionary alloc]init];
NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
temp = [responseString JSONValue]; //----> this line is wrong
因为temp
指针指向新创建的NSMutableDictionary
对象,您将它重新分配给JSONValue
方法返回的另一个对象,即autorelease
对象,并且你不拥有它,因此不能release
它。实现想要的一些更好的方法是:
NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSMutableDictionary *temp = [responseString JSONValue];
NSLog(@"webdata is %@",temp);
NSLog(@"inside usersignup success");
[[NSNotificationCenter defaultCenter] postNotificationName:CNotifySignupSucess object:temp];
//NO RELEASING the AUTORELEASE OBJECT!!!!
OR:
NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSMutableDictionary *temp = [[NSMutableDictionary alloc]initWithDictionary:[responseString JSONValue]];
NSLog(@"webdata is %@",temp);
NSLog(@"inside usersignup success");
[[NSNotificationCenter defaultCenter] postNotificationName:CNotifySignupSucess object:temp];
[temp release];
OR:
NSMutableDictionary *temp = [[NSMutableDictionary alloc]init];
NSString *responseString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
[temp addEntriesFromDictionary:[responseString JSONValue]];
NSLog(@"webdata is %@",temp);
NSLog(@"inside usersignup success");
[[NSNotificationCenter defaultCenter] postNotificationName:CNotifySignupSucess object:temp];
[temp release];
在最后两个案例中,我正在考虑JSONValue
方法返回NSDictionary
。 Goog Luck!