将 plist 数据读入 NSArray 但得到 null
我在这里收到同样的警告 “本地声明隐藏实例变量”警告
但我遇到了更多问题...
这是我的代码
- (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];
}
,我在这里收到警告“'plist'的本地声明隐藏实例变量”
- (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;
}
这是我创建的两个项目在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";
}
这就是我的问题,为什么我不能将“plist”从 传递
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
...
}
给
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
}
并使用 NSMutableArray *plist = [[NSMutableArray alloc] init];
但仍然出现““plist”的本地声明隐藏了实例变量”
???
希望有人能解决这个问题
最好的问候!
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那么,您应该解决这个问题。
该警告告诉您,您已经声明了两个名为
plist
的变量:一个是该实例方法的本地变量,另一个是实例变量。局部变量的作用域较窄,它隐藏了实例变量,因此当您在方法中引用plist
时,您引用的是局部变量。这意味着您无法通过其他方法访问实例变量中存储的任何内容,也无法在其中存储任何内容以供其他方法检索。解决方案是杀死或重命名局部变量。如果您想要后者,请使用 Xcode 的“Edit All in Scope”功能。
还:
在第一行中创建数组是多余的,因为您立即将指向该数组的指针替换为指向另一个数组的指针(由
propertyList
返回)。因此,您永远不会使用并且泄漏第一个数组。您至少应该删除第一个数组的创建,并且可能应该删除整个第一行(从而删除第一个数组和局部变量)。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 toplist
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:
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).这是我修复警告的代码,程序可以在没有任何警告的情况下构建
它还可以在tableview中读取plist后显示结果
1.加载plist:
2.将数字返回到行
3.读取plist数据以在单元格中显示结果
它可以获取正确的数据,并在表格中显示正确的结果表格视图单元格
但是TTTTTT
如果你向上滚动tableview,没关系,当你在顶部时它会自动向下滚动
当你“向下滚动”表格视图时,程序崩溃???
为什么 ???我写错了什么吗???
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:
2.return the number to rows
3.read the plist data to show result in cells
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 ???
plist = [listFile propertyList];
=====>self.plist = [listFile propertyList];
这是正确的
plist = [listFile propertyList];
=====>self.plist = [listFile propertyList];
THAT IS CORRECT