在Objective C中使用NSMutableArray对象的指针 [英] Using pointers in Objective C for NSMutableArray objects

查看:83
本文介绍了在Objective C中使用NSMutableArray对象的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从可可触摸中的NSMutableArray检索对象时,下面的代码确定吗?我应该每次分配([alloc])新的页面对象还是只是指向它好吗?

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.

推荐答案

'声明一个指针是正确的。您不需要alloc,因为当您想要引用数组中的现有对象时,它会创建一个新对象。如果你要保持引用不在该方法之外,你想保留它,但是因为你只是暂时使用它。

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.

指针变量将被销毁并重新创建每次循环的行程,因此没有必要将其设置为nil。即使你在循环之外声明了变量,只需将它赋给一个新对象就行了。只有当你释放存储在指针中的对象时(或者对象可能被释放到其他地方),才会将其设置为nil。如果在这种情况下没有将其设置为nil,则指针将在对象被卸载后引用无效的内存位置,通常导致崩溃。

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.

一个错误我可以看到,你跳过你的for循环中的最后一个元素减去1从计数。

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.

这篇关于在Objective C中使用NSMutableArray对象的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆