从对象中检索属性

时间:2013-12-10 02:45:59

标签: objective-c sqlite

我对Xcode来说几乎是全新的。我正在尝试从sqlite3数据库中检索艺术家的信息。以下是我插入艺术家的地方:

- (IBAction)addArtistButton:(id)sender 
{
    NSString *artist_Name = artistFirstNameField.text;
    NSString *artist_LastName = artistLastNameField.text;
    NSString *artist_Phone = artistPhone.text;
    NSString *artist_Address = artistAddress.text;
    NSString *artist_Email = artistEmail.text;
    NSString *artist_Bio = artistBio.text;
    NSString *artist_SundayHours = artistSundayHours.text;
    NSString *artist_MondayHours = artistMondayHours.text;
    NSString *artist_TuesdayHours = artistTuesdayHours.text;
    NSString *artist_WednesdayHours = artistWednesdayHours.text;
    NSString *artist_ThursdayHours = artistThursdayHours.text;
    NSString *artist_FridayHours = artistFridayHours.text;
    NSString *artist_SaturdayHours = artistSaturdayHours.text;

    int SunOn = artistSundayOn.on;
    int MonOn = artistMondayOn.on;
    int TueOn = artistTuesdayOn.on;
    int WedOn = artistWednesdayOn.on;
    int ThurOn = artistThursdayOn.on;
    int FriOn = artistFridayOn.on;
    int SatOn = artistSaturdayOn.on;

    if (sqlite3_open([dbPathString UTF8String], &artistDB)==SQLITE_OK) {


        NSString *insertStmt = [NSString stringWithFormat:@"INSERT INTO ARTISTS(NAME, LASTNAME, PHONE, ADDRESS, EMAIL, BIO, SUNDAY_HOURS, MONDAY_HOURS, TUESDAY_HOURS, WEDNESDAY_HOURS, THURSDAY_HOURS, FRIDAY_HOURS, SATURDAY_HOURS, SUNDAY_ON, MONDAY_ON, TUESDAY_ON, WEDNESDAY_ON, THURSDAY_ON, FRIDAY_ON, SATURDAY_ON) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",[artist_Name UTF8String], [artist_LastName UTF8String], [artist_Phone UTF8String], [artist_Address UTF8String], [artist_Email UTF8String], [artist_Bio UTF8String], [artist_SundayHours UTF8String], [artist_MondayHours UTF8String], [artist_TuesdayHours UTF8String], [artist_WednesdayHours UTF8String], [artist_ThursdayHours UTF8String], [artist_FridayHours UTF8String], [artist_SaturdayHours UTF8String], SunOn, MonOn, TueOn, WedOn, ThurOn, FriOn, SatOn];
        const char *insert_stmt = [insertStmt UTF8String];
        char *error;

        if (sqlite3_exec(artistDB, insert_stmt, NULL, NULL, &error)==SQLITE_OK) {

            // NSLog(@"Insert: %s", insert_stmt);

            Artists *Artist = [[Artists alloc]init];
            [Artist setNAME:artist_Name];
            [Artist setLASTNAME:artist_LastName];

            [arrayOfArtist addObject:Artist];
            NSLog(@"Added artist");

        }else{ NSLog(@"Uh Oh: %s", error); 
        }
        sqlite3_close(artistDB);
    }else{ NSLog(@"Unable to open artistDB"); 
    }
}

以下是我现在所掌握的信息。

 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    //UILabel *label = (UILabel *)[cell viewWithTag:100];
    UILabel *last  = (UILabel *)[cell viewWithTag:200];

    NSLog(@"arrayOfArtist: %@", arrayOfArtist);

    last.text   = [arrayOfArtist objectAtIndex:2];

    [cell.layer setBorderWidth:2.0f];
    [cell.layer setBorderColor:[UIColor whiteColor].CGColor];
    [cell.layer setCornerRadius:20.0f];

    return cell;
}

Right now those NSLog entries result in the following:

    2013-12-09 21:26:22.855 InkStudiosNav[24585:70b] arrayOfArtist: (
    "<Artists: 0x8ab96a0>",
    "<Artists: 0x8abede0>",
    "<Artists: 0x8abf010>")
    2013-12-09 21:26:22.856 InkStudiosNav[24585:70b] -[Artists length]: unrecognized selector sent to instance 0x8abf010
    2013-12-09 21:26:23.106 InkStudiosNav[24585:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Artists length]: unrecognized selector sent to instance 0x8abf010'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x0182b5e4 __exceptionPreprocess + 180
        1   libobjc.A.dylib                     0x015ae8b6 objc_exception_throw + 44
        2   CoreFoundation                      0x018c8903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
        3   CoreFoundation                      0x0181b90b ___forwarding___ + 1019
        4   CoreFoundation                      0x0181b4ee _CF_forwarding_prep_0 + 14
        5   Foundation                          0x011f38ed -[NSConcreteMutableAttributedString replaceCharactersInRange:withString:] + 39
        6   Foundation                          0x011f455a -[NSConcreteMutableAttributedString initWithString:attributes:] + 293
        7   UIKit                               0x004c9bc6 -[UILabel _setText:] + 97
        8   UIKit                               0x004c9d84 -[UILabel setText:] + 40
        9   InkStudiosNav                       0x000048ce -[SecondViewController collectionView:cellForItemAtIndexPath:] + 318
        10  UIKit                               0x0092ebc8 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 257
        11  UIKit                               0x00930217 -[UICollectionView _updateVisibleCellsNow:] + 3677
        12  UIKit                               0x0093357f -[UICollectionView layoutSubviews] + 267
        13  UIKit                               0x00387267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
        14  libobjc.A.dylib                     0x015c081f -[NSObject performSelector:withObject:] + 70
        15  QuartzCore                          0x03c392ea -[CALayer layoutSublayers] + 148
        16  QuartzCore                          0x03c2d0d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
        17  QuartzCore                          0x03c2cf40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
        18  QuartzCore                          0x03b94ae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
        19  QuartzCore                          0x03b95e71 _ZN2CA11Transaction6commitEv + 393
        20  QuartzCore                          0x03b96544 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
        21  CoreFoundation                      0x017f34ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
        22  CoreFoundation                      0x017f341f __CFRunLoopDoObservers + 399
        23  CoreFoundation                      0x017d1344 __CFRunLoopRun + 1076
        24  CoreFoundation                      0x017d0ac3 CFRunLoopRunSpecific + 467
        25  CoreFoundation                      0x017d08db CFRunLoopRunInMode + 123
        26  GraphicsServices                    0x037d09e2 GSEventRunModal + 192
        27  GraphicsServices                    0x037d0809 GSEventRun + 104
        28  UIKit                               0x0031cd3b UIApplicationMain + 1225
        29  InkStudiosNav                       0x000054dd main + 141
        30  libdyld.dylib                       0x01d6c70d start + 1
    )
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb) 

我正在尝试从表/ db中检索“NAME”和“LASTNAME”。谁能帮我?我一直在谷歌搜索几个小时,但显然我不是在搜索正确的术语或其他东西。

插入艺术家的代码工作正常,我使用SQLite3 Database浏览器查看了数据库条目。

1 个答案:

答案 0 :(得分:1)

根据你的回溯,看来这行就是问题所在:

last.text   = [arrayOfArtist objectAtIndex:2];

您在这里为last.text分配了Artists而不是NSString。你的意思是:

last.text   = [[arrayOfArtist objectAtIndex:2] NAME];

或类似的东西?