通知崩溃应用程序

时间:2013-03-06 05:13:26

标签: ios nsnotificationcenter

我正在尝试将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];

1 个答案:

答案 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!