Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 797489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:48:52+00:00 2026-05-14T22:48:52+00:00

I’m new to Objective-C and I’d like to abstract my database access using a

  • 0

I’m new to Objective-C and I’d like to abstract my database access using a model class like this:


@interface LectureModel : NSMutableDictionary
{
}

-(NSString*)title;
-(NSDate*)begin;

...

@end

I use the dictionary methods setValue:forKey: to store attributes and return these in the getters. Now I want to read these models from a sqlite database by using the Class dynamically.


+ (NSArray*)findBySQL:(NSString*)sql intoModelClass:(Class)modelClass
{
  NSMutableArray* models = [[[NSMutableArray alloc] init] autorelease];

  sqlite3* db = sqlite3_open(...);

  sqlite3_stmt* result = NULL;
  sqlite3_prepare_v2(db, [sql UTF8String], -1, &result, NULL);

  while(sqlite3_step(result) == SQLITE_ROW)
  {
    id modelInstance = [[modelClass alloc] init];

    for (int i = 0; i < sqlite3_column_count(result); ++i)
    {
      NSString* key = [NSString stringWithUTF8String:sqlite3_column_name(result, i)];
      NSString* value = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(result, i)];

      if([modelInstance respondsToSelector:@selector(setValue:forKey:)])
        [modelInstance setValue:value forKey:key];
    }

    [models addObject:modelInstance]; 
  }
  sqlite3_finalize(result);
  sqlite3_close(db);

  return models;
}

Funny thing is, the respondsToSelector: works, but if I try (in the debugger) to step over [modelInstance setValue:value forKey:key], it will throw an exception, and the stacktrace looks like:

#0  0x302ac924 in ___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___
#1  0x991d9509 in objc_exception_throw
#2  0x302d6e4d in __CFRequireConcreteImplementation
#3  0x00024d92 in +[DBManager findBySQL:intoModelClass:] at DBManager.m:114
#4  0x0001ea86 in -[FavoritesViewController initializeTableData:] at FavoritesViewController.m:423
#5  0x0001ee41 in -[FavoritesViewController initializeTableData] at FavoritesViewController.m:540
#6  0x305359da in __NSFireDelayedPerform
#7  0x302454a0 in CFRunLoopRunSpecific
#8  0x30244628 in CFRunLoopRunInMode
#9  0x32044c31 in GSEventRunModal
#10 0x32044cf6 in GSEventRun
#11 0x309021ee in UIApplicationMain
#12 0x00002988 in main at main.m:14

So, what’s wrong with this? Presumably I’m doing something really stupid and just don’t see it…

Many thanks in advance for your answers,
Arakyd :..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:48:53+00:00Added an answer on May 14, 2026 at 10:48 pm

    I think you need to start looking at Objective C from the start again it is not C++

    Your code example does not tell us that modelClass is a LectureModel so there are things missing so I might have misunderstood some things.

    First you have to call [super init] directly see Apple Objective C Primer.

    In Objective C you use composition instead than inheritance more often than in C++ – In this example I would use delegation to allow use of dictionary. LectureModel will pass any messages it does not understand to the dictionary attribute. Also from NSDictionary

    NSDictionary and NSMutableDictionary are part of a class cluster, so the objects you create with this interface are not actual instances of the these two classes. Rather, the instances belong to one of their private subclasses.

    So NSMutableDictionary is not really a good class to extend.

    This is partial code there are many parts missing including dealloc

    @interface LectureModel : NSObject
    {
      NSMutableDictionary *dict;
    }
    
    ...
    
    @end
    
    
    
    @implementation LectureModel
    
    
    -(LectureModel*)init
    {
      if (self = [super init]) {
        dict = [[NSMutableDictionary dictionary]retain]; // need to keep this
      }
      return self;
    }
    
    - (void)forwardInvocation:(NSInvocation *)invocation   // See NSObject for details
    {
        SEL aSelector = [invocation selector];
    
        [invocation invokeWithTarget:dict];   // let dict do the work
    }
    
    ...
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.