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

  • SEARCH
  • Home
  • 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 7646059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:06:33+00:00 2026-05-31T10:06:33+00:00

I’m very new at objective C, I’m just learning. I did the techotopia tutorial

  • 0

I’m very new at objective C, I’m just learning. I did the techotopia tutorial “An_Example_SQLite_based_iOS_4_iPhone_Application_(Xcode_4)”, then tried to implement it again with FMDB. (I’d post the link to the tutorial but it let’s me only post 2 links max)

The problem: In initWithFrame I create eventDB. Then in addEvent, after a keypress, the eventDB.database‘s contents are changed. This is eventDB in initWithFrame and this is it in addEvent.

#import "appTracker.h"
@implementation appTracker

- (id) initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    eventDB = [[appTrackerDB alloc] init];
    return self;
}


- (void) keyDown: (NSEvent *) event
{

    NSString *chars = [event characters];
    unichar character = [chars characterAtIndex: 0];
    if (character == 'A') {
        NSLog (@"Adding event");
        [self addEvent:@"test_arg"];            
    }
}

- (void) addEvent: (NSString *) name
{
    [eventDB setName:name];
    [eventDB setPhone:name];
    [eventDB setAddress:name];
    [eventDB setStatus:name];
    [eventDB saveData];
}
...
@end

Using GDB I stepped through and found that it is changing in main.m (autogenerated by XCode4) here: (not really sure what this code does or why it’s there)

    #import <Cocoa/Cocoa.h>

int main(int argc, char *argv[])
{
    return NSApplicationMain(argc, (const char **)argv);
}

I’m unfamiliar with objective C. Can someone help me figure out why my eventDB.database object is being changed? I’m probably not managing some memory correctly or totally misinterpreting how you are supposed to do this. Any help would be appreciated.

eventDB is an instance of:

#import <Foundation/Foundation.h>
#import "FMDatabase.h"

@interface appTrackerDB : NSObject {
    NSString *name;
    NSString *address;
    NSString *phone;
    NSString *status;
    NSString *databasePath;
    FMDatabase *database;
}

Thanks!

Also [eventDB saveData] is:

- (void) saveData
{
    [database executeUpdate:@"insert into user (name, address, phone) values(?,?,?)",
     name, address, phone,nil];
}

And created the database with:

@implementation appTrackerDB
@synthesize name,address,status,phone;

- (id)init
{
    self = [super init];
    if (self) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docsPath = [paths objectAtIndex:0];
        NSString *path = [docsPath stringByAppendingPathComponent:@"database.sqlite"];

        database = [FMDatabase databaseWithPath:path];
        [database open];

        [database executeUpdate:@"create table IF NOT EXISTS user(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)"];
        if ([database hadError]) {
            NSLog(@"DB Error %d: %@", [database lastErrorCode], [database lastErrorMessage]);
        }
        name = @"TEST";
    }

    return self;
}
  • 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-31T10:06:34+00:00Added an answer on May 31, 2026 at 10:06 am

    You don’t actually retain the Database. In Objective-C you need to manually retain the Objects, especially if they are not properties. (e.g. name is declared as a property, database is not)

    Retaining means that you own the Object. databaseWithObject retains the Database but calls autorelease on it, which normally means, it will delete the reference as soon as possible after the calling method is finished.

    Depending on your platform, e.g. OS X instead of iOS, you could enable the GarbageCollection-feature. This would mean, that the OSX/Objective-C environment would do a lot of the memory management for you. But for this to be of any use, you would need to the declare the pertaining instance variables as properties and use the appropriate setter- and getter-methods on them.

    Here is an example of a property-declaration (appTrackerDB.h):

    #import <Foundation/Foundation.h>
    #import "FMDatabase.h"
    
    @interface appTrackerDB : NSObject {
      /*
        These are only necessary when 
        using iOS-Versions prior to
        iOS 4, or if you really
        need to manipulate the values 
        without utilizing the setter-/getter-
        methods.
      */
      NSString *name;
      NSString *address;
      NSString *phone;
      NSString *status;
      NSString *databasePath;
      FMDatabase *database;
    }
    @property (retain) NSString *name,*address;
    @property (retain) NSString *phone,*status,*databasePath;
    @property (retain) FMDatabase *database;
    

    appTrackerDB.m:

    @implementation appTrackerDB
    @synthesize name,address,status,phone;
    @synthesize databasePath,database;
    

    An example setter method you would call instead of you manual assignment is:

    • [self setDatabase:...]; instead of assigning a value directly database = ...

    Setter methods like setVariableName and getter methods like variableName
    are synthesized for you by the @synthesize directive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.