zoukankan      html  css  js  c++  java
  • 项目中遇到的问题解决:深刻认识到类的初始化

    -(NSArray *)getdataFromDatabase
    {
        NSString *path = [self getDBPath];
        NSLog(@"path ==== %@",path);
        NSLog(@"self.getDBPath == %@",[self getDBPath]);
        
        FMDatabase *membersDB = [FMDatabase databaseWithPath:path];
        if (![membersDB open]) {
            NSLog(@"Failed to open membersDB");
            return nil;
            [membersDB close];
        }
        NSMutableArray *array = [NSMutableArray array];
        FMResultSet *rs = [membersDB executeQuery:@"SELECT * FROM Member"];//这里数组为空  easy出错
        <pre name="code" class="objc">    <span style="color:#FF0000;">
        Member *member = [[Member alloc]init];
        while ([rs next])
        { member.name = [rs stringForColumn:@"Name"]; 
          member.age = [rs intForColumn:@"Age"]; 
          member.sex = [rs intForColumn:@"Sex"];
          member.height = [rs intForColumn:@"Height"]; member.Weight = [rs intForColumn:@"Weight"];
          member.icon = [UIImage imageWithData:[rs dataForColumn:@"Photo"]];
         NSLog(@"member.name =============  %@",member.name);
         [array addObject:member];</span> 
        } 
         [membersDB close];<span style="color:#FF0000;"> 
         NSLog(@"array[0] == %@,
    array[1] == %@,
    array[2]==%@",[array[0] name],[array[1] name],[array[2] name]);</span>
         return [array copy];
     }
    
    </span>

    信息打印例如以下:

    在while循环中打印是正确的。不反复的。能够循环结束在打印我们加入成员的数组,发现都是反复的,也就是都是同一个对象!

    。。!
    泪奔啊。。。。

    。。这时对类的理解不深刻啊。我在循环外加的 Member *member = [[Member alloc]init];初始化,也就是说再循环中我一直在对同一个对象进行反复操作。
    ,以至于加入成员的数组都是同一个对象,不同的个数而已。
    所以解决的方法应该是在循环中初始化。这样一来,每次循环都是一个新的对象(内存地址),这样一来就正确了。!


    -(NSArray *)getdataFromDatabase
    {
        NSString *path = [self getDBPath];
        NSLog(@"path ==== %@",path);
        NSLog(@"self.getDBPath == %@",[self getDBPath]);
        
        FMDatabase *membersDB = [FMDatabase databaseWithPath:path];
        if (![membersDB open]) {
            NSLog(@"Failed to open membersDB");
            return nil;
            [membersDB close];
        }
        NSMutableArray *array = [NSMutableArray array];
        FMResultSet *rs = [membersDB executeQuery:@"SELECT * FROM Member"];//这里数组为空  easy出错
        
    
    
        while ([rs next])
        {
    <pre name="code" class="objc">     <span style="color:#FF0000;">Member *member = [[Member alloc]init];</span>

          member.name = [rs stringForColumn:@"Name"]; 
          member.age = [rs intForColumn:@"Age"]; 
          member.sex = [rs intForColumn:@"Sex"];
          member.height = [rs intForColumn:@"Height"]; member.Weight = [rs intForColumn:@"Weight"];
          member.icon = [UIImage imageWithData:[rs dataForColumn:@"Photo"]];
          NSLog(@"member.name =============  %@",member.name);
         [array addObject:member];</span> 
        } 
         [membersDB close];<span style="color:#FF0000;"> 
         NSLog(@"array[0] == %@,
    array[1] == %@,
    array[2]==%@",[array[0] name],[array[1] name],[array[2] name]);</span>
         return [array copy];
     }
    

     <pre name="code" class="objc"><pre name="code" class="objc"> <span style="color:#FF0000;"></span>
    
    
    
    
    
       
    
  • 相关阅读:
    数据库的左右表连接
    数据库索引-索引的优点、缺点、分类、不走索引的情况
    selenium中日期控件的操作方法
    esdoc 自动生成接口文档介绍
    css设置背景图(背景图太大导致图片显示不全)
    k8s调度器及调度队列源码分析
    MIT6.824 2020 Lab1 MapReduce 实现
    git命令
    python3.6、3.9多版本安装共存
    使用蓝鲸平台登录态验证接入第三方系统
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/6806668.html
Copyright © 2011-2022 走看看