0

I am getting rows from a SQLite DB and trying to insert them into a dictionary. Except I keep getting errors! I get the error "Implicit conversion of an Objective-C pointer to 'const id *' is disallowed with ARC" Which I know means that I cant use a pointer when I am adding objects to my dictionary. So how do I go about fixing it so I can add those arrays to a dictionary?

    NSArray *keyArray = [[NSMutableArray alloc] init ];
    NSArray *valueArray = [[NSMutableArray alloc ] init ];
    NSDictionary* dic;
    NSInteger dataCount = sqlite3_data_count(statement);
    while (sqlite3_step(statement) == SQLITE_ROW) {
        @try {
            for (int i = 0; i < dataCount; i ++)
            {
                NSString* key = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)];  
                NSString *value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)];

                if ([value length] == 0)
                {
                    value = @"";
                }

                keyArray = [keyArray arrayByAddingObject:key];
                valueArray = [valueArray arrayByAddingObject:value];

            }

        }
        @catch (NSException *ex)
        {
            NSLog(@"%@,%@", [ex name], [ex reason]);
        }

        dic= [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray count:[keyArray count]];
3
  • 2
    Why not use NSMutableArrays? And where exactly is your error? Commented Jul 30, 2012 at 18:00
  • 1
    On what line are you getting the error Commented Jul 30, 2012 at 18:00
  • A 0 length string is already @"" and there is no need to check the value. If you are checking for NULL then you need to check the value of sqlite3_column_text because stringWithUTF8String: will crash on NULL values. Commented Jul 30, 2012 at 18:06

2 Answers 2

5

The dictionaryWithObjects:forKeys:count: takes C-style arrays, not NSArray objects. The dictionaryWithObjects:forKeys: may do the trick, but you may be better off constructing a mutable dictionary as you go, bypassing NSArrays entirely.

NSDictionary* dic;
NSMutableDictionary *tmp = [NSMutableDictionary dictionary];

for (int i = 0; i < dataCount; i ++)
{
    NSString* key = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)];  
    NSString *value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, i)];
    [tmp setObject:value forKey:key];
}
dict = tmp;
Sign up to request clarification or add additional context in comments.

4 Comments

dictionaryWithObjects:<#(NSArray *)#> forKeys:<#(NSArray *)#> takes NSArrays, would that help?
@OscarHernandez Thank you very much for bringing this up, I updated the answer. Thanks!
Since your answer is correct lets nitpick at it! Remove the [value length] check as it is unnecessary. See my comment above.
@Joe You're right, the value is not nil there, because a DB null would have crashed the line above it! Thanks!
0

[dicBodyPost setValue:arrContactAddress forKey:@"contactAddress"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.