开发者

Array is null. Pushing from NSObject to AppDelegate

开发者 https://www.devze.com 2023-03-18 08:38 出处:网络
Again, Xcode newb here ... So I\'m having trouble ge开发者_Python百科tting my Items (code not shown but almost identical to the List query) and List arrays to show any data or even count. In the Dbase

Again, Xcode newb here ... So I'm having trouble ge开发者_Python百科tting my Items (code not shown but almost identical to the List query) and List arrays to show any data or even count. In the Dbase implementation file I can print out the array ... but in UWLAppDelegate I get nothing. I've seen examples of this issue on here but they don't seem to fix the issue. FYI, I'm using Preview Xcode also. So some things might not look right.

I've tried initializing as described on here and the tutorial:

    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
self.listArray = tempArray;

... but still no dice.

UWLAppDelegate.m  

- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self copyDatabase];

self.itemArray = [NSMutableArray array];
self.listArray = [NSMutableArray array];

NSLog(@"Items Array: %@",self.itemArray);
NSLog(@"List Array: %@",self.listArray);

[Dbase getDisplayData:[self getDBPath]];
[Dbase getListDisplayData:[self getDBPath]];
}

..

dbase.m

+ (void) getListDisplayData:(NSString *)dbPath {
UWLAppDelegate *appDelegate = (UWLAppDelegate *)[[UIApplication sharedApplication] delegate];
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK){
    const char *sql = "select id, name from lists";
    sqlite3_stmt *selectstmt;
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK){
        while(sqlite3_step(selectstmt) == SQLITE_ROW){
            NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
            Dbase *listObj = [[Dbase alloc] initWithPrimaryKey:primaryKey];
            listObj.dblistname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];

            listObj.isDirty = NO;

            [appDelegate.listArray addObject:listObj.dblistname];
        }
        //NSLog(@"%@",appDelegate.listArray);
        //NSLog(@"%d", [appDelegate.listArray count]);
    }
} else {
    sqlite3_close(database);
}
}

So the commented NSLog's will post data all 6 entires/row I've made from the App. Ideas?

Objective-C appDelegate array <-- didn't help either. I've looked at other similar questions with no avail. Ideas?


Frankly there doesn't seem to be a problem. It just seems to be a problem of printing in the wrong order. You have your NSLogs before you collect the data. That seems to be the reason why the arrays are empty in your app delegate method while they are printing in Dbase's methods.

Shouldn't it be?

self.itemArray = [NSMutableArray array];
self.listArray = [NSMutableArray array];

[Dbase getDisplayData:[self getDBPath]];
[Dbase getListDisplayData:[self getDBPath]];

NSLog(@"Items Array: %@",self.itemArray);
NSLog(@"List Array: %@",self.listArray);
0

精彩评论

暂无评论...
验证码 换一张
取 消