如何修复非属性列表对象null?

时间:2016-05-17 03:58:08

标签: ios nsuserdefaults

我有一个函数获取信息。我调试并获得真实的信息。但是,当我运行应用程序时,它会出错:

 App[2055:59870] Attempt to set a non-property-list object null as an NSUserDefaults/CFPreferences value for key lastName

我的功能检查登录并获取信息:

        NSString *password = _txtPass.text;

        NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];

        NSString *passwordMD5 = [functionStr MD5:passwordData];

        NSString *userUrl = [NSString stringWithFormat:@"http://ios.khothe.vn/web/gamecard/authenticate/user/%@/pass/%@", _txtUser.text, passwordMD5];
        //NSLog(@"link: %@", userUrl);

        NSURL *url = [NSURL URLWithString:userUrl];

        NSURLRequest *request=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0];

        //get data
        /*NSError *error= [[NSError alloc] init];
         NSHTTPURLResponse *response;
         NSData *data= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];*/


        NSError *error = nil;
        NSHTTPURLResponse *response;
        NSData *data= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        if (!data) {
            NSLog(@"Error: %@", [error localizedDescription]);                

        }
        //NSLog(@"Response code: %ld", (long)[response statusCode]);

        if([response statusCode]>=200 && [response statusCode]<300){

            //bug du lieu nhan dc
            //NSString *responseData= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            //NSLog(@"Data nhận đc: %@", responseData);
            NSError *error= nil;

            NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

            success = [[jsonDictionary valueForKey:@"success"] integerValue];

            if(success == 400){
                NSString *currency;

                if([[jsonDictionary valueForKey:@"country"]isEqualToString:@"36"]){
                    currency = @"AUD";
                }else if([[jsonDictionary valueForKey:@"country"]isEqualToString:@"704"]){
                    currency = @"VND";
                }else{
                    currency = @"USD";
                }                    


                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:[jsonDictionary valueForKey:@"username"] forKey:@"userName"];
                [defaults setObject:[jsonDictionary valueForKey:@"display_name"] forKey:@"displayName"];
                [defaults setObject:[jsonDictionary valueForKey:@"email"] forKey:@"email"];
                [defaults setObject:[jsonDictionary valueForKey:@"first_name"] forKey:@"firstName"];
                [defaults setObject:[jsonDictionary valueForKey:@"last_name"] forKey:@"lastName"];
                [defaults setObject:[jsonDictionary valueForKey:@"street1"] forKey:@"street"];
                [defaults setObject:[jsonDictionary valueForKey:@"postal_code"] forKey:@"postCode"];
                //[defaults setObject:[jsonDictionary valueForKey:@"rating"] forKey:@"rating"];
                [defaults setObject:currency forKey:@"currency"];
                [defaults synchronize];
            }else{
                [self showUIAlertWithMessage:@"Error! Please wrong username or password" andTitle:@"Message"];
            }

        }

    }

我希望这是足够的代码,让你知道我想做什么。租约帮助我修复我的功能。谢谢你!

0 个答案:

没有答案