调用数组方法

时间:2012-09-29 03:52:41

标签: objective-c xcode

我有这个方法,我试图调用那个方法做[self getUsers ObjectAtIndex:0]但是编译器根本不会编译该行并说它是未知的。如何调用数组方法来显示其中的所有字符串?

- (NSArray*)getUsers

{

NSArray *getUsers = [[NSArray alloc] init];
NSString *databasepath;

NSString *docsDir;
NSArray *dirPaths;
//NSString *databasePath;

//get the document directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];

//build the path to the database file
databasepath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:
                                                  @"users.db"]];

const char *dbPath = [databasepath UTF8String];
sqlite3_stmt *statement;
sqlite3 *userdb;

if (sqlite3_open(dbPath, &userdb) == SQLITE_OK)
{
    NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM USERS WHERE username = \"%@\"", txtUsername.text];
    const char *sql_stmt = [querySQL UTF8String];
    if (sqlite3_prepare_v2(userdb, sql_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            char *field = (char*) sqlite3_column_text(statement, 1);
            NSString *usernameField = [[NSString alloc]initWithUTF8String:field];
            //NSLog(@"%@", usernameField);
            getUsers = [NSArray arrayWithObject:usernameField];

        }

    }
    sqlite3_finalize(statement);
}

sqlite3_close(userdb);

return getUsers;

}

2 个答案:

答案 0 :(得分:1)

使用[[self getUsers] objectAtIndex:0]

如果您使用最新的Xcode / clang版本,您也可以使用[self getUsers][0],因为数组现在支持索引。

答案 1 :(得分:0)

考虑说你有一个方法 - (id)someMethod,然后像这样调用它:

id myValue = [self someMethod];

在someMethod中,有一个return语句 - 返回的权利是调用方法或函数中分配给myValue的内容。

就这样打电话:

NSArray *arrAllUsers = [self getUsers]; //As getUsers is method which returns nsarray.