-1

我正在尝试传递NSMutableDictionaryNSNotification其他班级。但是在发布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];
4

1 回答 1

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

或者:

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];

或者:

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。祝你好运!

于 2013-03-06T05:38:36.120 回答