Question: I am trying to access the nsstring data out side the function and also outside the class.

How can I access `useridStr` outside the function and class? This is my code:

```
-(id)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *loginStatus = [[NSString alloc] initWithBytes:[webData mutableBytes]
        length:[webData length] encoding:NSUTF8StringEncoding];
    NSDictionary *loginDict = [[loginStatus JSONValue] objectForKey:@"UserDetails"];
    NSArray *userId = [loginDict valueForKey: @"userid"];
    NSString *useridStr = [userId lastObject];
    NSLog(@"--------------....%@", useridStr);
}

```

Answer: 
```
NSUserDefaults *pre = [NSUserDefaults standardUserDefaults];
[pre setObject:useridStr forKey:@"useridStr"];

```

where you want to need string:

```
NSUserDefaults *pre =[NSUserDefaults standardUserDefaults];
NSString * useridStr =[pre stringForKey:@"useridStr"];

```

OR

In .h file of anotherView

```
-(id)initUserInfo:(NSString *)string;

```

In .m file of anotherView

```
-(id)initUserInfo:(NSString *)string{   
if (self = [super initWithNibName:@"nextView" bundle:nil]) {

    useridStr=string
}
return self;
}

```

In .m File of firstView

```
-(IBAction)btnNext_TouchUpInside:(id)sender{
nextView *second =[[nextView alloc]initUserInfo:useridStr];
[self presentModalViewController:second animated:NO];
}

```
What Piyush suggested is also correct. But there is also another way to achieve it. 

As he suggested **[NSUserDefaults](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html)** to save data you should keep in mind that **[NSUserDefaults](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html)** is generally used to save data like preferences which you want it to be stored even after Application is closed by user and you want those data again when you start your application. 

So if you want to save data like preferences go for **[NSUserDefaults](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html)**. If you want your data to be available throughout your application while its running and you do not need to save them like preferences I would recommend you declare that object globally in `Appdelegate` file and access them whenever you need. You should not store them as **[NSUserDefaults](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html)** because as Apple document says whatever you store in **[NSUserDefaults](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsuserdefaults_Class/Reference/Reference.html)** it will be saved in user's default database. So that will consume memory of your device. So in short saving everything to **NSUserDefaults** won't be a good idea if we consider memory managent concepts.