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 103765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:10:32+00:00 2026-05-11T01:10:32+00:00

When retrieving objects from an NSMutableArray in cocoa-touch is the below code ok? Should

  • 0

When retrieving objects from an NSMutableArray in cocoa-touch is the below code ok? Should I be allocating([alloc]) new Page objects each time or is just pointing to it alright? Do I need to do anything to the Page *pageObj after, such as set it to nil?

const char *sql = 'insert into Page(Book_ID, Page_Num, Page_Text) Values(?, ?, ?)'; for (i = 0; i < ([[self pagesArray] count] - 1); i++) {     if(addStmt == nil) {         if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK) {             NSAssert1(0, @'Error while creating add statement. '%s'', sqlite3_errmsg(database));         }     }     Page *pageObj = [[self pagesArray] objectAtIndex:i];     if(pageObj.isNew) {         sqlite3_bind_int(addStmt, 1, self.book_ID);          sqlite3_bind_int(addStmt, 2, pageObj.page_Number);           sqlite3_bind_text(addStmt, 3, [[pageObj page_Text] UTF8String], -1, SQLITE_TRANSIENT);         if(SQLITE_DONE != sqlite3_step(addStmt)) {             NSAssert1(0, @'Error while inserting data. '%s'', sqlite3_errmsg(database));         }         NSLog(@'Inserted Page: %i into DB. Page text: %@', pageObj.page_Number, pageObj.page_Text);     }     //Reset the add statement.     sqlite3_reset(addStmt);                      } 

Thanks. I also understand this should probably be in a transaction but I didn’t quite get that working just yet.

  • 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. 2026-05-11T01:10:32+00:00Added an answer on May 11, 2026 at 1:10 am

    The way you’re declaring a pointer is correct. You don’t need alloc, since that creates a new object, when you want to refer to an existing object in the array. You would want to retain it if you were going to keep the reference outside of that method, but since you’re only using it temporarily it’s fine not to.

    The actual pointer variable will be destroyed and recreated every trip to the loop, so there’s no need to set it to nil. Even if you declared the variable outside the loop, simply assigning it to a new object is fine. The only time you’d set it to nil is when you’re releasing the object stored in the pointer (or the object may be released elsewhere). If you didn’t set it to nil in that case, the pointer would refer to an invalid memory location after the object is dealloced, usually causing a crash.

    One bug I can see though, you’re skipping the last element in your array in your for loop by subtracting 1 from the count.

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

Sidebar

Ask A Question

Stats

  • Questions 57k
  • Answers 57k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer That link is ONE way, but it's nicer to use… May 11, 2026 at 8:22 am
  • added an answer There's a whole set of rules about what you can… May 11, 2026 at 8:22 am
  • added an answer SQLite by default creates the journal file at the start… May 11, 2026 at 8:22 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.