【问题标题】:Get Address of Pointer Variable in Objective-C在 Objective-C 中获取指针变量的地址
【发布时间】:2012-10-15 03:44:02
【问题描述】:

在 sqlite3_open 函数(通用 C 函数)中,我需要将 sqlite3 通用 C 指针的地址作为参数发送。
示例:

//Valid code
NSString * databaseFile; //Path of database file. (Objective-C Object)
sqlite3 * db; //Pointer to hold the sqlite database. (C Pointer)

sqlite3_open([databaseFile UTF8String], &db); //db is sent using the & operator.

问题是我需要从对象内部获取 *db 变量。 我知道这段代码是错误的,但它是一种解释我需要什么的方式:

//I need to get the address of the pointer 'database' from inside the Objective-C Object 'object'.
//I hope one of those wrong lines explain what I want:

sqlite3_open([databaseFile UTF8String], &[object database]);
sqlite3_open([databaseFile UTF8String], [object &database]);
sqlite3_open([databaseFile UTF8String], &(object->database);

我希望有人能理解我...哈哈哈 感谢您的时间! =)

【问题讨论】:

  • 您可能想设置一个临时变量database,然后将&database 传递给sqlite3_open(),然后调用[object setDatabase:database](或object.database = database
  • 我假设当您说“从对象内部”时,您的意思是引用属性。请记住,属性引用会变成对 getter 或 setter 方法的调用。您不能“获取属性的地址”。
  • @nielsbot 是的!谢谢!这太简单了......我在这个问题上徘徊了很长时间,以至于我忘记了我可以简单地做相反的事情。请将此设置为答案,以便我投票。 :)

标签: objective-c sqlite addressof


【解决方案1】:

假设MyClass 看起来像这样:

@interface MyClass : NSObject
...
@property ( nonatomic ) sqlite3 * database ;
...
@end

您的代码将是:

MyClass * object = ... ;

sqlite3 * database ;
sqlite3_open( ..., & database ) ;

object.database = database ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多