Variable into Method how ?

Discussion in 'iOS Programming' started by mngoose, Oct 9, 2012.

  1. mngoose macrumors member

    Joined:
    May 26, 2012
    #1
    all,

    I got most of my app done... major problems worked out down to 2 problems but the first one is kicking my rear end all over the place.

    I am trying to get a tableview to populate with JSON data and can get the screen to give me the table with the keyboard (which is a whole another problem) but I can't get it to populate the screen with my json data so that I can move on the didselectrow process for the next view controller because I can't get the screen to count my array and then populate it with the data that I need it to in order to move on. I have done several different tutorials on youtube and can get them all to work now I am just trying to put the pieces together in order to make my app work the way that I need / want it to in order to move on.

    DevicetableView.h
    Code:
    #import "ViewController.h"
    
    @interface DeviceTableView : UIViewController <UITableViewDelegate, UITableViewDataSource>
    {
        UITextField *username;
        UITextField *password;
        NSMutableArray *anArray;
        NSArray *anArray2;
        
       
    }
    @property (retain, nonatomic) IBOutlet UILabel *loginusername;
    @property (retain, nonatomic) IBOutlet UILabel *loginpassword;
    @property (retain, nonatomic) IBOutlet UITableView *tableview;
    @property (strong, nonatomic) IBOutlet UITableView *myTable;
    
    @property (retain, nonatomic) IBOutlet UITextField *username;
    @property (retain, nonatomic) IBOutlet UITextField *password;
    @property (retain, nonatomic) NSError *localError;
    
    @property (retain, nonatomic) NSDictionary *myConvertedJSONData;
    @property (retain, nonatomic) NSArray *anArray2;
    @property (retain, nonatomic) NSArray *data3;
    @property (retain, nonatomic) NSData *datajson;
    
    
    @end
    
    DeviceTableView.m
    Code:
    
    
    #import "DeviceTableView.h"
    
    @interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
    
    
    
    @end
    @implementation DeviceTableView
    
    
    @synthesize loginpassword, loginusername;
    @synthesize username, password;
    @synthesize myTable;
    @synthesize anArray2;
    
    
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) { 
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    { 
        loginpassword.text = password.text;
        loginusername.text = username.text;
        NSLog(@"loginpassword = %@", loginpassword);
        NSLog(@"loginusername = %@", loginusername);
    
        
        NSString *url = [NSString stringWithFormat:@"http://a1.php?Fone=%@&pincode=%@&edit=Register", loginusername.text, loginpassword.text];
        NSLog(@"string = %@", url);
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString : url]];
        
        
        NSString *strResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"string result = %@", strResult);
        
        
        NSData *datajson = [strResult dataUsingEncoding: NSUTF8StringEncoding];
        
        id my2ConvertedJSONData = [NSJSONSerialization JSONObjectWithData : datajson options: NSJSONReadingMutableContainers error:&_localError];    
        NSArray *anArray = my2ConvertedJSONData;
        
        
        
        if ([my2ConvertedJSONData isKindOfClass:[NSArray class]])
        {
            NSArray *anArray = my2ConvertedJSONData;
            NSLog(@"ARRAY count: %i", [anArray count]);
            if ([[anArray objectAtIndex: 0] isKindOfClass:[NSDictionary class]]) NSLog(@"Yep, dictionary inside of an array!");
            NSLog(@"Array dict index 0: %@", [anArray objectAtIndex: 0]);
            NSLog(@"anArray = %@", anArray);
        
            NSArray * anArray2 = [[NSArray alloc] initWithArray:anArray copyItems:YES];
        
            NSLog(@"anarray2 count = %i", [anArray2 count]);
            NSLog(@"anarray2 = %@", anArray2);
            
            
            
            
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
        }}
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
          NSLog(@"anarray 2 in the tableview method = %@", anArray2);
        
       // tried several different items here to get from anArray to anArray2 in order to create the counter and each time the nslog comes up with 0 or null//
        
        NSLog(@"anarray counter = %u", [anArray2 count]);
        return [anArray2 count];
         [tableView reloadData];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
       
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        
        cell.textLabel.text = [[anArray2 objectAtIndex:indexPath.row] objectForKey:@"dname"];
        
        cell.detailTextLabel.text = [[anArray2 objectAtIndex:indexPath.row] objectForKey:@"dlloc"];
        
        NSLog(@"%@", cell);
        
        
        return cell;
    
    }
    
    
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (void)viewDidUnload {
        [self setUsername:nil];
        [self setPassword:nil];
        [self setLoginusername:nil];
        [self setLoginpassword:nil];
        [self setTableview:nil];
        [super viewDidUnload];
    }
    
    
    @end
    
    I finally got the system to pass the variables from one controller to the next and I appreciate all the help that you guys gave me on that,so that now works so that I can build my json URL string properly with auth but this me and my partner have been beating our heads against the wall and running in circles just confused.

    thanks in advance
    RON
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I suspect you are getting at least one compile-time warning from this code, am I right? Something along the lines of "Local declaration of ... hides instance variable"? Don't ignore it.
     
  3. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #3
    Dejo,

    yes your right but how do I get around them ? does this mean that I have to put new variables and synthesize new variables everytime that I want to use something new in a variable.

    I get that all the time when I use my variables in various parts of my code.

    thats the only thing that has bugged me lately

    Its one thing that I haven't been able to find online or in the documentation.

    Thanks
    RON
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    You get around them by changing your code so that you don't have local declarations that are hiding instance variables. Simple as that. I suspect you don't really have a good understanding of how declared-properties are used and how the instance variables that back them are set up and how local variables differ from them.

    Look at this line in your code:
    Code:
    NSArray * anArray2 = [[NSArray alloc] initWithArray:anArray copyItems:YES];
    Answer me this: is anArray2 an instance variable or a local variable in this case?
     
  5. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #5
    What is more than irritating with xcode is that most of my "local" declarations is NSLOGS so that I can test my outputs and see whats in the variables... THATS JUST STUPID in my opinion.

    I am going to again start from scratch on my app and do it again.

    RON
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    You didn't answer my question. If you're not going to help me, I'm afraid I can no longer help you. Sorry. Good luck with your app, though.
     
  7. mngoose thread starter macrumors member

    Joined:
    May 26, 2012
    #7
    Dejo,

    Can you tell me the logic behind this problem ? Why if I place a variable in a nslog code that it flags the variable looking for results of that variable and what is in that variable (regardless if it is an instance variable or not) as a local declaration variable and hides it? There is not another language that I know of that does that .

    The answer to your question is (as I understand it) anArray2 is a local variable in that specific view controller.

    I apologize if I seem a little short. XCODE is driving me nuts because it is doing things that I can't completely understand like giving me suggestions that do nothing but give me even more errors when I have worked my way through the tutorial that I found online 15 times the same way and it works but when I change one name it decides that it's now wrong and no longer valid but gives me a suggestion that doesn't work.

    I am not a complete newbie to programming (I primarily do web site design and work in the realm of php and html) and understand that there is a learning curve that is there but xcode as an IDE seems to be the strangest I have seen next to Eclipse or android.

    RON
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    I honestly have no idea what that question means. Post the actual code that matches your description, and then point out exactly what the problem is.

    Objective-C starts with exactly the same scoping rules as C. It then adds the "class scope", which allows references to instance variables to be in-scope in methods of that class, subject to ivar-scoping keywords (@public, @private, etc.) given for the ivars.

    This is essentially the same as Java, C++, C#, and various other C-like languages. The local/non-local scoping distinction is even the same as JavaScript, where a non-local name is presumed to reside in the global "window" scope.

    If you don't know what scope is, start with this:
    http://en.wikipedia.org/wiki/Scope_(computer_science)

    Post your code. Post the URL of the tutorial.

    With the code and tutorial posted, point out exactly which name you changed that makes it fail.

    We can't see your screen. We can't read your mind. And we can't debug code just from a vague description.
     
  9. dejo, Oct 9, 2012
    Last edited: Oct 10, 2012

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    NSLog is not the issue here. It just happens to be the line that is revealing that you have a local declaration that is hiding a instance variable. More on this next...

    Not exactly. anArray2 is a local variable in that if-block.

    But then you also have this synthesizer:
    Code:
    @synthesize anArray2;
    There is an instance variable behind this synthesizer and it is the one being hidden by the above local declaration.

    So, next question: What is the difference between the following two lines?:
    Code:
    self.anArray2 = [[NSArray alloc] initWithArray:anArray copyItems:YES];
    vs.
    NSArray * anArray2 = [[NSArray alloc] initWithArray:anArray copyItems:YES];
    What resources are you using to learn the fundamentals of Objective-C and the iOS SDK. You keep slagging Xcode when that's not really what's causing the problems. It's the way you're coding Objective-C. Plus, you might want to look into using the debugger so you don't have to NSLog all your values but instead you can set breakpoints, examine variables and step through code.
     
  10. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #10
    You're fumbling around in the dark, bumping into things and getting mad when you stub your toes.

    You need to learn the basics of C programming, then the basics of Objective C programming. The things you are struggling with are absolutely fundamental. Until you understand them, you will be lost, confused, and frustrated.
     

Share This Page