views:

259

answers:

3

I got same warning here “local declaration hides instance variable” warning

but I got more problems...

Here is my code

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

 NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.someaddress.php"]
             cachePolicy:NSURLRequestUseProtocolCachePolicy
            timeoutInterval:60.0];

 // create the connection with the request
 // and start loading the data
 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
 NSLog(@"\n\nCONNECTION:   %@", theConnection);
 NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; 
 NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];   

 NSMutableArray *plist = [[NSMutableArray alloc] init];
 plist = [listFile propertyList];

 NSLog( @"\n 1111 plist is \n%@", plist );
   //I can get a plist format data here,But nothing in 2222
 NSLog(@"Now you see me tableView Row Count");
 NSLog(@"TOTAL PLIST ROW COUNT IS    = %i", [plist count]);


 // Return the number of rows in the section.
    return [plist count];
}

and I got Warning here"Local declaration of 'plist' hides instance variable"

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 static NSString *CellIdentifier = @"LightCell";

 LightCell0 *cell =(LightCell0 *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
  cell = [[[LightCell0 alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
 }
 // Set up the cell…
 NSLog(@"Now you see me Load Data %i", indexPath.row);

 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    //I try to get list data here But RETURN NULL
 NSLog( @"\n 2222 plist is \n %@", plist);

 switch (indexPath.row) {
   case 0:
   if ([plist valueForKey:@"nodeStatus"] == 0){
    cell.lightImageView.image = [UIImage imageNamed:@"lightOff.png"]; 
    NSLog(@"value for key Node Status : %@" ,[self.plists Valuefokey:@"nodeStatus"]);
                            //also return NULL !!
   }

   else if([self valueForKey:@"nodeStatus"] == 1){
    cell.lightImageView.image = [UIImage imageNamed:@"lightOn.png"];
   }
   break;


  case 1:
   cell.lightLocation.text =[plist valueForKey:@"nodeName"] ;
   if ([plist valueForKey:@"nodeStatus"] == 0){
    cell.lightImageView.image = [UIImage imageNamed:@"lightOff.png"];
   }
   else if([plist valueForKey:@"nodeStatus"] == 1){
    cell.lightImageView.image = [UIImage imageNamed:@"lightOn.png"];
   };

   break;
  default:
   break;
 }
 return cell;
}

This is the tow items I create in a plist

{
        category = Light;
        nodeID = 1;
        nodeName = "Living Room";
        nodeStatus = 0;
        nodeTrigger = 0;
        nodeType = "light_sw";
    },
        {
        category = Light;
        nodeID = 2;
        nodeName = Kitchen;
        nodeStatus = 0;
        nodeTrigger = 0;
        nodeType = "light_sw";
    }

So that's my question ,Why can't I pass "plist" from

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      ...
}

to

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ...
}

and I use NSMutableArray *plist = [[NSMutableArray alloc] init];

But still appear "Local declaration of 'plist' hides instance variable"

???

hope someone can figure out this problem

Best Regards !

+3  A: 

and I got Warning here"Local declaration of 'plist' hides instance variable"

Well, then, you should fix that.

The warning is telling you that you've declared two variables named plist: One local to this instance method, and the other an instance variable. The local variable, having a narrower scope, hides the instance variable, so that when you refer to plist in the method, you are referring to the local variable. This means that you cannot access anything stored in the instance variable by another method, nor store anything in it for another method to retrieve.

The solution is either to kill off or to rename the local variable. If the latter is what you want, use Xcode's “Edit All in Scope” feature.

Also:

NSMutableArray *plist = [[NSMutableArray alloc] init];
plist = [listFile propertyList];

Creating the array on the first of those lines is redundant, because you immediately replace your pointer to that array with the pointer to another array, returned by propertyList. Thus, you never use and you leak the first array. You should at least cut out the creation of the first array, and you should probably cut out the entire first line (thereby cutting out both the first array and the local variable).

Peter Hosey
Thanks !!!without your help i can't finish this program !If I got any problem I be back soon...
WebberLai
OK,I fix the warning "Local declaration of 'plist' hides instance variable"But got another warning if ([plist valueForKey:@"nodeStatus"] == 0){...}"Comparsion between pointer and integer"???is [plist valueForKey:@"nodeStatus"] should be 0 or 1 ?
WebberLai
You should ask that as a separate question.
Peter Hosey
@Peter Hosey :Can you edit my code to teach me what is the right grammar Thanks again !
WebberLai
Like I said, you should ask it as a separate question.
Peter Hosey
WebberLai
sorry,the browser was delay to show your comment
WebberLai
I use the %i to print out the [plist valueForKey:@"nodeStauts"]It's a pointer,because I use %@ to print it,got a plist format file it make me think it's a integer.
WebberLai
A: 

Here is the code I fix the warning ,the program can build without any warning it also can display the result after reading the plist in tableview

1.Load the plist:

- (void)viewDidLoad {
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www. someaddress.php"]
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                          timeoutInterval:60.0];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; 
    NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];   
    plist = [listFile propertyList];
}

2.return the number to rows

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [plist count];
}

3.read the plist data to show result in cells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"LightCell0";

    LightCell0 *cell =(LightCell0 *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[LightCell0 alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    // Set up the cell…

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    int i;
    for (i=0; i<[plist count]; i++) {

        //Get nodeName
        if(indexPath.row == i)
        {
            cell.lightLocation.text =  [[[plist objectAtIndex:i] valueForKey: @"nodeName"]description];


        //Get Light Status to show the image
        if ([[[plist objectAtIndex:i] valueForKey: @"nodeStatus"] intValue] == 0){
                cell.lightImageView.image = [UIImage imageNamed:@"lightOff.png"]; 
            }

        else if([[[plist objectAtIndex:i] valueForKey: @"nodeStatus"] intValue] == 1){
                cell.lightImageView.image = [UIImage imageNamed:@"lightOn.png"];
                cell.lightSwitch.on=YES;
            }   

        }
    }

    return cell;

}

It can get the right data ,and display the correct result in the tableview cells BUTTTTTTT If you scroll up the tableview,it's ok,when you on the top it will scroll down Automatically When you "scroll down" the tableview,program crash ???

WHY ??? did I write anything wrong ???

WebberLai
A: 

plist = [listFile propertyList];=====>self.plist = [listFile propertyList]; THAT IS IT

WebberLai