0

Im storing some values in my server. Then i fetched that values using JSON and added to local database table. Then i need to display that values to view. But array values displaying in NSLog. It won't displaying in view. I don't need to display in TableView.

code:

-(void) addDataToArray{


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    //NSLog(@"docs dir is %@", documentsDirectory);

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"db1.sqlite"];
    //NSLog(@"filepath %@",path);

    mArray = [[NSMutableArray alloc]init];


    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

        // const char *sql = "SELECT id,cat_name FROM categories order by order_by";

        const char *sql = "SELECT * FROM categories";

        NSLog(@"Sql is %s",sql);


        sqlite3_stmt *statement;
        int catID = 0;
        if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
            // We "step" through the results - once for each row.
            while (sqlite3_step(statement) == SQLITE_ROW) {


              NSString *catName = [[NSString alloc] initWithUTF8String:
                                     (const char *) sqlite3_column_text(statement, 1)];
                NSLog(@"catName is %@",catName);

                [mArray addObject:catName];

                // [self.view addConstraints:mArray];

                NSLog(@"mArray is %@", mArray);

                [catName release];

                catID = sqlite3_column_int(statement, 0);


            }
        }
        sqlite3_finalize(statement);
    } else {
        sqlite3_close(database);
        NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
        // Additional error handling, as appropriate...
    }
}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading");
[self addDataToArray];
}

NSLog:

catName is person1
mArray is (
    person1
)

catName is person2

mArray is (
    person1
    person2
)

TextView:

    UITextView *txt=[[UITextView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)];
      //  txt.text=[mArray objectAtIndex:0];
        [self.view addSubview:txt];

for (int i = 0; i<[mArray count]; i++ ) { NSLog(@"index %d",i); }
6
  • Where do you want to show JSON data ? Your question is not clear. Commented Aug 22, 2013 at 4:24
  • I need to show mArray value to TextView while button click. Commented Aug 22, 2013 at 4:45
  • You need to merge all objects of array first because you can show Only NSString in UITextView. Commented Aug 22, 2013 at 4:46
  • NSlog(@"%@",[mArray objectAtIndex:0]);just print what you getting? Commented Aug 22, 2013 at 5:00
  • UITextView *txt=[[UITextView alloc]initWithFrame:CGRectMake(50, 50, 200, 200)]; txt.text=[mArray objectAtIndex:0]; [self.view addSubview:txt];where these code calling? in viewDidload? Commented Aug 22, 2013 at 5:01

1 Answer 1

1

You need to merge array's objects to convert them in string, because you can't show an array in textView directly. If mArray contains NSString type object then you can do like this:

NSMutableString *string = [[NSMutableString alloc] init];
for (id obj in mArray){
    [string appendString:obj];
}

textView.text = string;

If you want to show each person in next line you can add \n after each name.

Sign up to request clarification or add additional context in comments.

2 Comments

How to display all values using for loop. Im using below code. for (int i = 0; i<[mArray count]; i++ ) { NSLog(@"index %d",i); }
for (int i=0; i<[mArray count]; i++ ) { NSLog(@"index %@", mArray[i]); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.