无法将Nsarray的对象转换为字符串

时间:2012-05-02 10:59:22

标签: iphone nsstring nsarray

我正在尝试将数组对象转换为字符串,但它仍然返回对象的地址。 它正在返回 - 0x5e1e370 但是应该返回一些字符串值。

NSMutableArray *arrStudents = [pillboxDb getStudentRecords];
NSString *strHow = [arrStudents objectAtIndex:0];
NSLog(@"%@",strHow);

以下是getStudentRecords的代码

+(NSMutableArray*)getStudentRecords{

NSArray *arrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strDestPath = [NSString stringWithFormat:@"%@/samplepillbox1.sqlite",[arrDocPath objectAtIndex:0]];
NSMutableArray *arrStudents = [[NSMutableArray alloc]init];
sqlite3 *db;
if(sqlite3_open([strDestPath UTF8String], &db)==SQLITE_OK)
{

    NSString *query = @"select * from doctor_master";
    void* v;
    char* err_msg;
    sqlite3_stmt *studentStmt;

    if(sqlite3_prepare_v2(db, [query UTF8String], -1, &studentStmt, &err_msg)==SQLITE_OK)
    {
        while (sqlite3_step(studentStmt)==SQLITE_ROW) {

            int sno = sqlite3_column_int(studentStmt, 0);
            NSString *sname = [NSString stringWithUTF8String: sqlite3_column_text(studentStmt, 1)];
            //float marks = sqlite3_column_double(studentStmt, 2);

            pillbox *st = [[pillbox alloc]init];
            st.Din = sno;
            st.Name = sname;

            [arrStudents addObject:st];
        }

    }

}
return arrStudents;

}

6 个答案:

答案 0 :(得分:1)

试试这个:

NSString *strHow = [[arrStudents objectAtIndex:0] stringValue] ;

答案 1 :(得分:1)

或许你的返回对象不是字符串对象。试试

的NSLog(@ “%@”,arrStudents);

它将显示对象类型,然后使用该对象连接字符串

答案 2 :(得分:1)

试试这个:

NSMutableArray *arrStudents = [pillboxDb getStudentRecords];
pillbox *st = [arrStudents objectAtIndex:0];
NSLog(@"%@ , %d", st.Name,st.Din);

答案 3 :(得分:0)

你的自定义pillBox类(顺便说一下,编码约定应该是名字PillBox)正在使用标准的NSObject描述方法:

  

description返回表示内容的字符串   接受课程。

+ (NSString *)description
  

返回值表示接收内容的字符串   类。

     

讨论调试器的print-object命令调用此方法   产生对象的文本描述。

     

NSObject的这种方法的实现只是打印出来的名字   类。

覆盖说明以提供所需的值。或者,创建一个stringValue方法,提供您需要的字符串。

答案 4 :(得分:0)

NSString *str = [arrStudents ObjectAtIndex:0];

答案 5 :(得分:0)

您正在将数组中的药丸类添加到数组中。因此您必须首先访问药丸对象

for(int i=0;i<[array count];i++)
{
pillbox *pill=[array objectAtIndex:i];

NSLog(@"%@  %@",pillbox.Name,pillbox.Din);
}

现在从那个药盒你必须访问pillbox.Name和pillbox.Din作为字符串你不能直接将数组转换为字符串!