2013-03-06 96 views
-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]; 
+0

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(signupsucessreceived :) name:CNotifySignupSucess object:nil]; – jpd 2013-03-06 05:20:52

+0

NSMutableDictionary * dict = notification.object;如果([[dict objectForKey:@「Success」] isEqualToString:@「1」]) { appDelegate.islogin = TRUE; self.title = nil; [appDelegate.userinfo setObject:[dict objectForKey:@「user_id」] forKey:@「userid」]; NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; NSString * strtemp = [NSString stringWithFormat:@「%@」,[dict objectForKey:@「user_id」]]; [默認setObject:strtemp forKey:@「userid」]; – jpd 2013-03-06 05:21:38

+0

羅布我試圖這 – jpd 2013-03-06 05:22:49

回答

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對象返回另一個對象,你並不擁有它,從而可以」 t 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]; 

在過去的2情況下,我正在考慮是JSONValue方法返回NSDictionary 。祝你好運!

+0

非常感謝法赫裏阿茲莫夫我得到了這個 – jpd 2013-03-06 09:01:22